Skip to content

Commit

Permalink
Add content_unformatted config to avoid formatting content of an elem…
Browse files Browse the repository at this point in the history
…ent (fix beautifier#906)
  • Loading branch information
arai-a committed Dec 25, 2016
1 parent 17cb3d8 commit c36213a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ HTML Beautifier Options:
-A, --wrap-attributes Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline] ["auto"]
-i, --wrap-attributes-indent-size Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "force-aligned")
-U, --unformatted List of tags (defaults to inline) that should not be reformatted
-T, --content_unformatted List of tags (defaults to inline) that its 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
```
Expand Down
7 changes: 6 additions & 1 deletion js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "none"
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted
content_unformatted (defaults to empty array) - list of tags, that its content shouldn't be reformatted
indent_scripts (default normal) - "keep"|"separate"|"normal"
preserve_newlines (default true) - whether existing line breaks before elements should be preserved
Only works before elements, not inside tags or for text.
Expand Down Expand Up @@ -118,6 +119,7 @@
wrap_line_length,
brace_style,
unformatted,
content_unformatted,
preserve_newlines,
max_preserve_newlines,
indent_handlebars,
Expand Down Expand Up @@ -161,6 +163,7 @@
'acronym', 'address', 'big', 'dt', 'ins', 'strike', 'tt',
'pre',
];
content_unformatted = options.content_unformatted || [];
preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
max_preserve_newlines = preserve_newlines ?
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) :
Expand Down Expand Up @@ -580,7 +583,9 @@
this.indent_content = true;
this.traverse_whitespace();
}
} else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags
} else if (this.is_unformatted(tag_check, unformatted) ||
this.is_unformatted(tag_check, content_unformatted)) {
// do not reformat the "unformatted" or "content_unformatted" tags
comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function
content.push(comment);
tag_end = this.pos - 1;
Expand Down
3 changes: 3 additions & 0 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ var path = require('path'),
// HTML-only
"max_char": Number, // obsolete since 1.3.5
"unformatted": [String, Array],
"content_unformatted": [String, Array],
"indent_inner_html": [Boolean],
"indent_handlebars": [Boolean],
"indent_scripts": ["keep", "separate", "normal"],
Expand Down Expand Up @@ -139,6 +140,7 @@ var path = require('path'),
"i": ["--wrap_attributes_indent_size"],
"W": ["--max_char"], // obsolete since 1.3.5
"U": ["--unformatted"],
"T": ["--content_unformatted"],
"I": ["--indent_inner_html"],
"H": ["--indent_handlebars"],
"S": ["--indent_scripts"],
Expand Down Expand Up @@ -354,6 +356,7 @@ function usage(err) {
msg.push(' -p, --preserve-newlines Preserve line-breaks (--no-preserve-newlines disables)');
msg.push(' -m, --max-preserve-newlines Number of line-breaks to be preserved in one chunk [10]');
msg.push(' -U, --unformatted List of tags (defaults to inline) that should not be reformatted');
msg.push(' -T, --content_unformatted List of tags (defaults to inline) that its content should not be reformatted');
msg.push(' -E, --extra_liners List of tags (defaults to [head,body,/html] that should have an extra newline');
break;
case "css":
Expand Down
7 changes: 7 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,13 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
test_fragment('<html>\n\n<head>\n<meta>\n</head>\n\n</html>');


//============================================================
// content_unformatted to prevent formatting content
reset_options();
opts.content_unformatted = ['script', 'style'];
test_fragment('<html><body><h1>A</h1><script>if(1){f();}</script><style>.a{display:none;}</style></body></html>', '<html>\n<body>\n <h1>A</h1>\n <script>if(1){f();}</script>\n <style>.a{display:none;}</style>\n</body>\n\n</html>');


//============================================================
// New Test Suite
reset_options();
Expand Down
11 changes: 11 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,17 @@ exports.test_data = {
fragment: true,
unchanged: '<html>\n\n<head>\n<meta>\n</head>\n\n</html>'
}]
}, {
name: "content_unformatted to prevent formatting content",
description: "",
options: [
{ name: "content_unformatted", value: "['script', 'style']" },
],
tests: [{
fragment: true,
input: '<html><body><h1>A</h1><script>if(1){f();}</script><style>.a{display:none;}</style></body></html>',
output: '<html>\n<body>\n <h1>A</h1>\n <script>if(1){f();}</script>\n <style>.a{display:none;}</style>\n</body>\n\n</html>'
}]
}, {
name: "New Test Suite"
}],
Expand Down

0 comments on commit c36213a

Please sign in to comment.