Skip to content

Commit 87bdfd0

Browse files
committedJun 20, 2022
Fix two spacing issue in CSS
1 parent a187f22 commit 87bdfd0

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed
 

‎js/src/css/beautifier.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ Beautifier.prototype.beautify = function() {
400400
}
401401
}
402402
} else if (this._ch === '"' || this._ch === '\'') {
403-
this.preserveSingleSpace(isAfterSpace);
403+
var preserveQuoteSpace = previous_ch === '"' || previous_ch === '\'';
404+
this.preserveSingleSpace(preserveQuoteSpace || isAfterSpace);
404405
this.print_string(this._ch + this.eatString(this._ch));
405406
this.eatWhitespace(true);
406407
} else if (this._ch === ';') {
@@ -507,7 +508,7 @@ Beautifier.prototype.beautify = function() {
507508
this._ch = '';
508509
}
509510
} else if (this._ch === '!' && !this._input.lookBack("\\")) { // !important
510-
this.print_string(' ');
511+
this._output.space_before_token = true;
511512
this.print_string(this._ch);
512513
} else {
513514
var preserveAfterSpace = previous_ch === '"' || previous_ch === '\'';

‎python/cssbeautifier/css/beautifier.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,8 @@ def beautify(self):
422422
# pseudo-element
423423
self.print_string(":")
424424
elif self._ch == '"' or self._ch == "'":
425-
self.preserveSingleSpace(isAfterSpace)
425+
preserveQuoteSpace = previous_ch == '"' or previous_ch == "'"
426+
self.preserveSingleSpace(preserveQuoteSpace or isAfterSpace)
426427
self.print_string(self._ch + self.eatString(self._ch))
427428
self.eatWhitespace(True)
428429
elif self._ch == ";":
@@ -537,7 +538,7 @@ def beautify(self):
537538
self._ch = ""
538539
elif self._ch == "!" and not (self._input.lookBack("\\")):
539540
# !important
540-
self.print_string(" ")
541+
self._output.space_before_token = True
541542
self.print_string(self._ch)
542543
else:
543544
preserveAfterSpace = previous_ch == '"' or previous_ch == "'"

‎test/data/css/tests.js

+25
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,31 @@ exports.test_data = {
15021502
' filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=3);',
15031503
'}'
15041504
]
1505+
}, {
1506+
comment: "#2056 - Extra space before !important added",
1507+
unchanged: [
1508+
'.x {',
1509+
' $d: a !default;',
1510+
'}'
1511+
]
1512+
}, {
1513+
unchanged: [
1514+
'.x {',
1515+
' $d: a !default;',
1516+
' @if $x !=0 {',
1517+
' color: $var !important;',
1518+
' }',
1519+
'}'
1520+
]
1521+
}, {
1522+
comment: "#2051 - css format removes space after quoted value",
1523+
unchanged: [
1524+
'q {',
1525+
' quotes: \\\'"\\\' \\\'"\\\' "\\\'" "\\\'";',
1526+
' quotes: "some" \\\'thing\\\' "different";',
1527+
' quotes: \\\'some\\\' "thing" \\\'different\\\';',
1528+
'}'
1529+
]
15051530
}]
15061531
}, {
15071532
name: "Regression tests - with default options",

0 commit comments

Comments
 (0)
Please sign in to comment.