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

fix #906. New unformatted_tag_content option. #1081

Closed
wants to merge 3 commits into from
Closed
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
67 changes: 66 additions & 1 deletion js/lib/beautify-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
wrap_line_length,
brace_style,
unformatted,
unformatted_tag_content,
preserve_newlines,
max_preserve_newlines,
indent_handlebars,
Expand Down Expand Up @@ -161,6 +162,7 @@
'acronym', 'address', 'big', 'dt', 'ins', 'strike', 'tt',
'pre',
];
unformatted_tag_content = options.unformatted_tag_content || [];
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 @@ -287,6 +289,27 @@
}
};

this.get_unformatted_content = function() {
var target = this.last_text.match(/<([^>\s]+)/)[1],
end_tag = '</' + target + '>',

m_start = new RegExp('<\s*' + target + '[^>]*>', "gi"),
m_end = new RegExp('</\s*' + target + '[^>]*>', "gi");

var res = '';

// can be subtags inside unformatted flow
while ((res.match(m_start) || []).length + 1 !== (res.match(m_end) || []).length) {
res += this.get_unformatted(end_tag, this.last_text);
}

//remove closing target tag.
res = res.replace(new RegExp(end_tag + '$'), '');
this.pos -= end_tag.length;

return res;
};

this.get_content = function() { //function to capture regular content between tags
var input_char = '',
content = [],
Expand Down Expand Up @@ -582,9 +605,33 @@
}
} else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags
comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function

