Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for indent scripts option #1563

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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