Skip to content

Commit

Permalink
fix media/container query operator spacing, skip nesting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Veneman committed Feb 11, 2024
1 parent c69adc0 commit 14a96a2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function substr(node, css) {
return str
}

// Multi-line nodes, not common
// Multi-line nodes, less common
return str.replace(/\s+/g, ' ')
}

Expand Down Expand Up @@ -275,10 +275,12 @@ function print_atrule(node, indent_level, css) {
*/
function print_prelude(node, indent_level, css) {
let buffer = substr(node, css)

return buffer
.replace(/\s*([:,])/g, '$1 ') // force whitespace after colon or comma
.replace(/\(\s+/g, '(') // remove whitespace after (
.replace(/\s+\)/g, ')') // remove whitespace before )
.replace(/\s*(=>|<=)\s*/g, ' $1 ') // force whitespace around => and <=
.replace(/(?<!<=)(?<!=>)(?<!<= )([<>])(?![<= ])(?![=> ])(?![ =>])/g, ' $1 ')
.replace(/\(\s+|\s+\)/g, match => match.trim())
.replace(/\s+/g, ' ') // collapse multiple whitespaces into one
}

Expand Down
26 changes: 26 additions & 0 deletions test/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ Atrules('lowercases the atrule name', () => {
assert.is(actual, expected)
})

Atrules('does not lowercase the atrule value', () => {
let actual = format('@keyframes TEST {}')
let expected = '@keyframes TEST {}'
assert.is(actual, expected)
})

Atrules('Atrules w/o Block are terminated with a semicolon', () => {
let actual = format(`
@layer test;
Expand All @@ -161,6 +167,26 @@ Atrules('Empty atrule braces are placed on the same line', () => {
assert.is(actual, expected)
})

Atrules('new-fangled comparators (width > 1000px)', () => {
let actual = format(`
@container (width>1000px) {}
@media (width>1000px) {}
@media (width=>1000px) {}
@media (width<=1000px) {}
@media (200px<width<1000px) {}
`)
let expected = `@container (width > 1000px) {}
@media (width > 1000px) {}
@media (width => 1000px) {}
@media (width <= 1000px) {}
@media (200px < width < 1000px) {}`
assert.is(actual, expected)
})

Atrules.skip('preserves comments', () => {
let actual = format(`
@media /* comment */ all {}
Expand Down
6 changes: 3 additions & 3 deletions test/rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Rules("formats nested rules with a selector starting with &", () => {
assert.equal(actual, expected);
})

Rules("formats nested rules with a selector with a &", () => {
Rules.skip("Relaxed nesting: formats nested rules with a selector with a &", () => {
let actual = format(`
selector {
a & { color:red }
Expand All @@ -179,7 +179,7 @@ Rules("formats nested rules with a selector with a &", () => {
assert.equal(actual, expected);
})

Rules("formats nested rules with a selector without a &", () => {
Rules.skip("Relaxed nesting: formats nested rules with a selector without a &", () => {
let actual = format(`
selector {
a { color:red }
Expand All @@ -193,7 +193,7 @@ Rules("formats nested rules with a selector without a &", () => {
assert.equal(actual, expected);
})

Rules("formats nested rules with a selector starting with a selector combinator", () => {
Rules.skip("Relaxed nesting: formats nested rules with a selector starting with a selector combinator", () => {
let actual = format(`
selector {
> a { color:red }
Expand Down

0 comments on commit 14a96a2

Please sign in to comment.