Skip to content

Commit

Permalink
Fix null beautifier and add tests
Browse files Browse the repository at this point in the history
Fixes #1590
  • Loading branch information
bitwiseman committed Dec 9, 2018
1 parent f79082e commit bdf611c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
14 changes: 9 additions & 5 deletions js/src/html/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ var get_custom_beautifier_name = function(tag_check, start_token) {
return 'javascript';
} else if (typeAttribute.search(/(text|application|dojo)\/(x-)?(html)/) > -1) {
return 'html';
} else if (typeAttribute.search(/test\/null/) > -1) {
// Test only mime-type for testing the beautifier when null is passed as beautifing function
return 'null';
}

return null;
Expand Down Expand Up @@ -479,11 +482,12 @@ Beautifier.prototype._print_custom_beatifier_text = function(printer, raw_token,
text = _beautifier(indentation + text, child_options);
} else {
// simply indent the string otherwise
var white = text.match(/^\s*/)[0];
var _level = white.match(/[^\n\r]*$/)[0].split(this._options.indent_string).length - 1;
var reindent = this._get_full_indent(script_indent_level - _level);
text = (indentation + text.trim())
.replace(/\r\n|\r|\n/g, '\n' + reindent);
var white = raw_token.whitespace_before;
if (white) {
text = text.replace(new RegExp('\n(' + white + ')?', 'g'), '\n');
}

text = indentation + text.replace(/\n/g, '\n' + indentation);
}
if (text) {
printer.print_raw_text(text);
Expand Down
29 changes: 29 additions & 0 deletions js/test/generated/beautify-html-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,35 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be
' <div></div>\n' +
' </div>\n' +
'</script>');

// null beatifier behavior - should still indent
test_fragment(
'<script type="test/null">\n' +
' <div>\n' +
' <div></div><div></div></div></script>',
// -- output --
'<script type="test/null">\n' +
' <div>\n' +
' <div></div><div></div></div>\n' +
'</script>');
bth(
'<script type="test/null">\n' +
' <div>\n' +
' <div></div><div></div></div></script>',
// -- output --
'<script type="test/null">\n' +
' <div>\n' +
' <div></div><div></div></div>\n' +
'</script>');
bth(
'<script type="test/null">\n' +
'<div>\n' +
'<div></div><div></div></div></script>',
// -- output --
'<script type="test/null">\n' +
' <div>\n' +
' <div></div><div></div></div>\n' +
'</script>');
bth(
'<script>var foo = "bar";</script>',
// -- output --
Expand Down
26 changes: 26 additions & 0 deletions test/data/html/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ exports.test_data = {
' </div>',
'</script>'
]
}, {
comment: 'null beatifier behavior - should still indent',
fragment: true,
input: '<script type="test/null">\n <div>\n <div></div><div></div></div></script>',
output: [
'<script type="test/null">',
' <div>',
' <div></div><div></div></div>',
'</script>'
]
}, {
input: '<script type="test/null">\n <div>\n <div></div><div></div></div></script>',
output: [
'<script type="test/null">',
' <div>',
' <div></div><div></div></div>',
'</script>'
]
}, {
input: '<script type="test/null">\n<div>\n<div></div><div></div></div></script>',
output: [
'<script type="test/null">',
' <div>',
' <div></div><div></div></div>',
'</script>'
]
}, {
input: '<script>var foo = "bar";</script>',
output: [
Expand Down

0 comments on commit bdf611c

Please sign in to comment.