corrade-nucleus-nucleons – Blame information for rev 20

Subversion Repositories:
Rev:
Rev Author Line No. Line
20 office 1 /*
2 {{&header_text}}
3  
4 The MIT License (MIT)
5  
6 Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors.
7  
8 Permission is hereby granted, free of charge, to any person
9 obtaining a copy of this software and associated documentation files
10 (the "Software"), to deal in the Software without restriction,
11 including without limitation the rights to use, copy, modify, merge,
12 publish, distribute, sublicense, and/or sell copies of the Software,
13 and to permit persons to whom the Software is furnished to do so,
14 subject to the following conditions:
15  
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18  
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 SOFTWARE.
27 */
28 /*jshint unused:false */
29  
30 function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
31 {
32  
33 var default_opts = {
34 indent_size: 4,
35 indent_char: ' ',
36 preserve_newlines: true,
37 jslint_happy: false,
38 keep_array_indentation: false,
39 brace_style: 'collapse',
40 space_before_conditional: true,
41 break_chained_methods: false,
42 selector_separator: '\n',
43 end_with_newline: false
44 };
45 var opts;
46  
47 {{#default_options}} default_opts.{{name}} = {{&value}};
48 {{/default_options}}
49  
50 function reset_options()
51 {
52 opts = JSON.parse(JSON.stringify(default_opts));
53 }
54  
55 function test_html_beautifier(input)
56 {
57 return html_beautify(input, opts);
58 }
59  
60 var sanitytest;
61  
62 // test the input on beautifier with the current flag settings
63 // does not check the indentation / surroundings as bt() does
64 function test_fragment(input, expected)
65 {
66 expected = expected || expected === '' ? expected : input;
67 sanitytest.expect(input, expected);
68 // if the expected is different from input, run it again
69 // expected output should be unchanged when run twice.
70 if (expected !== input) {
71 sanitytest.expect(expected, expected);
72 }
73  
74 // Everywhere we do newlines, they should be replaced with opts.eol
75 opts.eol = '\r\n';
76 expected = expected.replace(/[\n]/g, '\r\n');
77 sanitytest.expect(input, expected);
78 if (input.indexOf('\n') !== -1) {
79 input = input.replace(/[\n]/g, '\r\n');
80 sanitytest.expect(input, expected);
81 // Ensure support for auto eol detection
82 opts.eol = 'auto';
83 sanitytest.expect(input, expected);
84 }
85 opts.eol = '\n';
86 }
87  
88 // test html
89 function bth(input, expectation)
90 {
91 var wrapped_input, wrapped_expectation, field_input, field_expectation;
92  
93 expectation = expectation || expectation === '' ? expectation : input;
94 sanitytest.test_function(test_html_beautifier, 'html_beautify');
95 test_fragment(input, expectation);
96  
97 if (opts.indent_size === 4 && input) {
98 wrapped_input = '<div>\n' + input.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
99 wrapped_expectation = '<div>\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
100 test_fragment(wrapped_input, wrapped_expectation);
101 }
102 }
103  
104 function unicode_char(value) {
105 return String.fromCharCode(value);
106 }
107  
108 function beautifier_tests()
109 {
110 sanitytest = test_obj;
111  
112 reset_options();
113 //============================================================
114 bth('');
115  
116 {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}}
117 //============================================================
118 {{^matrix}}
119 // {{&name}}
120 reset_options();
121 {{#options}}
122 opts.{{name}} = {{&value}};
123 {{/options}}
124 {{#tests}}
125 {{#test_line}}.{{/test_line}};
126 {{/tests}}
127  
128 {{/matrix}}
129 {{#matrix}}
130 // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
131 reset_options();
132 {{#options}}
133 opts.{{name}} = {{&value}};
134 {{/options}}
135 {{#tests}}
136 {{#test_line}}.{{/test_line}};
137 {{/tests}}
138  
139 {{/matrix}}
140 {{#unset_mustache_tags}}.{{/unset_mustache_tags}}{{/groups}}
141 }
142  
143 function beautifier_unconverted_tests()
144 {
145 sanitytest = test_obj;
146  
147 reset_options();
148 //============================================================
149 opts.end_with_newline = true;
150 test_fragment('', '\n');
151 test_fragment('<div></div>\n');
152 test_fragment('<div></div>\n\n\n', '<div></div>\n');
153 test_fragment('<head>\n' +
154 ' <script>\n' +
155 ' mocha.setup("bdd");\n' +
156 '\n' +
157 ' </script>\n' +
158 '</head>\n');
159  
160  
161 opts.end_with_newline = false;
162 // error cases need love too
163 bth('<img title="Bad food!" src="foo.jpg" alt="Evil" ">');
164 bth("<!-- don't blow up if a comment is not complete"); // -->
165  
166 test_fragment(
167 '<head>\n' +
168 ' <script>\n' +
169 ' mocha.setup("bdd");\n' +
170 ' </script>\n' +
171 '</head>');
172  
173 test_fragment('<div></div>\n', '<div></div>');
174 bth('<div></div>');
175 bth('<div>content</div>');
176 bth('<div><div></div></div>',
177 '<div>\n' +
178 ' <div></div>\n' +
179 '</div>');
180 bth('<div><div>content</div></div>',
181 '<div>\n' +
182 ' <div>content</div>\n' +
183 '</div>');
184 bth('<div>\n' +
185 ' <span>content</span>\n' +
186 '</div>');
187 bth('<div>\n' +
188 '</div>');
189 bth('<div>\n' +
190 ' content\n' +
191 '</div>');
192 bth('<div>\n' +
193 ' </div>',
194 '<div>\n' +
195 '</div>');
196 bth(' <div>\n' +
197 ' </div>',
198 '<div>\n' +
199 '</div>');
200 bth('<div>\n' +
201 '</div>\n' +
202 ' <div>\n' +
203 ' </div>',
204 '<div>\n' +
205 '</div>\n' +
206 '<div>\n' +
207 '</div>');
208 bth(' <div>\n' +
209 '</div>',
210 '<div>\n' +
211 '</div>');
212 bth('<div >content</div>',
213 '<div>content</div>');
214 bth('<div thinger="preserve space here" ></div >',
215 '<div thinger="preserve space here"></div>');
216 bth('content\n' +
217 ' <div>\n' +
218 ' </div>\n' +
219 'content',
220 'content\n' +
221 '<div>\n' +
222 '</div>\n' +
223 'content');
224 bth('<li>\n' +
225 ' <div>\n' +
226 ' </div>\n' +
227 '</li>');
228 bth('<li>\n' +
229 '<div>\n' +
230 '</div>\n' +
231 '</li>',
232 '<li>\n' +
233 ' <div>\n' +
234 ' </div>\n' +
235 '</li>');
236 bth('<li>\n' +
237 ' content\n' +
238 '</li>\n' +
239 '<li>\n' +
240 ' content\n' +
241 '</li>');
242  
243 bth('<img>content');
244 bth('<img> content');
245 bth('<img> content', '<img> content');
246  
247 bth('<img><img>content');
248 bth('<img> <img>content');
249 bth('<img> <img>content', '<img> <img>content');
250  
251 bth('<img><b>content</b>');
252 bth('<img> <b>content</b>');
253 bth('<img> <b>content</b>', '<img> <b>content</b>');
254  
255 bth('<div>content<img>content</div>');
256 bth('<div> content <img> content</div>');
257 bth('<div> content <img> content </div>',
258 '<div> content <img> content </div>');
259 bth('Text <a href="#">Link</a> Text');
260  
261 var unformatted = opts.unformatted;
262 opts.unformatted = ['script', 'style'];
263 bth('<script id="javascriptTemplate" type="text/x-kendo-template">\n' +
264 ' <ul>\n' +
265 ' # for (var i = 0; i < data.length; i++) { #\n' +
266 ' <li>#= data[i] #</li>\n' +
267 ' # } #\n' +
268 ' </ul>\n' +
269 '</script>');
270 bth('<style>\n' +
271 ' body {background-color:lightgrey}\n' +
272 ' h1 {color:blue}\n' +
273 '</style>');
274 opts.unformatted = unformatted;
275  
276 unformatted = opts.unformatted;
277 opts.unformatted = ['custom-element'];
278 test_fragment('<div>should <custom-element>not</custom-element>' +
279 ' insert newlines</div>',
280 '<div>should <custom-element>not</custom-element>' +
281 ' insert newlines</div>');
282 opts.unformatted = unformatted;
283  
284 // Tests that don't pass, but probably should.
285 // bth('<div><span>content</span></div>');
286  
287 // Handlebars tests
288 // Without the indent option on, handlebars are treated as content.
289  
290 opts.wrap_line_length = 0;
291 //...---------1---------2---------3---------4---------5---------6---------7
292 //...1234567890123456789012345678901234567890123456789012345678901234567890
293 bth('<div>Some text that should not wrap at all.</div>',
294 /* expected */
295 '<div>Some text that should not wrap at all.</div>');
296  
297 // A value of 0 means no max line length, and should not wrap.
298 //...---------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
299 //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
300 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>',
301 /* expected */
302 '<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>');
303  
304 opts.wrap_line_length = "0";
305 //...---------1---------2---------3---------4---------5---------6---------7
306 //...1234567890123456789012345678901234567890123456789012345678901234567890
307 bth('<div>Some text that should not wrap at all.</div>',
308 /* expected */
309 '<div>Some text that should not wrap at all.</div>');
310  
311 // A value of "0" means no max line length, and should not wrap
312 //...---------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
313 //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
314 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>',
315 /* expected */
316 '<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>');
317  
318 //BUGBUG: This should wrap before 40 not after.
319 opts.wrap_line_length = 40;
320 //...---------1---------2---------3---------4---------5---------6---------7
321 //...1234567890123456789012345678901234567890123456789012345678901234567890
322 bth('<div>Some test text that should wrap_inside_this section here.</div>',
323 /* expected */
324 '<div>Some test text that should wrap_inside_this\n' +
325 ' section here.</div>');
326  
327 opts.wrap_line_length = "40";
328 //...---------1---------2---------3---------4---------5---------6---------7
329 //...1234567890123456789012345678901234567890123456789012345678901234567890
330 bth('<div>Some test text that should wrap_inside_this section here.</div>',
331 /* expected */
332 '<div>Some test text that should wrap_inside_this\n' +
333 ' section here.</div>');
334  
335 opts.indent_size = 1;
336 opts.indent_char = '\t';
337 opts.preserve_newlines = false;
338 bth('<div>\n\tfoo\n</div>', '<div> foo </div>');
339  
340 opts.preserve_newlines = true;
341 bth('<div>\n\tfoo\n</div>');
342  
343  
344  
345 // test preserve_newlines and max_preserve_newlines
346 opts.preserve_newlines = false;
347 bth('<div>Should not</div>\n\n\n' +
348 '<div>preserve newlines</div>',
349 '<div>Should not</div>\n' +
350 '<div>preserve newlines</div>');
351  
352 opts.preserve_newlines = true;
353 opts.max_preserve_newlines = 0;
354 bth('<div>Should</div>\n\n\n' +
355 '<div>preserve zero newlines</div>',
356 '<div>Should</div>\n' +
357 '<div>preserve zero newlines</div>');
358  
359 opts.max_preserve_newlines = 1;
360 bth('<div>Should</div>\n\n\n' +
361 '<div>preserve one newline</div>',
362 '<div>Should</div>\n\n' +
363 '<div>preserve one newline</div>');
364  
365 opts.max_preserve_newlines = null;
366 bth('<div>Should</div>\n\n\n' +
367 '<div>preserve one newline</div>',
368 '<div>Should</div>\n\n\n' +
369 '<div>preserve one newline</div>');
370 }
371  
372 beautifier_tests();
373 beautifier_unconverted_tests();
374 }
375  
376 if (typeof exports !== "undefined") {
377 exports.run_html_tests = run_html_tests;
378 }