Skip to content

Commit

Permalink
Merge pull request #1563 from MacKLess/scripts
Browse files Browse the repository at this point in the history
Add tests for indent scripts option
  • Loading branch information
bitwiseman authored Sep 28, 2018
2 parents 234369d + 096a070 commit d98c27b
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ HTML Beautifier Options:
-T, --content_unformatted List of tags (defaults to pre) whose content should not be reformatted
-E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline before them.
--editorconfig Use EditorConfig to set up the options
--indent_scripts Sets indent level inside script tags ("normal", "keep", "separate")
```

## Directives to Ignore or Preserve sections (Javascript beautifier only)
Expand Down
110 changes: 110 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5942,6 +5942,116 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
'</body>');


//============================================================
// Tests script indent behavior - (indent_scripts = ""normal"")
reset_options();
set_name('Tests script indent behavior - (indent_scripts = ""normal"")');
opts.indent_scripts = 'normal';
test_fragment(
'<head>\n' +
'<script>\n' +
'if (a == b) {\n' +
'test();\n' +
'}\n' +
'</script>\n' +
'<style>\n' +
'.selector {\n' +
'font-size: 12px;\n' +
'}\n' +
'</style>\n' +
'</head>',
// -- output --
'<head>\n' +
' <script>\n' +
' if (a == b) {\n' +
' test();\n' +
' }\n' +
' </script>\n' +
' <style>\n' +
' .selector {\n' +
' font-size: 12px;\n' +
' }\n' +
' </style>\n' +
'</head>');
test_fragment(
'<body>\n' +
' <script src="one.js"></script> <!-- one -->\n' +
' <script src="two.js"></script> <!-- two-->\n' +
'</body>');

// Tests script indent behavior - (indent_scripts = ""keep"")
reset_options();
set_name('Tests script indent behavior - (indent_scripts = ""keep"")');
opts.indent_scripts = 'keep';
test_fragment(
'<head>\n' +
'<script>\n' +
'if (a == b) {\n' +
'test();\n' +
'}\n' +
'</script>\n' +
'<style>\n' +
'.selector {\n' +
'font-size: 12px;\n' +
'}\n' +
'</style>\n' +
'</head>',
// -- output --
'<head>\n' +
' <script>\n' +
' if (a == b) {\n' +
' test();\n' +
' }\n' +
' </script>\n' +
' <style>\n' +
' .selector {\n' +
' font-size: 12px;\n' +
' }\n' +
' </style>\n' +
'</head>');
test_fragment(
'<body>\n' +
' <script src="one.js"></script> <!-- one -->\n' +
' <script src="two.js"></script> <!-- two-->\n' +
'</body>');

// Tests script indent behavior - (indent_scripts = ""separate"")
reset_options();
set_name('Tests script indent behavior - (indent_scripts = ""separate"")');
opts.indent_scripts = 'separate';
test_fragment(
'<head>\n' +
'<script>\n' +
'if (a == b) {\n' +
'test();\n' +
'}\n' +
'</script>\n' +
'<style>\n' +
'.selector {\n' +
'font-size: 12px;\n' +
'}\n' +
'</style>\n' +
'</head>',
// -- output --
'<head>\n' +
' <script>\n' +
'if (a == b) {\n' +
' test();\n' +
'}\n' +
' </script>\n' +
' <style>\n' +
'.selector {\n' +
' font-size: 12px;\n' +
'}\n' +
' </style>\n' +
'</head>');
test_fragment(
'<body>\n' +
' <script src="one.js"></script> <!-- one -->\n' +
' <script src="two.js"></script> <!-- two-->\n' +
'</body>');


//============================================================
// underscore.js formatting
reset_options();
Expand Down
72 changes: 72 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,78 @@ exports.test_data = {
]
}
]
}, {
name: "Tests script indent behavior",
description: "Tests script indenting behavior",
matrix: [{
options: [
{ name: "indent_scripts", value: "'normal'" }
],
h: ' ',
c: ' ',
j: ' ',
hscript: ' '
},
{
options: [
{ name: "indent_scripts", value: "'keep'" }
],
h: ' ',
c: ' ',
j: ' ',
hscript: ' '
},
{
options: [
{ name: "indent_scripts", value: "'separate'" }
],
h: ' ',
c: ' ',
j: ' ',
hscript: ''
}
],
tests: [{
fragment: true,
input: [
'<head>',
'<script>',
'if (a == b) {',
'test();',
'}',
'</script>',
'<style>',
'.selector {',
'font-size: 12px;',
'}',
'</style>',
'</head>'
],
output: [
'<head>',
'{{h}}<script>',
'{{hscript}}if (a == b) {',
'{{hscript}}{{j}}test();',
'{{hscript}}}',
'{{h}}</script>',
'{{h}}<style>',
'{{hscript}}.selector {',
'{{hscript}}{{c}}font-size: 12px;',
'{{hscript}}}',
'{{h}}</style>',
'</head>'
]
},
{
fragment: true,
unchanged: [
'<body>',
'{{h}}<script src="one.js"></script> <!-- one -->',
'{{h}}<script src="two.js"></script> <!-- two-->',
'</body>'
]
}
]
}, {
name: "underscore.js formatting",
description: "underscore.js templates (<% ... %>) treated as comments.",
Expand Down

0 comments on commit d98c27b

Please sign in to comment.