Skip to content

Commit dea7915

Browse files
fix(css): preserve spaces around >= and <= in media query range operators
Fixes #2164 Fixes the CSS beautifier collapsing space after range operators (>= <=) inside @media query conditions. Before: @media (width >= 90rem) -> @media (width >=90rem) After: @media (width >= 90rem) -> @media (width >= 90rem) Also normalizes minified input: @media (width>=90rem) -> @media (width >= 90rem) Fixed in both JS and Python implementations.
1 parent 45898e1 commit dea7915

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

js/src/css/beautifier.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,18 @@ Beautifier.prototype.beautify = function() {
500500
} else {
501501
this._output.space_before_token = true;
502502
}
503+
} else if ((this._ch === '>' || this._ch === '<') && parenLevel > 0 && this._input.peek() === '=') {
504+
// Media query range operator: >= or <=
505+
// Ensure a single space before and after the operator
506+
this._output.space_before_token = true;
507+
this.print_string(this._ch);
508+
this._input.next(); // consume '='
509+
this.print_string('=');
510+
this.eatWhitespace();
511+
this._output.space_before_token = true;
512+
if (whitespaceChar.test(this._ch)) {
513+
this._ch = '';
514+
}
503515
} else if ((this._ch === '>' || this._ch === '+' || this._ch === '~') && !insidePropertyValue && parenLevel === 0) {
504516
//handle combinator spacing
505517
if (this._options.space_around_combinator) {

python/cssbeautifier/css/beautifier.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,21 @@ def beautify(self):
519519
self._output.add_new_line()
520520
else:
521521
self._output.space_before_token = True
522+
elif (
523+
(self._ch == ">" or self._ch == "<")
524+
and parenLevel > 0
525+
and self._input.peek() == "="
526+
):
527+
# Media query range operator: >= or <=
528+
# Ensure a single space before and after the operator
529+
self._output.space_before_token = True
530+
self.print_string(self._ch)
531+
self._input.next() # consume '='
532+
self.print_string("=")
533+
self.eatWhitespace()
534+
self._output.space_before_token = True
535+
if bool(whitespaceChar.search(self._ch)):
536+
self._ch = ""
522537
elif (
523538
(self._ch == ">" or self._ch == "+" or self._ch == "~")
524539
and not insidePropertyValue

test/data/css/tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,24 @@ exports.test_data = {
388388
input: '@media print {.tab{}}',
389389
output: '@media print{{curly_separator}}{\n .tab{{curly_separator2}}{}\n}'
390390
},
391+
{
392+
comment: 'Media query range operators should preserve spaces on both sides',
393+
unchanged: '@media (width >= 90rem) {\n .foo {}\n}'
394+
},
395+
{
396+
comment: 'Media query range operators should preserve spaces on both sides',
397+
unchanged: '@media (width <= 90rem) {\n .foo {}\n}'
398+
},
399+
{
400+
comment: 'Media query range operators - minified input should have spaces enforced around operator',
401+
input: '@media (width>=90rem){.foo{}}',
402+
output: '@media (width >= 90rem){{curly_separator}}{\n .foo{{curly_separator2}}{}\n}'
403+
},
404+
{
405+
comment: 'Media query range operators - minified input should have spaces enforced around operator',
406+
input: '@media (width<=90rem){.foo{}}',
407+
output: '@media (width <= 90rem){{curly_separator}}{\n .foo{{curly_separator2}}{}\n}'
408+
},
391409
{
392410
comment: 'This is bug #1489',
393411
input: '@media print {.tab,.bat{}}',

0 commit comments

Comments
 (0)