Skip to content

Commit 22a73b7

Browse files
committed
Fix duplicated prefixed selectors on reformatted CSS
The `already()` check compared `before.selector` to the generated prefixed selector with strict equality. Generated prefixed selectors always join the comma list with `', '`, so once an existing prefixed rule had its selector list reformatted (e.g. one selector per line by a formatter), the comparison failed and Autoprefixer inserted a duplicate prefixed rule on every run. Normalize the compared selector with `list.comma(...).join(', ')` so whitespace differences in the selector list no longer defeat the duplication check. Closes #1497
1 parent 958d390 commit 22a73b7

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/selector.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ class Selector extends Prefixer {
3838
return false
3939
}
4040

41+
let beforeSelector = list.comma(before.selector).join(', ')
42+
4143
let some = false
4244
for (let key in prefixeds[this.name]) {
4345
let prefixed = prefixeds[this.name][key]
44-
if (before.selector === prefixed) {
46+
if (beforeSelector === prefixed) {
4547
if (prefix === key) {
4648
return true
4749
} else {

test/selector.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ test('finds prefixed even if unknown prefix is between', () => {
8686
is(selector.already(css.nodes[2], prefixeds, '-moz-'), true)
8787
})
8888

89+
test('finds prefixed even if selector list was reformatted', () => {
90+
let css = parse(
91+
'.a::-moz-selection,\n.b::-moz-selection {}\n' +
92+
'.a::selection,\n.b::selection {}'
93+
)
94+
let prefixeds2 = selector.prefixeds(css.nodes[1])
95+
is(selector.already(css.nodes[1], prefixeds2, '-moz-'), true)
96+
})
97+
8998
test('adds prefix to selectors', () => {
9099
equal(
91100
selector.replace('body ::selection, input::selection, a', '-ms-'),

0 commit comments

Comments
 (0)