| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- /*global js_beautify: true */
- function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
- {
- var 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
- };
- 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);
- input = input.replace(/[\n]/g, '\r\n');
- 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 = '<div>\n' + input.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
- wrapped_expectation = '<div>\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
- test_fragment(wrapped_input, wrapped_expectation);
- }
- }
- function unicode_char(value) {
- return String.fromCharCode(value)
- }
- function beautifier_tests()
- {
- sanitytest = test_obj;
- bth('');
- {{#default_options}} opts.{{name}} = {{&value}};
- {{/default_options}}
- {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}}{{^matrix}}
- // {{&name}}
- {{#options}}
- opts.{{name}} = {{&value}};
- {{/options}}
- {{#tests}}
- {{#test_line}}.{{/test_line}};
- {{/tests}}
- {{/matrix}}{{#matrix}}
- // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
- {{#options}}
- opts.{{name}} = {{&value}};
- {{/options}}
- {{#tests}}
- {{#test_line}}.{{/test_line}};
- {{/tests}}
- {{/matrix}}{{#unset_mustache_tags}}.{{/unset_mustache_tags}}
- {{/groups}}
- opts.end_with_newline = true;
- test_fragment('', '\n');
- test_fragment('<div></div>\n');
- test_fragment('<div></div>\n\n\n', '<div></div>\n');
- test_fragment('<head>\n' +
- ' <script>\n' +
- ' mocha.setup("bdd");\n' +
- '\n' +
- ' </script>\n' +
- '</head>\n');
- opts.end_with_newline = false;
- // error cases need love too
- bth('<img title="Bad food!" src="foo.jpg" alt="Evil" ">');
- bth("<!-- don't blow up if a comment is not complete"); // -->
- test_fragment(
- '<head>\n' +
- ' <script>\n' +
- ' mocha.setup("bdd");\n' +
- ' </script>\n' +
- '</head>');
- test_fragment('<div></div>\n', '<div></div>');
- bth('<div></div>');
- bth('<div>content</div>');
- bth('<div><div></div></div>',
- '<div>\n' +
- ' <div></div>\n' +
- '</div>');
- bth('<div><div>content</div></div>',
- '<div>\n' +
- ' <div>content</div>\n' +
- '</div>');
- bth('<div>\n' +
- ' <span>content</span>\n' +
- '</div>');
- bth('<div>\n' +
- '</div>');
- bth('<div>\n' +
- ' content\n' +
- '</div>');
- bth('<div>\n' +
- ' </div>',
- '<div>\n' +
- '</div>');
- bth(' <div>\n' +
- ' </div>',
- '<div>\n' +
- '</div>');
- bth('<div>\n' +
- '</div>\n' +
- ' <div>\n' +
- ' </div>',
- '<div>\n' +
- '</div>\n' +
- '<div>\n' +
- '</div>');
- bth(' <div>\n' +
- '</div>',
- '<div>\n' +
- '</div>');
- bth('<div >content</div>',
- '<div>content</div>');
- bth('<div thinger="preserve space here" ></div >',
- '<div thinger="preserve space here"></div>');
- bth('content\n' +
- ' <div>\n' +
- ' </div>\n' +
- 'content',
- 'content\n' +
- '<div>\n' +
- '</div>\n' +
- 'content');
- bth('<li>\n' +
- ' <div>\n' +
- ' </div>\n' +
- '</li>');
- bth('<li>\n' +
- '<div>\n' +
- '</div>\n' +
- '</li>',
- '<li>\n' +
- ' <div>\n' +
- ' </div>\n' +
- '</li>');
- bth('<li>\n' +
- ' content\n' +
- '</li>\n' +
- '<li>\n' +
- ' content\n' +
- '</li>');
- bth('<img>content');
- bth('<img> content');
- bth('<img> content', '<img> content');
- bth('<img><img>content');
- bth('<img> <img>content');
- bth('<img> <img>content', '<img> <img>content');
- bth('<img><b>content</b>');
- bth('<img> <b>content</b>');
- bth('<img> <b>content</b>', '<img> <b>content</b>');
- bth('<div>content<img>content</div>');
- bth('<div> content <img> content</div>');
- bth('<div> content <img> content </div>',
- '<div> content <img> content </div>');
- bth('Text <a href="#">Link</a> Text');
- // START tests for issue 453
- bth('<script type="text/unknown"><div></div></script>',
- '<script type="text/unknown">\n' +
- ' <div></div>\n' +
- '</script>');
- bth('<script type="text/javascript"><div></div></script>',
- '<script type="text/javascript">\n' +
- ' < div > < /div>\n' +
- '</script>');
- bth('<script><div></div></script>',
- '<script>\n' +
- ' < div > < /div>\n' +
- '</script>');
- bth('<script type="text/javascript">var foo = "bar";</script>',
- '<script type="text/javascript">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script type="application/javascript">var foo = "bar";</script>',
- '<script type="application/javascript">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script type="application/javascript;version=1.8">var foo = "bar";</script>',
- '<script type="application/javascript;version=1.8">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script type="application/x-javascript">var foo = "bar";</script>',
- '<script type="application/x-javascript">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script type="application/ecmascript">var foo = "bar";</script>',
- '<script type="application/ecmascript">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script type="text/javascript1.5">var foo = "bar";</script>',
- '<script type="text/javascript1.5">\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<script>var foo = "bar";</script>',
- '<script>\n' +
- ' var foo = "bar";\n' +
- '</script>');
- bth('<style type="text/unknown"><tag></tag></style>',
- '<style type="text/unknown">\n' +
- ' <tag></tag>\n' +
- '</style>');
- bth('<style type="text/css"><tag></tag></style>',
- '<style type="text/css">\n' +
- ' <tag></tag>\n' +
- '</style>');
- bth('<style><tag></tag></style>',
- '<style>\n' +
- ' <tag></tag>\n' +
- '</style>');
- bth('<style type="text/css">.selector {font-size:12px;}</style>',
- '<style type="text/css">\n' +
- ' .selector {\n' +
- ' font-size: 12px;\n' +
- ' }\n'+
- '</style>');
- bth('<style>.selector {font-size:12px;}</style>',
- '<style>\n' +
- ' .selector {\n' +
- ' font-size: 12px;\n' +
- ' }\n'+
- '</style>');
- // END tests for issue 453
- var unformatted = opts.unformatted;
- opts.unformatted = ['script', 'style'];
- bth('<script id="javascriptTemplate" type="text/x-kendo-template">\n' +
- ' <ul>\n' +
- ' # for (var i = 0; i < data.length; i++) { #\n' +
- ' <li>#= data[i] #</li>\n' +
- ' # } #\n' +
- ' </ul>\n' +
- '</script>');
- bth('<style>\n' +
- ' body {background-color:lightgrey}\n' +
- ' h1 {color:blue}\n' +
- '</style>');
- opts.unformatted = unformatted;
- unformatted = opts.unformatted;
- opts.unformatted = ['custom-element'];
- test_fragment('<div>should <custom-element>not</custom-element>' +
- ' insert newlines</div>',
- '<div>should <custom-element>not</custom-element>' +
- ' insert newlines</div>');
- opts.unformatted = unformatted;
- // Tests that don't pass, but probably should.
- // bth('<div><span>content</span></div>');
- // 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('<div>Some text that should not wrap at all.</div>',
- /* expected */
- '<div>Some text that should not wrap at all.</div>');
- // 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('<div>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.</div>',
- /* expected */
- '<div>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.</div>');
- opts.wrap_line_length = "0";
- //...---------1---------2---------3---------4---------5---------6---------7
- //...1234567890123456789012345678901234567890123456789012345678901234567890
- bth('<div>Some text that should not wrap at all.</div>',
- /* expected */
- '<div>Some text that should not wrap at all.</div>');
- // 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('<div>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.</div>',
- /* expected */
- '<div>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.</div>');
- //BUGBUG: This should wrap before 40 not after.
- opts.wrap_line_length = 40;
- //...---------1---------2---------3---------4---------5---------6---------7
- //...1234567890123456789012345678901234567890123456789012345678901234567890
- bth('<div>Some test text that should wrap_inside_this section here.</div>',
- /* expected */
- '<div>Some test text that should wrap_inside_this\n' +
- ' section here.</div>');
- opts.wrap_line_length = "40";
- //...---------1---------2---------3---------4---------5---------6---------7
- //...1234567890123456789012345678901234567890123456789012345678901234567890
- bth('<div>Some test text that should wrap_inside_this section here.</div>',
- /* expected */
- '<div>Some test text that should wrap_inside_this\n' +
- ' section here.</div>');
- opts.indent_size = 1;
- opts.indent_char = '\t';
- opts.preserve_newlines = false;
- bth('<div>\n\tfoo\n</div>', '<div> foo </div>');
- opts.preserve_newlines = true;
- bth('<div>\n\tfoo\n</div>');
- // test preserve_newlines and max_preserve_newlines
- opts.preserve_newlines = false;
- bth('<div>Should not</div>\n\n\n' +
- '<div>preserve newlines</div>',
- '<div>Should not</div>\n' +
- '<div>preserve newlines</div>');
- opts.preserve_newlines = true;
- opts.max_preserve_newlines = 0;
- bth('<div>Should</div>\n\n\n' +
- '<div>preserve zero newlines</div>',
- '<div>Should</div>\n' +
- '<div>preserve zero newlines</div>');
- opts.max_preserve_newlines = 1;
- bth('<div>Should</div>\n\n\n' +
- '<div>preserve one newline</div>',
- '<div>Should</div>\n\n' +
- '<div>preserve one newline</div>');
- opts.max_preserve_newlines = null;
- bth('<div>Should</div>\n\n\n' +
- '<div>preserve one newline</div>',
- '<div>Should</div>\n\n\n' +
- '<div>preserve one newline</div>');
- }
- beautifier_tests();
- }
- if (typeof exports !== "undefined") {
- exports.run_html_tests = run_html_tests;
- }
|