/* {{&header_text}} The MIT License (MIT) Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*jshint unused:false */ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify) { var default_opts = { indent_size: 4, indent_char: ' ', preserve_newlines: true, jslint_happy: false, keep_array_indentation: false, brace_style: 'collapse', space_before_conditional: true, break_chained_methods: false, selector_separator: '\n', end_with_newline: false }; var opts; {{#default_options}} default_opts.{{name}} = {{&value}}; {{/default_options}} function reset_options() { opts = JSON.parse(JSON.stringify(default_opts)); } function test_html_beautifier(input) { return html_beautify(input, opts); } var sanitytest; // test the input on beautifier with the current flag settings // does not check the indentation / surroundings as bt() does function test_fragment(input, expected) { expected = expected || expected === '' ? expected : input; sanitytest.expect(input, expected); // if the expected is different from input, run it again // expected output should be unchanged when run twice. if (expected !== input) { sanitytest.expect(expected, expected); } // Everywhere we do newlines, they should be replaced with opts.eol opts.eol = '\r\n'; expected = expected.replace(/[\n]/g, '\r\n'); sanitytest.expect(input, expected); if (input.indexOf('\n') !== -1) { input = input.replace(/[\n]/g, '\r\n'); sanitytest.expect(input, expected); // Ensure support for auto eol detection opts.eol = 'auto'; sanitytest.expect(input, expected); } opts.eol = '\n'; } // test html function bth(input, expectation) { var wrapped_input, wrapped_expectation, field_input, field_expectation; expectation = expectation || expectation === '' ? expectation : input; sanitytest.test_function(test_html_beautifier, 'html_beautify'); test_fragment(input, expectation); if (opts.indent_size === 4 && input) { wrapped_input = '
\n' + input.replace(/^(.+)$/mg, ' $1') + '\n inline\n
'; wrapped_expectation = '
\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n inline\n
'; test_fragment(wrapped_input, wrapped_expectation); } } function unicode_char(value) { return String.fromCharCode(value); } function beautifier_tests() { sanitytest = test_obj; reset_options(); //============================================================ bth(''); {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}} //============================================================ {{^matrix}} // {{&name}} reset_options(); {{#options}} opts.{{name}} = {{&value}}; {{/options}} {{#tests}} {{#test_line}}.{{/test_line}}; {{/tests}} {{/matrix}} {{#matrix}} // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}}) reset_options(); {{#options}} opts.{{name}} = {{&value}}; {{/options}} {{#tests}} {{#test_line}}.{{/test_line}}; {{/tests}} {{/matrix}} {{#unset_mustache_tags}}.{{/unset_mustache_tags}}{{/groups}} } function beautifier_unconverted_tests() { sanitytest = test_obj; reset_options(); //============================================================ opts.end_with_newline = true; test_fragment('', '\n'); test_fragment('
\n'); test_fragment('
\n\n\n', '
\n'); test_fragment('\n' + ' \n' + '\n'); opts.end_with_newline = false; // error cases need love too bth('Evil'); bth(" test_fragment( '\n' + ' \n' + ''); test_fragment('
\n', '
'); bth('
'); bth('
content
'); bth('
', '
\n' + '
\n' + '
'); bth('
content
', '
\n' + '
content
\n' + '
'); bth('
\n' + ' content\n' + '
'); bth('
\n' + '
'); bth('
\n' + ' content\n' + '
'); bth('
\n' + '
', '
\n' + '
'); bth('
\n' + '
', '
\n' + '
'); bth('
\n' + '
\n' + '
\n' + '
', '
\n' + '
\n' + '
\n' + '
'); bth('
\n' + '
', '
\n' + '
'); bth('
content
', '
content
'); bth('
', '
'); bth('content\n' + '
\n' + '
\n' + 'content', 'content\n' + '
\n' + '
\n' + 'content'); bth('
  • \n' + '
    \n' + '
    \n' + '
  • '); bth('
  • \n' + '
    \n' + '
    \n' + '
  • ', '
  • \n' + '
    \n' + '
    \n' + '
  • '); bth('
  • \n' + ' content\n' + '
  • \n' + '
  • \n' + ' content\n' + '
  • '); bth('content'); bth(' content'); bth(' content', ' content'); bth('content'); bth(' content'); bth(' content', ' content'); bth('content'); bth(' content'); bth(' content', ' content'); bth('
    contentcontent
    '); bth('
    content content
    '); bth('
    content content
    ', '
    content content
    '); bth('Text Link Text'); var unformatted = opts.unformatted; opts.unformatted = ['script', 'style']; bth(''); bth(''); opts.unformatted = unformatted; unformatted = opts.unformatted; opts.unformatted = ['custom-element']; test_fragment('
    should not' + ' insert newlines
    ', '
    should not' + ' insert newlines
    '); opts.unformatted = unformatted; // Tests that don't pass, but probably should. // bth('
    content
    '); // Handlebars tests // Without the indent option on, handlebars are treated as content. opts.wrap_line_length = 0; //...---------1---------2---------3---------4---------5---------6---------7 //...1234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some text that should not wrap at all.
    ', /* expected */ '
    Some text that should not wrap at all.
    '); // A value of 0 means no max line length, and should not wrap. //...---------1---------2---------3---------4---------5---------6---------7---------8---------9--------10--------11--------12--------13--------14--------15--------16--------17--------18--------19--------20--------21--------22--------23--------24--------25--------26--------27--------28--------29 //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.
    ', /* expected */ '
    Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.
    '); opts.wrap_line_length = "0"; //...---------1---------2---------3---------4---------5---------6---------7 //...1234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some text that should not wrap at all.
    ', /* expected */ '
    Some text that should not wrap at all.
    '); // A value of "0" means no max line length, and should not wrap //...---------1---------2---------3---------4---------5---------6---------7---------8---------9--------10--------11--------12--------13--------14--------15--------16--------17--------18--------19--------20--------21--------22--------23--------24--------25--------26--------27--------28--------29 //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.
    ', /* expected */ '
    Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all. Some text that should not wrap at all.
    '); //BUGBUG: This should wrap before 40 not after. opts.wrap_line_length = 40; //...---------1---------2---------3---------4---------5---------6---------7 //...1234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some test text that should wrap_inside_this section here.
    ', /* expected */ '
    Some test text that should wrap_inside_this\n' + ' section here.
    '); opts.wrap_line_length = "40"; //...---------1---------2---------3---------4---------5---------6---------7 //...1234567890123456789012345678901234567890123456789012345678901234567890 bth('
    Some test text that should wrap_inside_this section here.
    ', /* expected */ '
    Some test text that should wrap_inside_this\n' + ' section here.
    '); opts.indent_size = 1; opts.indent_char = '\t'; opts.preserve_newlines = false; bth('
    \n\tfoo\n
    ', '
    foo
    '); opts.preserve_newlines = true; bth('
    \n\tfoo\n
    '); // test preserve_newlines and max_preserve_newlines opts.preserve_newlines = false; bth('
    Should not
    \n\n\n' + '
    preserve newlines
    ', '
    Should not
    \n' + '
    preserve newlines
    '); opts.preserve_newlines = true; opts.max_preserve_newlines = 0; bth('
    Should
    \n\n\n' + '
    preserve zero newlines
    ', '
    Should
    \n' + '
    preserve zero newlines
    '); opts.max_preserve_newlines = 1; bth('
    Should
    \n\n\n' + '
    preserve one newline
    ', '
    Should
    \n\n' + '
    preserve one newline
    '); opts.max_preserve_newlines = null; bth('
    Should
    \n\n\n' + '
    preserve one newline
    ', '
    Should
    \n\n\n' + '
    preserve one newline
    '); } beautifier_tests(); beautifier_unconverted_tests(); } if (typeof exports !== "undefined") { exports.run_html_tests = run_html_tests; }