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 space_in_paren to the CSS options #1857

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions js/src/css/beautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,22 @@ Beautifier.prototype.beautify = function() {
} else if (this._ch === '(') { // may be a url
if (this._input.lookBack("url")) {
this.print_string(this._ch);
this.eatWhitespace();
if (this._options.space_in_paren) {
this._output.space_before_token = true;
} else {
this.eatWhitespace();
}
parenLevel++;
this.indent();
this._ch = this._input.next();
if (this._ch === ')' || this._ch === '"' || this._ch === '\'') {
this._input.back();
} else if (this._ch) {
this.print_string(this._ch + this.eatString(')'));
if (this._options.space_in_paren) {
this.print_string(this._ch + this.eatString(')').slice(0, -1) + ' )');
} else {
this.print_string(this._ch + this.eatString(')'));
}
if (parenLevel) {
parenLevel--;
this.outdent();
Expand All @@ -421,7 +429,11 @@ Beautifier.prototype.beautify = function() {
} else {
this.preserveSingleSpace(isAfterSpace);
this.print_string(this._ch);
this.eatWhitespace();
if (this._options.space_in_paren) {
this._output.space_before_token = true;
} else {
this.eatWhitespace();
}
parenLevel++;
this.indent();
}
Expand All @@ -430,6 +442,9 @@ Beautifier.prototype.beautify = function() {
parenLevel--;
this.outdent();
}
if (this._options.space_in_paren) {
this._output.space_before_token = true;
}
this.print_string(this._ch);
} else if (this._ch === ',') {
this.print_string(this._ch);
Expand Down
1 change: 1 addition & 0 deletions js/src/css/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function Options(options) {

this.selector_separator_newline = this._get_boolean('selector_separator_newline', true);
this.newline_between_rules = this._get_boolean('newline_between_rules', true);
this.space_in_paren = this._get_boolean('space_in_paren', false);
var space_around_selector_separator = this._get_boolean('space_around_selector_separator');
this.space_around_combinator = this._get_boolean('space_around_combinator') || space_around_selector_separator;

Expand Down
34 changes: 34 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,40 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
'}');


//============================================================
// space_in_paren true
reset_options();
set_name('space_in_paren true');
opts.space_in_paren = true;
test_fragment(
'a {\n' +
'width: min(100%,100vw);\n' +
'height: calc( 100vh - 100px );\n' +
'}',
// -- output --
'a {\n' +
' width: min( 100%, 100vw );\n' +
' height: calc( 100vh - 100px );\n' +
'}');


//============================================================
// space_in_paren false
reset_options();
set_name('space_in_paren false');
opts.space_in_paren = false;
test_fragment(
'a {\n' +
'width: min(100%,100vw);\n' +
'height: calc( 100vh - 100px );\n' +
'}',
// -- output --
'a {\n' +
' width: min(100%, 100vw);\n' +
' height: calc(100vh - 100px);\n' +
'}');


//============================================================
//
reset_options();
Expand Down
19 changes: 16 additions & 3 deletions python/cssbeautifier/css/beautifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,27 +429,40 @@ def beautify(self):
# may be a url
if self._input.lookBack("url"):
self.print_string(self._ch)
self.eatWhitespace()
if self._options.space_in_paren:
self._output.space_before_token = True
else:
self.eatWhitespace()
parenLevel += 1
self.indent()
self._ch = self._input.next()
if self._ch in {")", '"', "'"}:
self._input.back()
elif self._ch is not None:
self.print_string(self._ch + self.eatString(")"))
if self._options.space_in_paren:
self.print_string(
self._ch + self.eatString(")")[0:-1] + " )"
)
else:
self.print_string(self._ch + self.eatString(")"))
if parenLevel:
parenLevel -= 1
self.outdent()
else:
self.preserveSingleSpace(isAfterSpace)
self.print_string(self._ch)
self.eatWhitespace()
if self._options.space_in_paren:
self._output.space_before_token = True
else:
self.eatWhitespace()
parenLevel += 1
self.indent()
elif self._ch == ")":
if parenLevel:
parenLevel -= 1
self.outdent()
if self._options.space_in_paren:
self._output.space_before_token = True
self.print_string(self._ch)
elif self._ch == ",":
self.print_string(self._ch)
Expand Down
1 change: 1 addition & 0 deletions python/cssbeautifier/css/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, options=None):
"selector_separator_newline", True
)
self.newline_between_rules = self._get_boolean("newline_between_rules", True)
self.space_in_paren = self._get_boolean("space_in_paren", False)

brace_style_split = self._get_selection_list(
"brace_style",
Expand Down
32 changes: 32 additions & 0 deletions python/cssbeautifier/tests/generated/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10914,6 +10914,38 @@ def testGenerated(self):
'}')


#============================================================
# space_in_paren true
self.reset_options()
self.options.space_in_paren = true
test_fragment(
'a {\n' +
'width: min(100%,100vw);\n' +
'height: calc( 100vh - 100px );\n' +
'}',
# -- output --
'a {\n' +
' width: min( 100%, 100vw );\n' +
' height: calc( 100vh - 100px );\n' +
'}')


#============================================================
# space_in_paren false
self.reset_options()
self.options.space_in_paren = false
test_fragment(
'a {\n' +
'width: min(100%,100vw);\n' +
'height: calc( 100vh - 100px );\n' +
'}',
# -- output --
'a {\n' +
' width: min(100%, 100vw);\n' +
' height: calc(100vh - 100px);\n' +
'}')


#============================================================
#
self.reset_options()
Expand Down
42 changes: 42 additions & 0 deletions test/data/css/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,48 @@ exports.test_data = {
]
}]
}, {
name: "space_in_paren true",
description: "",
options: [
{ name: "space_in_paren", value: "true" }
],
tests: [{
fragment: true,
input: [
'a {',
'width: min(100%,100vw);',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add another test where the input has multiple spaces in inside the paren, example: 'width: min(. 100%. , 100vw. );',. In the case the extra spaces should be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! Will add them in the coming week.

'height: calc( 100vh - 100px );',
'}'
],
output: [
'a {',
' width: min( 100%, 100vw );',
' height: calc( 100vh - 100px );',
'}'
]
}]
}, {
name: "space_in_paren false",
description: "",
options: [
{ name: "space_in_paren", value: "false" }
],
tests: [{
fragment: true,
input: [
'a {',
'width: min(100%,100vw);',
'height: calc( 100vh - 100px );',
'}'
],
output: [
'a {',
' width: min(100%, 100vw);',
' height: calc(100vh - 100px);',
'}'
]
}]
}, {

}
]
Expand Down