content.push(comment);
tag_end = this.pos - 1;
this.tag_type = 'SINGLE';
} else if (this.is_unformatted_tag_content(tag_check, unformatted_tag_content)) {
if (tag_check.charAt(0) === '/') { //this tag is a double tag so check for tag-ending
this.retrieve_tag(tag_check.substring(1)); //remove it and all ancestors
this.tag_type = 'END';
} else { //otherwise it's a start-tag
this.record_tag(tag_check); //push it on the tag stack
if (tag_check.toLowerCase() !== 'html') {
this.indent_content = true;
}
this.tag_type = 'UNFORMATTED';
}

// Allow preserving of newlines after a start or end tag
if (this.traverse_whitespace()) {
this.space_or_wrap(content);
}

if (this.Utils.in_array(tag_check, this.Utils.extra_liners)) { //check if this double needs an extra line
this.print_newline(false, this.output);
if (this.output.length && this.output[this.output.length - 2] !== '\n') {
this.print_newline(true, this.output);
}
}
} else if (tag_check === 'script' &&
(tag_complete.search('type') === -1 ||
(tag_complete.search('type') > -1 &&
Expand Down Expand Up @@ -776,6 +823,16 @@
}
return [token, 'TK_' + type];
}

if (this.last_token === 'TK_TAG_UNFORMATTED') {
token = this.get_unformatted_content();

if (typeof token !== 'string') {
return token;
}
return [token, 'TK_CONTENT'];
}

if (this.current_mode === 'CONTENT') {
token = this.get_content();
if (typeof token !== 'string') {
Expand Down Expand Up @@ -832,6 +889,10 @@
}
};

this.is_unformatted_tag_content = function(tag_check, unformatted_content_tags) {
return this.is_unformatted(tag_check, unformatted_content_tags);
};

this.printer = function(js_source, indent_character, indent_size, wrap_line_length, brace_style) { //handles input/output and some other printing functions

this.input = js_source || ''; //gets the input for the Parser
Expand Down Expand Up @@ -937,6 +998,7 @@
}

switch (multi_parser.token_type) {
case 'TK_TAG_UNFORMATTED':
case 'TK_TAG_START':
multi_parser.print_newline(false, multi_parser.output);
multi_parser.print_token(multi_parser.token_text);
Expand Down Expand Up @@ -966,7 +1028,9 @@
tag_extracted_from_last_output = multi_parser.output[multi_parser.output.length - 1].match(/(?:<|{{#)\s*(\w+)/);
}
if (tag_extracted_from_last_output === null ||
(tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted))) {
(tag_extracted_from_last_output[1] !== tag_name && !multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted) &&
!multi_parser.Utils.in_array(tag_extracted_from_last_output[1], unformatted_tag_content))
) {
multi_parser.print_newline(false, multi_parser.output);
}
}
Expand Down Expand Up @@ -1101,6 +1165,7 @@
var js_beautify = require('./beautify.js');
var css_beautify = require('./beautify-css.js');


exports.html_beautify = function(html_source, options) {
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
};
Expand Down
9 changes: 6 additions & 3 deletions js/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var path = require('path'),
"space_around_selector_separator": Boolean,
// HTML-only
"max_char": Number, // obsolete since 1.3.5
"unformatted_tag_content": [String, Array],
"unformatted": [String, Array],
"indent_inner_html": [Boolean],
"indent_handlebars": [Boolean],
Expand Down Expand Up @@ -143,6 +144,7 @@ var path = require('path'),
"H": ["--indent_handlebars"],
"S": ["--indent_scripts"],
"E": ["--extra_liners"],
"utc": ["--unformatted_tag_content"],
// non-dasherized hybrid shortcuts
"good-stuff": [
"--keep_array_indentation",
Expand All @@ -158,9 +160,9 @@ var path = require('path'),
"f": ["--files"],
"o": ["--outfile"],
"r": ["--replace"],
"q": ["--quiet"]
// no shorthand for "config"
// no shorthand for "editorconfig"
"q": ["--quiet"],
// no shorthand for "config"
// no shorthand for "editorconfig"
});

function verifyExists(fullPath) {
Expand Down Expand Up @@ -414,6 +416,7 @@ function processInputSync(filepath) {
}

function makePretty(code, config, outfile, callback) {

try {
var fileType = getOutputType(outfile, config.type);
var pretty = beautify[fileType](code, config);
Expand Down
11 changes: 11 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,17 @@ 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>');


//============================================================
// Unformatted script/style with extra script, style and body liners
reset_options();
opts.unformatted_tag_content = [ 'script', 'style', 'p', 'span', 'br' ];
test_fragment('<body><h1>Heading</h1><script>var formatMe = function() { return false; };</script><style>.format-disabled { display: none; } </style></body>', '<body>\n <h1>Heading</h1>\n <script>var formatMe = function() { return false; };</script>\n <style>.format-disabled { display: none; } </style>\n</body>');
test_fragment('<div><p>Beautify me</p></div><p><p>But not me</p></p>', '<div>\n <p>Beautify me</p>\n</div>\n<p><p>But not me</p></p>');
test_fragment('<div><p\n class="beauty-me"\n>Beautify me</p></div><p><p\n class="iamalreadybeauty"\n>But not me</p></p>', '<div>\n <p class="beauty-me">Beautify me</p>\n</div>\n<p><p\n class="iamalreadybeauty"\n>But not me</p></p>');
test_fragment('<div><span>blabla<div>something here</div></span></div>');
test_fragment('<div><br /></div>');


//============================================================
// New Test Suite
reset_options();
Expand Down
36 changes: 36 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,42 @@ exports.test_data = {
fragment: true,
unchanged: '<html>\n\n<head>\n<meta>\n</head>\n\n</html>'
}]
}, {
name: "Unformatted script/style with extra script, style and body liners",
options: [
{ name: 'unformatted_tag_content', value: "[ 'script', 'style', 'p', 'span', 'br' ]" }
],
tests: [{
fragment: true,
input: "<body><h1>Heading</h1><script>var formatMe = function() { return false; };</script><style>.format-disabled { display: none; } </style></body>",
output: "<body>\n" +
" <h1>Heading</h1>\n" +
" <script>var formatMe = function() { return false; };</script>\n" +
" <style>.format-disabled { display: none; } </style>\n" +
"</body>"
}, {
fragment: true,
input: "<div><p>Beautify me</p></div><p><p>But not me</p></p>",
output: "<div>\n" +
" <p>Beautify me</p>\n" +
"</div>\n" +
"<p><p>But not me</p></p>"
}, {
fragment: true,
input: "<div><p\n class=\"beauty-me\"\n>Beautify me</p></div><p><p\n class=\"iamalreadybeauty\"\n>But not me</p></p>",
output: "<div>\n" +
" <p class=\"beauty-me\">Beautify me</p>\n" +
"</div>\n" +
"<p><p\n" +
" class=\"iamalreadybeauty\"\n" +
">But not me</p></p>"
}, {
fragment: true,
unchanged: "<div><span>blabla<div>something here</div></span></div>",
}, {
fragment: true,
unchanged: "<div><br /></div>"
}]
}, {
name: "New Test Suite"
}],
Expand Down