From ec7b247f8b1be47bcf03b95ab7ab8b33bf9fcdbe Mon Sep 17 00:00:00 2001 From: Jai Chandra Date: Mon, 9 May 2016 15:30:11 -0400 Subject: [PATCH] Fixes https://github.com/beautify-web/js-beautify/issues/930 Added tests --- js/lib/beautify-html.js | 9 +- js/test/generated/beautify-html-tests.js | 110 +++++++++++++++++++++++ test/data/html/tests.js | 10 +++ 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/js/lib/beautify-html.js b/js/lib/beautify-html.js index eaa047583..c21b813e7 100644 --- a/js/lib/beautify-html.js +++ b/js/lib/beautify-html.js @@ -595,9 +595,14 @@ } else if (comment.indexOf(''; matched = true; - } else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment - delimiter = '}}'; + } else if (comment.indexOf('{{!--') === 0) { // {{!-- handlebars comment + delimiter = '--}}'; matched = true; + } else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment + if (comment.length === 5 && comment.indexOf('{{!--') === -1) { + delimiter = '}}'; + matched = true; + } } else if (comment.indexOf(''; matched = true; diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index a31e478b9..24f41f753 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -464,6 +464,61 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be test_fragment('
'); test_fragment('
'); + // Handlebars Indenting On - (content = "{{!-- comment--}}") + opts.indent_handlebars = true; + test_fragment('{{page-title}}'); + test_fragment('{{#if 0}}{{/if}}'); + test_fragment('{{#if 0}}{{!-- comment--}}{{/if}}'); + test_fragment('{{#if 0}}\n{{/if}}'); + test_fragment( + '{{#if words}}{{/if}}', + '{{#if words}}{{/if}}'); + test_fragment( + '{{#if words}}{{!-- comment--}}{{/if}}', + '{{#if words}}{{!-- comment--}}{{/if}}'); + test_fragment( + '{{#if words}}{{!-- comment--}}{{/if}}', + '{{#if words}}{{!-- comment--}}{{/if}}'); + test_fragment('{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment( + '{{#if 1}}\n
\n
\n{{/if}}', + '{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment('
\n {{#if 1}}\n {{/if}}\n
'); + test_fragment( + '
\n{{#if 1}}\n{{/if}}\n
', + '
\n {{#if 1}}\n {{/if}}\n
'); + test_fragment( + '{{#if}}\n{{#each}}\n{{#if}}\n{{!-- comment--}}\n{{/if}}\n{{#if}}\n{{!-- comment--}}\n{{/if}}\n{{/each}}\n{{/if}}', + '{{#if}}\n {{#each}}\n {{#if}}\n {{!-- comment--}}\n {{/if}}\n {{#if}}\n {{!-- comment--}}\n {{/if}}\n {{/each}}\n{{/if}}'); + test_fragment('{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment( + '{{#if 1}}\n {{!-- comment--}}\n {{else}}\n {{!-- comment--}}\n{{/if}}', + '{{#if 1}}\n {{!-- comment--}}\n{{else}}\n {{!-- comment--}}\n{{/if}}'); + test_fragment( + '{{#if 1}}\n {{else}}\n {{/if}}', + '{{#if 1}}\n{{else}}\n{{/if}}'); + test_fragment( + '{{#if thing}}\n{{#if otherthing}}\n {{!-- comment--}}\n {{else}}\n{{!-- comment--}}\n {{/if}}\n {{else}}\n{{!-- comment--}}\n{{/if}}', + '{{#if thing}}\n {{#if otherthing}}\n {{!-- comment--}}\n {{else}}\n {{!-- comment--}}\n {{/if}}\n{{else}}\n {{!-- comment--}}\n{{/if}}'); + test_fragment( + '', + '
'); + test_fragment( + '{{!-- comment--}}', + '
{{!-- comment--}}
'); + test_fragment( + '{{!-- comment--}}', + '
{{!-- comment--}}
'); + test_fragment( + '{{!-- comment--}}', + '{{!-- comment--}}'); + test_fragment('
{{!-- comment--}}
'); + test_fragment('
{{!-- comment--}}
'); + test_fragment('
'); + test_fragment('
'); + test_fragment('
'); + test_fragment('
'); + // Handlebars Indenting On - (content = "{pre{{field1}} {{field2}} {{field3}}post") opts.indent_handlebars = true; test_fragment('{{page-title}}'); @@ -574,6 +629,61 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be test_fragment('
'); test_fragment('
'); + // Handlebars Indenting On - (content = "{{!-- \n mult-line\ncomment \n with spacing\n--}}") + opts.indent_handlebars = true; + test_fragment('{{page-title}}'); + test_fragment('{{#if 0}}{{/if}}'); + test_fragment('{{#if 0}}{{!-- \n mult-line\ncomment \n with spacing\n--}}{{/if}}'); + test_fragment('{{#if 0}}\n{{/if}}'); + test_fragment( + '{{#if words}}{{/if}}', + '{{#if words}}{{/if}}'); + test_fragment( + '{{#if words}}{{!-- \n mult-line\ncomment \n with spacing\n--}}{{/if}}', + '{{#if words}}{{!-- \n mult-line\ncomment \n with spacing\n--}}{{/if}}'); + test_fragment( + '{{#if words}}{{!-- \n mult-line\ncomment \n with spacing\n--}}{{/if}}', + '{{#if words}}{{!-- \n mult-line\ncomment \n with spacing\n--}}{{/if}}'); + test_fragment('{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment( + '{{#if 1}}\n
\n
\n{{/if}}', + '{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment('
\n {{#if 1}}\n {{/if}}\n
'); + test_fragment( + '
\n{{#if 1}}\n{{/if}}\n
', + '
\n {{#if 1}}\n {{/if}}\n
'); + test_fragment( + '{{#if}}\n{{#each}}\n{{#if}}\n{{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}\n{{#if}}\n{{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}\n{{/each}}\n{{/if}}', + '{{#if}}\n {{#each}}\n {{#if}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{/if}}\n {{#if}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{/if}}\n {{/each}}\n{{/if}}'); + test_fragment('{{#if 1}}\n
\n
\n{{/if}}'); + test_fragment( + '{{#if 1}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{else}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}', + '{{#if 1}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{else}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}'); + test_fragment( + '{{#if 1}}\n {{else}}\n {{/if}}', + '{{#if 1}}\n{{else}}\n{{/if}}'); + test_fragment( + '{{#if thing}}\n{{#if otherthing}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{else}}\n{{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{/if}}\n {{else}}\n{{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}', + '{{#if thing}}\n {{#if otherthing}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{else}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n {{/if}}\n{{else}}\n {{!-- \n mult-line\ncomment \n with spacing\n--}}\n{{/if}}'); + test_fragment( + '', + '
'); + test_fragment( + '{{!-- \n mult-line\ncomment \n with spacing\n--}}', + '
{{!-- \n mult-line\ncomment \n with spacing\n--}}
'); + test_fragment( + '{{!-- \n mult-line\ncomment \n with spacing\n--}}', + '
{{!-- \n mult-line\ncomment \n with spacing\n--}}
'); + test_fragment( + '{{!-- \n mult-line\ncomment \n with spacing\n--}}', + '{{!-- \n mult-line\ncomment \n with spacing\n--}}'); + test_fragment('
{{!-- \n mult-line\ncomment \n with spacing\n--}}
'); + test_fragment('
{{!-- \n mult-line\ncomment \n with spacing\n--}}
'); + test_fragment('
'); + test_fragment('
'); + test_fragment('
'); + test_fragment('
'); + reset_options(); //============================================================ diff --git a/test/data/html/tests.js b/test/data/html/tests.js index ea2114c7a..f1b36ea2f 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -389,6 +389,11 @@ exports.test_data = { { name: "indent_handlebars", value: "true" } ], content: '{{! comment}}' + }, { + options: [ + { name: "indent_handlebars", value: "true" } + ], + content: '{{!-- comment--}}' }, { options: [ { name: "indent_handlebars", value: "true" } @@ -399,6 +404,11 @@ exports.test_data = { { name: "indent_handlebars", value: "true" } ], content: '{{! \\n mult-line\\ncomment \\n with spacing\\n}}' + }, { + options: [ + { name: "indent_handlebars", value: "true" } + ], + content: '{{!-- \\n mult-line\\ncomment \\n with spacing\\n--}}' }], tests: [ { fragment: true, unchanged: '{{page-title}}' },