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

Added option max_preserve_newlines in CSS beautifier #1867

Merged
merged 7 commits into from
Jan 8, 2021
Merged
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
7 changes: 3 additions & 4 deletions js/src/css/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
29 changes: 29 additions & 0 deletions js/test/generated/beautify-css-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions python/cssbeautifier/css/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,15 @@ def eatString(self, endChars):
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

Expand Down
28 changes: 28 additions & 0 deletions python/cssbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10914,6 +10914,34 @@ def testGenerated(self):
'}')


#============================================================
# 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()
Expand Down
15 changes: 15 additions & 0 deletions test/data/css/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
}]
}, {

}
]
Expand Down