From c36213ab433dcee7cdb649c80a89c99e0c862f47 Mon Sep 17 00:00:00 2001 From: arai Date: Sun, 25 Dec 2016 18:34:22 +0900 Subject: [PATCH] Add content_unformatted config to avoid formatting content of an element (fix #906) --- README.md | 1 + js/lib/beautify-html.js | 7 ++++++- js/lib/cli.js | 3 +++ js/test/generated/beautify-html-tests.js | 7 +++++++ test/data/html/tests.js | 11 +++++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e3d4bcff9..bb5b582f7 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 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 ``` diff --git a/js/lib/beautify-html.js b/js/lib/beautify-html.js index 02ab9e66c..28fee7bff 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 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. @@ -118,6 +119,7 @@ wrap_line_length, brace_style, unformatted, + content_unformatted, preserve_newlines, max_preserve_newlines, indent_handlebars, @@ -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)) : @@ -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_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 47644d8f0..dd84dbcd6 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 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": diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index 2836c722e..254ae0f92 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -1027,6 +1027,13 @@ 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']; + test_fragment('

A

', '\n\n

A

\n \n \n\n\n'); + + //============================================================ // New Test Suite reset_options(); diff --git a/test/data/html/tests.js b/test/data/html/tests.js index b87600179..f9e1263f1 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -899,6 +899,17 @@ 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']" }, + ], + tests: [{ + fragment: true, + input: '

A

', + output: '\n\n

A

\n \n \n\n\n' + }] }, { name: "New Test Suite" }],