node-html.mustache 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*global js_beautify: true */
  2. function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_beautify)
  3. {
  4. var opts = {
  5. indent_size: 4,
  6. indent_char: ' ',
  7. preserve_newlines: true,
  8. jslint_happy: false,
  9. keep_array_indentation: false,
  10. brace_style: 'collapse',
  11. space_before_conditional: true,
  12. break_chained_methods: false,
  13. selector_separator: '\n',
  14. end_with_newline: false
  15. };
  16. function test_html_beautifier(input)
  17. {
  18. return html_beautify(input, opts);
  19. }
  20. var sanitytest;
  21. // test the input on beautifier with the current flag settings
  22. // does not check the indentation / surroundings as bt() does
  23. function test_fragment(input, expected)
  24. {
  25. expected = expected || expected === '' ? expected : input;
  26. sanitytest.expect(input, expected);
  27. // if the expected is different from input, run it again
  28. // expected output should be unchanged when run twice.
  29. if (expected !== input) {
  30. sanitytest.expect(expected, expected);
  31. }
  32. // Everywhere we do newlines, they should be replaced with opts.eol
  33. opts.eol = '\r\n';
  34. expected = expected.replace(/[\n]/g, '\r\n');
  35. sanitytest.expect(input, expected);
  36. input = input.replace(/[\n]/g, '\r\n');
  37. sanitytest.expect(input, expected);
  38. opts.eol = '\n';
  39. }
  40. // test html
  41. function bth(input, expectation)
  42. {
  43. var wrapped_input, wrapped_expectation, field_input, field_expectation;
  44. expectation = expectation || expectation === '' ? expectation : input;
  45. sanitytest.test_function(test_html_beautifier, 'html_beautify');
  46. test_fragment(input, expectation);
  47. if (opts.indent_size === 4 && input) {
  48. wrapped_input = '<div>\n' + input.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
  49. wrapped_expectation = '<div>\n' + expectation.replace(/^(.+)$/mg, ' $1') + '\n <span>inline</span>\n</div>';
  50. test_fragment(wrapped_input, wrapped_expectation);
  51. }
  52. }
  53. function unicode_char(value) {
  54. return String.fromCharCode(value)
  55. }
  56. function beautifier_tests()
  57. {
  58. sanitytest = test_obj;
  59. bth('');
  60. {{#default_options}} opts.{{name}} = {{&value}};
  61. {{/default_options}}
  62. {{#groups}}{{#set_mustache_tags}}.{{/set_mustache_tags}}{{^matrix}}
  63. // {{&name}}
  64. {{#options}}
  65. opts.{{name}} = {{&value}};
  66. {{/options}}
  67. {{#tests}}
  68. {{#test_line}}.{{/test_line}};
  69. {{/tests}}
  70. {{/matrix}}{{#matrix}}
  71. // {{&name}} - ({{#matrix_context_string}}.{{/matrix_context_string}})
  72. {{#options}}
  73. opts.{{name}} = {{&value}};
  74. {{/options}}
  75. {{#tests}}
  76. {{#test_line}}.{{/test_line}};
  77. {{/tests}}
  78. {{/matrix}}{{#unset_mustache_tags}}.{{/unset_mustache_tags}}
  79. {{/groups}}
  80. opts.end_with_newline = true;
  81. test_fragment('', '\n');
  82. test_fragment('<div></div>\n');
  83. test_fragment('<div></div>\n\n\n', '<div></div>\n');
  84. test_fragment('<head>\n' +
  85. ' <script>\n' +
  86. ' mocha.setup("bdd");\n' +
  87. '\n' +
  88. ' </script>\n' +
  89. '</head>\n');
  90. opts.end_with_newline = false;
  91. // error cases need love too
  92. bth('<img title="Bad food!" src="foo.jpg" alt="Evil" ">');
  93. bth("<!-- don't blow up if a comment is not complete"); // -->
  94. test_fragment(
  95. '<head>\n' +
  96. ' <script>\n' +
  97. ' mocha.setup("bdd");\n' +
  98. ' </script>\n' +
  99. '</head>');
  100. test_fragment('<div></div>\n', '<div></div>');
  101. bth('<div></div>');
  102. bth('<div>content</div>');
  103. bth('<div><div></div></div>',
  104. '<div>\n' +
  105. ' <div></div>\n' +
  106. '</div>');
  107. bth('<div><div>content</div></div>',
  108. '<div>\n' +
  109. ' <div>content</div>\n' +
  110. '</div>');
  111. bth('<div>\n' +
  112. ' <span>content</span>\n' +
  113. '</div>');
  114. bth('<div>\n' +
  115. '</div>');
  116. bth('<div>\n' +
  117. ' content\n' +
  118. '</div>');
  119. bth('<div>\n' +
  120. ' </div>',
  121. '<div>\n' +
  122. '</div>');
  123. bth(' <div>\n' +
  124. ' </div>',
  125. '<div>\n' +
  126. '</div>');
  127. bth('<div>\n' +
  128. '</div>\n' +
  129. ' <div>\n' +
  130. ' </div>',
  131. '<div>\n' +
  132. '</div>\n' +
  133. '<div>\n' +
  134. '</div>');
  135. bth(' <div>\n' +
  136. '</div>',
  137. '<div>\n' +
  138. '</div>');
  139. bth('<div >content</div>',
  140. '<div>content</div>');
  141. bth('<div thinger="preserve space here" ></div >',
  142. '<div thinger="preserve space here"></div>');
  143. bth('content\n' +
  144. ' <div>\n' +
  145. ' </div>\n' +
  146. 'content',
  147. 'content\n' +
  148. '<div>\n' +
  149. '</div>\n' +
  150. 'content');
  151. bth('<li>\n' +
  152. ' <div>\n' +
  153. ' </div>\n' +
  154. '</li>');
  155. bth('<li>\n' +
  156. '<div>\n' +
  157. '</div>\n' +
  158. '</li>',
  159. '<li>\n' +
  160. ' <div>\n' +
  161. ' </div>\n' +
  162. '</li>');
  163. bth('<li>\n' +
  164. ' content\n' +
  165. '</li>\n' +
  166. '<li>\n' +
  167. ' content\n' +
  168. '</li>');
  169. bth('<img>content');
  170. bth('<img> content');
  171. bth('<img> content', '<img> content');
  172. bth('<img><img>content');
  173. bth('<img> <img>content');
  174. bth('<img> <img>content', '<img> <img>content');
  175. bth('<img><b>content</b>');
  176. bth('<img> <b>content</b>');
  177. bth('<img> <b>content</b>', '<img> <b>content</b>');
  178. bth('<div>content<img>content</div>');
  179. bth('<div> content <img> content</div>');
  180. bth('<div> content <img> content </div>',
  181. '<div> content <img> content </div>');
  182. bth('Text <a href="#">Link</a> Text');
  183. // START tests for issue 453
  184. bth('<script type="text/unknown"><div></div></script>',
  185. '<script type="text/unknown">\n' +
  186. ' <div></div>\n' +
  187. '</script>');
  188. bth('<script type="text/javascript"><div></div></script>',
  189. '<script type="text/javascript">\n' +
  190. ' < div > < /div>\n' +
  191. '</script>');
  192. bth('<script><div></div></script>',
  193. '<script>\n' +
  194. ' < div > < /div>\n' +
  195. '</script>');
  196. bth('<script type="text/javascript">var foo = "bar";</script>',
  197. '<script type="text/javascript">\n' +
  198. ' var foo = "bar";\n' +
  199. '</script>');
  200. bth('<script type="application/javascript">var foo = "bar";</script>',
  201. '<script type="application/javascript">\n' +
  202. ' var foo = "bar";\n' +
  203. '</script>');
  204. bth('<script type="application/javascript;version=1.8">var foo = "bar";</script>',
  205. '<script type="application/javascript;version=1.8">\n' +
  206. ' var foo = "bar";\n' +
  207. '</script>');
  208. bth('<script type="application/x-javascript">var foo = "bar";</script>',
  209. '<script type="application/x-javascript">\n' +
  210. ' var foo = "bar";\n' +
  211. '</script>');
  212. bth('<script type="application/ecmascript">var foo = "bar";</script>',
  213. '<script type="application/ecmascript">\n' +
  214. ' var foo = "bar";\n' +
  215. '</script>');
  216. bth('<script type="text/javascript1.5">var foo = "bar";</script>',
  217. '<script type="text/javascript1.5">\n' +
  218. ' var foo = "bar";\n' +
  219. '</script>');
  220. bth('<script>var foo = "bar";</script>',
  221. '<script>\n' +
  222. ' var foo = "bar";\n' +
  223. '</script>');
  224. bth('<style type="text/unknown"><tag></tag></style>',
  225. '<style type="text/unknown">\n' +
  226. ' <tag></tag>\n' +
  227. '</style>');
  228. bth('<style type="text/css"><tag></tag></style>',
  229. '<style type="text/css">\n' +
  230. ' <tag></tag>\n' +
  231. '</style>');
  232. bth('<style><tag></tag></style>',
  233. '<style>\n' +
  234. ' <tag></tag>\n' +
  235. '</style>');
  236. bth('<style type="text/css">.selector {font-size:12px;}</style>',
  237. '<style type="text/css">\n' +
  238. ' .selector {\n' +
  239. ' font-size: 12px;\n' +
  240. ' }\n'+
  241. '</style>');
  242. bth('<style>.selector {font-size:12px;}</style>',
  243. '<style>\n' +
  244. ' .selector {\n' +
  245. ' font-size: 12px;\n' +
  246. ' }\n'+
  247. '</style>');
  248. // END tests for issue 453
  249. var unformatted = opts.unformatted;
  250. opts.unformatted = ['script', 'style'];
  251. bth('<script id="javascriptTemplate" type="text/x-kendo-template">\n' +
  252. ' <ul>\n' +
  253. ' # for (var i = 0; i < data.length; i++) { #\n' +
  254. ' <li>#= data[i] #</li>\n' +
  255. ' # } #\n' +
  256. ' </ul>\n' +
  257. '</script>');
  258. bth('<style>\n' +
  259. ' body {background-color:lightgrey}\n' +
  260. ' h1 {color:blue}\n' +
  261. '</style>');
  262. opts.unformatted = unformatted;
  263. unformatted = opts.unformatted;
  264. opts.unformatted = ['custom-element'];
  265. test_fragment('<div>should <custom-element>not</custom-element>' +
  266. ' insert newlines</div>',
  267. '<div>should <custom-element>not</custom-element>' +
  268. ' insert newlines</div>');
  269. opts.unformatted = unformatted;
  270. // Tests that don't pass, but probably should.
  271. // bth('<div><span>content</span></div>');
  272. // Handlebars tests
  273. // Without the indent option on, handlebars are treated as content.
  274. opts.wrap_line_length = 0;
  275. //...---------1---------2---------3---------4---------5---------6---------7
  276. //...1234567890123456789012345678901234567890123456789012345678901234567890
  277. bth('<div>Some text that should not wrap at all.</div>',
  278. /* expected */
  279. '<div>Some text that should not wrap at all.</div>');
  280. // A value of 0 means no max line length, and should not wrap.
  281. //...---------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
  282. //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
  283. 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>',
  284. /* expected */
  285. '<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>');
  286. opts.wrap_line_length = "0";
  287. //...---------1---------2---------3---------4---------5---------6---------7
  288. //...1234567890123456789012345678901234567890123456789012345678901234567890
  289. bth('<div>Some text that should not wrap at all.</div>',
  290. /* expected */
  291. '<div>Some text that should not wrap at all.</div>');
  292. // A value of "0" means no max line length, and should not wrap
  293. //...---------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
  294. //...12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
  295. 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>',
  296. /* expected */
  297. '<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>');
  298. //BUGBUG: This should wrap before 40 not after.
  299. opts.wrap_line_length = 40;
  300. //...---------1---------2---------3---------4---------5---------6---------7
  301. //...1234567890123456789012345678901234567890123456789012345678901234567890
  302. bth('<div>Some test text that should wrap_inside_this section here.</div>',
  303. /* expected */
  304. '<div>Some test text that should wrap_inside_this\n' +
  305. ' section here.</div>');
  306. opts.wrap_line_length = "40";
  307. //...---------1---------2---------3---------4---------5---------6---------7
  308. //...1234567890123456789012345678901234567890123456789012345678901234567890
  309. bth('<div>Some test text that should wrap_inside_this section here.</div>',
  310. /* expected */
  311. '<div>Some test text that should wrap_inside_this\n' +
  312. ' section here.</div>');
  313. opts.indent_size = 1;
  314. opts.indent_char = '\t';
  315. opts.preserve_newlines = false;
  316. bth('<div>\n\tfoo\n</div>', '<div> foo </div>');
  317. opts.preserve_newlines = true;
  318. bth('<div>\n\tfoo\n</div>');
  319. // test preserve_newlines and max_preserve_newlines
  320. opts.preserve_newlines = false;
  321. bth('<div>Should not</div>\n\n\n' +
  322. '<div>preserve newlines</div>',
  323. '<div>Should not</div>\n' +
  324. '<div>preserve newlines</div>');
  325. opts.preserve_newlines = true;
  326. opts.max_preserve_newlines = 0;
  327. bth('<div>Should</div>\n\n\n' +
  328. '<div>preserve zero newlines</div>',
  329. '<div>Should</div>\n' +
  330. '<div>preserve zero newlines</div>');
  331. opts.max_preserve_newlines = 1;
  332. bth('<div>Should</div>\n\n\n' +
  333. '<div>preserve one newline</div>',
  334. '<div>Should</div>\n\n' +
  335. '<div>preserve one newline</div>');
  336. opts.max_preserve_newlines = null;
  337. bth('<div>Should</div>\n\n\n' +
  338. '<div>preserve one newline</div>',
  339. '<div>Should</div>\n\n\n' +
  340. '<div>preserve one newline</div>');
  341. }
  342. beautifier_tests();
  343. }
  344. if (typeof exports !== "undefined") {
  345. exports.run_html_tests = run_html_tests;
  346. }