diff --git a/README.md b/README.md index 3477220d8..d7d391cbd 100644 --- a/README.md +++ b/README.md @@ -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 pre) 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 ``` diff --git a/js/lib/beautify-html.js b/js/lib/beautify-html.js index 02ab9e66c..b12055836 100644 --- a/js/lib/beautify-html.js +++ b/js/lib/beautify-html.js @@ -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 pre tag) - 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. @@ -118,6 +119,7 @@ wrap_line_length, brace_style, unformatted, + content_unformatted, preserve_newlines, max_preserve_newlines, indent_handlebars, @@ -159,6 +161,8 @@ 'video', 'wbr', 'text', // prexisting - not sure of full effect of removing, leaving in 'acronym', 'address', 'big', 'dt', 'ins', 'strike', 'tt', + ]; + content_unformatted = options.content_unformatted || [ 'pre', ]; preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines; @@ -580,7 +584,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; diff --git a/js/lib/cli.js b/js/lib/cli.js index 8da37a3a8..1c7e49807 100755 --- a/js/lib/cli.js +++ b/js/lib/cli.js @@ -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"], @@ -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"], @@ -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 pre) 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": diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index 2836c722e..a7af0a59f 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -870,7 +870,13 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be //============================================================ // Unformatted tags reset_options(); - test_fragment('
c
c
c\n' + + '
c
Beautify me
But not me
', + 'Beautify me
\n' + + 'But not me
'); + test_fragment( + 'Beautify me
But not me
', + 'Beautify me
\n' + + 'But not me
'); + test_fragment('var a=1;\nvar b=a;
var a=1; var b=a;\n' + + '
\nvar a=1;\nvar b=a;\n
\n' + + ' var a=1; var b=a;\n' + + '\n' + + '
Beautify me
But not me
', + 'Beautify me
\n' + + '\n' + + '
But not me
\n' + + ''); + test_fragment( + 'Beautify me
But not me
', + 'Beautify me
\n' + + '\n' + + '
But not me
\n' + + ''); + test_fragment('var a=1;\nvar b=a;
var a=1;\n' + + 'var b=a;\n' + + '
\nvar a=1;\nvar b=a;\n
\n' + + 'var a=1;\n' + + 'var b=a;\n' + + '\n' + + '
c
c
c', + '
c
Beautify me
But not me
', + output: [ + 'Beautify me
', + 'But not me
' + ] + }, { + fragment: true, + input: 'Beautify me
But not me
', + output: [ + 'Beautify me
', + 'But not me
' + ] + }, { + fragment: true, + unchanged: 'var a=1;\nvar b=a;
var a=1; var b=a;', + '
\nvar a=1;\nvar b=a;\n
', + ' var a=1; var b=a;', + '', + '
Beautify me
But not me
', + output: [ + 'Beautify me
', + '', + '
But not me
', + '', + ] + }, { + fragment: true, + input: 'Beautify me
But not me
', + output: [ + 'Beautify me
', + '', + '
But not me
', + '' + ] + }, { + fragment: true, + unchanged: 'var a=1;\nvar b=a;
var a=1;', + 'var b=a;', + '
\nvar a=1;\nvar b=a;\n
', + 'var a=1;', + 'var b=a;', + '', + '