Skip to content

Commit

Permalink
Fix spacing after closing ) and following text (#56)
Browse files Browse the repository at this point in the history
closes #55
  • Loading branch information
bartveneman authored May 16, 2024
1 parent 1c21324 commit abccab1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ function print_prelude(node, css) {
return buffer
.replace(/\s*([:,])/g, buffer.includes('selector(') ? '$1' : '$1 ') // force whitespace after colon or comma, except inside `selector()`
.replace(/\s*(=>|<=)\s*/g, ' $1 ') // force whitespace around => and <=
.replace(/\)([a-zA-Z])/g, ') $1') // force whitespace between closing parenthesis and following text (usually and|or)
.replace(/(?<!<=)(?<!=>)(?<!<= )([<>])(?![<= ])(?![=> ])(?![ =>])/g, ' $1 ')
.replace(/\s+/g, SPACE) // collapse multiple whitespaces into one
}
Expand Down
31 changes: 30 additions & 1 deletion test/atrules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ test('Atrule blocks are surrounded by {} with correct spacing and indentation',
assert.equal(actual, expected)
})

test('AtRule prelude formatting', () => {
test('@media prelude formatting', () => {
let fixtures = [
[`@media all{}`, `@media all {}`],
[`@media all and print{}`, `@media all and print {}`],
[`@media (min-width:1000px){}`, `@media (min-width: 1000px) {}`],
[`@media (min-width : 1000px) {}`, `@media (min-width: 1000px) {}`],
[`@media all and (transform-3d) {}`, `@media all and (transform-3d) {}`],
[`@media only screen and (min-width: 1024px)and (max-width: 1439px), only screen and (min-width: 768px)and (max-width: 1023px) {}`, `@media only screen and (min-width: 1024px) and (max-width: 1439px), only screen and (min-width: 768px) and (max-width: 1023px) {}`],
[`@media (min-width: 1024px)or (max-width: 1439px) {}`, `@media (min-width: 1024px) or (max-width: 1439px) {}`],
[`@media all and (transform-3d), (-webkit-transform-3d) {}`, `@media all and (transform-3d), (-webkit-transform-3d) {}`],
[`@media screen or print {}`, `@media screen or print {}`],
[`@media (update: slow) or (hover: none) {}`, `@media (update: slow) or (hover: none) {}`],
[`@media (update: slow)or (hover: none) {}`, `@media (update: slow) or (hover: none) {}`],
[`@layer test;`, `@layer test;`],
[`@layer tbody,thead;`, `@layer tbody, thead;`],
[`@supports (display:grid){}`, `@supports (display: grid) {}`],
Expand All @@ -78,6 +82,31 @@ test('AtRule prelude formatting', () => {
}
})

test('@supports prelude formatting', () => {
let fixtures = [
[`@supports (display:grid){}`, `@supports (display: grid) {}`],
[`@supports (-webkit-appearance: none) {}`, `@supports (-webkit-appearance: none) {}`],
['@supports selector([popover]:open) {}', '@supports selector([popover]:open) {}'],
]

for (let [css, expected] of fixtures) {
let actual = format(css)
assert.equal(actual, expected)
}
})

test('@layer prelude formatting', () => {
let fixtures = [
[`@layer test;`, `@layer test;`],
[`@layer tbody,thead;`, `@layer tbody, thead;`],
]

for (let [css, expected] of fixtures) {
let actual = format(css)
assert.equal(actual, expected)
}
})

test('single empty line after a rule, before atrule', () => {
let actual = format(`
rule1 { property: value }
Expand Down

0 comments on commit abccab1

Please sign in to comment.