diff --git a/js/src/css/beautifier.js b/js/src/css/beautifier.js index b7b0a4f7..d9692ca7 100644 --- a/js/src/css/beautifier.js +++ b/js/src/css/beautifier.js @@ -91,13 +91,12 @@ Beautifier.prototype.eatString = function(endChars) { // the first newline will be output Beautifier.prototype.eatWhitespace = function(allowAtLeastOneNewLine) { var result = whitespaceChar.test(this._input.peek()); - var isFirstNewLine = true; - + var newline_count = 0; while (whitespaceChar.test(this._input.peek())) { this._ch = this._input.next(); if (allowAtLeastOneNewLine && this._ch === '\n') { - if (this._options.preserve_newlines || isFirstNewLine) { - isFirstNewLine = false; + if (newline_count === 0 || newline_count < this._options.max_preserve_newlines) { + newline_count++; this._output.add_new_line(true); } } diff --git a/js/test/generated/beautify-css-tests.js b/js/test/generated/beautify-css-tests.js index e56090fa..60016757 100644 --- a/js/test/generated/beautify-css-tests.js +++ b/js/test/generated/beautify-css-tests.js @@ -11044,6 +11044,35 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea '}'); + //============================================================ + // Preserve Newlines and max number of new lines + reset_options(); + set_name('Preserve Newlines and max number of new lines'); + opts.preserve_newlines = true; + opts.max_preserve_newlines = 2; + t( + 'p {\n' + + '\n' + + '\n' + + '\n' + + ' color: blue;\n' + + '}', + // -- output -- + 'p {\n' + + '\n' + + ' color: blue;\n' + + '}'); + t( + 'p {\n' + + '\n' + + ' color: blue;\n' + + '}'); + t( + 'p {\n' + + ' color: blue;\n' + + '}'); + + //============================================================ // reset_options(); diff --git a/python/cssbeautifier/css/beautifier.py b/python/cssbeautifier/css/beautifier.py index 09214ce5..fe63ae5a 100644 --- a/python/cssbeautifier/css/beautifier.py +++ b/python/cssbeautifier/css/beautifier.py @@ -139,13 +139,13 @@ class Beautifier: def eatWhitespace(self, allowAtLeastOneNewLine=False): result = whitespaceChar.search(self._input.peek() or "") is not None isFirstNewLine = True - + newline_count = 0 while whitespaceChar.search(self._input.peek() or "") is not None: self._ch = self._input.next() if allowAtLeastOneNewLine and self._ch == "\n": - if self._options.preserve_newlines or isFirstNewLine: - isFirstNewLine = False - self._output.add_new_line(True) + if newline_count == 0 or newline_count < self._options.max_preserve_newlines: + newline_count += 1 + self._output.add_new_line(True) return result # Nested pseudo-class if we are insideRule diff --git a/python/cssbeautifier/tests/generated/tests.py b/python/cssbeautifier/tests/generated/tests.py index af228df9..d69526f7 100644 --- a/python/cssbeautifier/tests/generated/tests.py +++ b/python/cssbeautifier/tests/generated/tests.py @@ -10914,6 +10914,34 @@ class CSSBeautifierTest(unittest.TestCase): '}') + #============================================================ + # Preserve Newlines and max number of new lines + self.reset_options() + self.options.preserve_newlines = true + self.options.max_preserve_newlines = 2 + t( + 'p {\n' + + '\n' + + '\n' + + '\n' + + ' color: blue;\n' + + '}', + # -- output -- + 'p {\n' + + '\n' + + ' color: blue;\n' + + '}') + t( + 'p {\n' + + '\n' + + ' color: blue;\n' + + '}') + t( + 'p {\n' + + ' color: blue;\n' + + '}') + + #============================================================ # self.reset_options() diff --git a/test/data/css/tests.js b/test/data/css/tests.js index d2fcd6db..e207d478 100644 --- a/test/data/css/tests.js +++ b/test/data/css/tests.js @@ -1800,6 +1800,21 @@ exports.test_data = { ] }] }, { + name: "Preserve Newlines and max number of new lines", + options: [ + { name: "preserve_newlines", value: "true" }, + { name: "max_preserve_newlines", value: "2" } + ], + description: "", + tests: [{ + input: 'p {\n\n\n\n color: blue;\n}', + output: 'p {\n\n color: blue;\n}' + }, { + unchanged: 'p {\n\n color: blue;\n}' + }, { + unchanged: 'p {\n color: blue;\n}' + }] + }, { } ]