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_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('
    \n
  1. b
    c
  2. \n
'); + test_fragment( + '
    \n
  1. b
    c
  2. \n
', + '
    \n' + + '
  1. b\n' + + '
    c
    \n' + + '
  2. \n' + + '
'); test_fragment('
    \n
  1. bc
  2. \n
'); test_fragment(''); test_fragment('
'); @@ -1027,6 +1033,105 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be test_fragment('\n\n\n\n\n\n'); + //============================================================ + // content_unformatted to prevent formatting content + reset_options(); + opts.content_unformatted = ['script', 'style', 'p', 'span', 'br']; + test_fragment( + '

A

', + '\n' + + '\n' + + '

A

\n' + + ' \n' + + ' \n' + + '\n' + + '\n' + + ''); + test_fragment( + '

Beautify me

But not me

', + '
\n' + + '

Beautify me

\n' + + '
\n' + + '

But not me

'); + test_fragment( + '
Beautify me

But not me

', + '
\n' + + '

Beautify me

\n' + + '
\n' + + '

But not me

'); + test_fragment('
blabla
something here
'); + test_fragment('

'); + test_fragment( + '
var a=1;\nvar b=a;
', + '
\n' + + '
var a=1; var b=a;
\n' + + '
'); + test_fragment( + '
\nvar a=1;\nvar b=a;\n
', + '
\n' + + '
\n' +
+            '        var a=1; var b=a;\n' +
+            '    
\n' + + '
'); + + + //============================================================ + // default content_unformatted + reset_options(); + test_fragment( + '

A

', + '\n' + + '\n' + + '

A

\n' + + ' \n' + + ' \n' + + '\n' + + '\n' + + ''); + test_fragment( + '

Beautify me

But not me

', + '
\n' + + '

Beautify me

\n' + + '
\n' + + '

\n' + + '

But not me

\n' + + '

'); + test_fragment( + '
Beautify me

But not me

', + '
\n' + + '

Beautify me

\n' + + '
\n' + + '

\n' + + '

But not me

\n' + + '

'); + test_fragment('
blabla
something here
'); + test_fragment('

'); + test_fragment( + '
var a=1;\nvar b=a;
', + '
\n' + + '
var a=1;\n' +
+            'var b=a;
\n' + + '
'); + test_fragment( + '
\nvar a=1;\nvar b=a;\n
', + '
\n' + + '
\n' +
+            'var a=1;\n' +
+            'var b=a;\n' +
+            '
\n' + + '
'); + + //============================================================ // New Test Suite reset_options(); diff --git a/test/data/html/tests.js b/test/data/html/tests.js index b87600179..0e35cad4c 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -707,8 +707,17 @@ exports.test_data = { name: "Unformatted tags", description: "Unformatted tag behavior", options: [], - tests: [ - { fragment: true, unchanged: '
    \n
  1. b
    c
  2. \n
' }, + tests: [{ + fragment: true, + input: '
    \n
  1. b
    c
  2. \n
', + output: [ + '
    ', + '
  1. b', + '
    c
    ', + '
  2. ', + '
' + ] + }, { fragment: true, unchanged: '
    \n
  1. bc
  2. \n
' }, { fragment: true, unchanged: '' }, { fragment: true, unchanged: '
' }, @@ -899,6 +908,144 @@ exports.test_data = { fragment: true, unchanged: '\n\n\n\n\n\n' }] + }, { + name: "content_unformatted to prevent formatting content", + description: "", + options: [ + { name: 'content_unformatted', value: "['script', 'style', 'p', 'span', 'br']" } + ], + tests: [{ + fragment: true, + input: '

A

', + output: [ + '', + '', + '

A

', + ' ', + ' ', + '', + '', + '' + ] + }, { + fragment: true, + input: '

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: '
blabla
something here
' + }, { + fragment: true, + unchanged: '

' + }, { + fragment: true, + input: '
var a=1;\nvar b=a;
', + output: [ + '
', + '
var a=1; var b=a;
', + '
' + ] + }, { + fragment: true, + input: '
\nvar a=1;\nvar b=a;\n
', + output: [ + '
', + '
',
+                '        var a=1; var b=a;',
+                '    
', + '
' + ] + }] + }, { + name: "default content_unformatted", + description: "", + options: [], + tests: [{ + fragment: true, + input: '

A

', + output: [ + '', + '', + '

A

', + ' ', + ' ', + '', + '', + '' + ] + }, { + fragment: true, + input: '

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: '
blabla
something here
' + }, { + fragment: true, + unchanged: '

' + }, { + fragment: true, + input: '
var a=1;\nvar b=a;
', + output: [ + '
', + '
var a=1;',
+                'var b=a;
', + '
' + ] + }, { + fragment: true, + input: '
\nvar a=1;\nvar b=a;\n
', + output: [ + '
', + '
',
+                'var a=1;',
+                'var b=a;',
+                '
', + '
' + ] + }] }, { name: "New Test Suite" }],