Skip to content

Commit 958f99a

Browse files
authored
fix: minify whitespace in selector lists (#117)
closes #116
1 parent 917eb5c commit 958f99a

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const CLOSE_BRACKET = ']'
1313
const OPEN_BRACE = '{'
1414
const CLOSE_BRACE = '}'
1515
const EMPTY_BLOCK = '{}'
16+
const COMMA = ','
1617
const TYPE_ATRULE = 'Atrule'
1718
const TYPE_RULE = 'Rule'
1819
const TYPE_BLOCK = 'Block'
@@ -150,7 +151,7 @@ export function format(css, { minify = false } = {}) {
150151
}
151152

152153
if (item.next !== null) {
153-
buffer += `,` + NEWLINE
154+
buffer += COMMA + NEWLINE
154155
}
155156

156157
let end = item.next !== null ? start_offset(item.next.data) : end_offset(node)
@@ -203,13 +204,13 @@ export function format(css, { minify = false } = {}) {
203204
break
204205
}
205206
case TYPE_SELECTORLIST: {
206-
child.children.forEach((grandchild, item) => {
207-
if (grandchild.type === TYPE_SELECTOR) {
208-
buffer += print_simple_selector(grandchild)
207+
child.children.forEach((selector_list_item, item) => {
208+
if (selector_list_item.type === TYPE_SELECTOR) {
209+
buffer += print_simple_selector(selector_list_item)
209210
}
210211

211-
if (item.next) {
212-
buffer += ',' + SPACE
212+
if (item.next && item.next.data.type === TYPE_SELECTOR) {
213+
buffer += COMMA + OPTIONAL_SPACE
213214
}
214215
})
215216
break

test/minify.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ test('removes whitespace before !important', () => {
8383
assert.equal(actual, expected)
8484
})
8585

86+
test.only('minifies complex selectors', () => {
87+
let actual = minify(`:is(a, b) { color: green }`)
88+
let expected = `:is(a,b){color:green}`
89+
assert.equal(actual, expected)
90+
})
91+
8692
test.run()

0 commit comments

Comments
 (0)