Cleaned commit history and added changes

This commit is contained in:
Juan Medina 2020-12-24 12:17:04 -06:00
parent 97caa30849
commit 5ddfa5784b
5 changed files with 79 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -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();

View File

@ -139,12 +139,12 @@ 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
if newline_count == 0 or newline_count < self._options.max_preserve_newlines:
newline_count += 1
self._output.add_new_line(True)
return result

View File

@ -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()

View File

@ -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}'
}]
}, {
}
]