From 96876ed1fa1873e286ce9ccd2cdf0ca6e0928f79 Mon Sep 17 00:00:00 2001 From: Liam Newman Date: Sat, 8 Dec 2018 18:41:47 -0800 Subject: [PATCH] Fix null beautifier and add tests Fixes #1590 --- js/src/html/beautifier.js | 14 ++++++++---- js/test/generated/beautify-html-tests.js | 29 ++++++++++++++++++++++++ test/data/html/tests.js | 26 +++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/js/src/html/beautifier.js b/js/src/html/beautifier.js index 28fd1dd86..809b406a3 100644 --- a/js/src/html/beautifier.js +++ b/js/src/html/beautifier.js @@ -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; @@ -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); diff --git a/js/test/generated/beautify-html-tests.js b/js/test/generated/beautify-html-tests.js index 842f93618..30f240d3e 100644 --- a/js/test/generated/beautify-html-tests.js +++ b/js/test/generated/beautify-html-tests.js @@ -444,6 +444,35 @@ function run_html_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_be '
\n' + ' \n' + ''); + + // null beatuifier function behavior - should still indent + test_fragment( + '', + // -- output -- + ''); + bth( + '', + // -- output -- + ''); + bth( + '', + // -- output -- + ''); bth( '', // -- output -- diff --git a/test/data/html/tests.js b/test/data/html/tests.js index e079db238..e60d3456d 100644 --- a/test/data/html/tests.js +++ b/test/data/html/tests.js @@ -249,6 +249,32 @@ exports.test_data = { ' ', '' ] + }, { + comment: 'null beatuifier function behavior - should still indent', + fragment: true, + input: '', + output: [ + '' + ] + }, { + input: '', + output: [ + '' + ] + }, { + input: '', + output: [ + '' + ] }, { input: '', output: [