Skip to content

Commit e68b538

Browse files
Merge pull request #247 from r-wettstaedt/selector-groups
feat: handle selector groups
2 parents 1b216ba + 3c527d1 commit e68b538

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

src/index.js

+17-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const postcss = require('postcss');
22
const parser = require('postcss-selector-parser');
33
const {name} = require('../package.json');
44

5-
65
/**
76
* Ensure that attributes with different quotes match.
87
* @param {Object} selector - postcss selector node
@@ -32,6 +31,8 @@ function sortGroups(selector) {
3231
return a.value < b.value ? -1 : 1;
3332
});
3433
});
34+
35+
selector.sort((a, b) => (a.nodes.join('') < b.nodes.join('') ? -1 : 1));
3536
}
3637

3738
/**
@@ -50,12 +51,10 @@ function removeDupProperties(selector) {
5051
}
5152
}
5253

53-
const uniformStyle = parser(
54-
(selector) => {
55-
normalizeAttributes(selector);
56-
sortGroups(selector);
57-
}
58-
);
54+
const uniformStyle = parser((selector) => {
55+
normalizeAttributes(selector);
56+
sortGroups(selector);
57+
});
5958

6059
const defaultOptions = {
6160
removeDuplicatedProperties: false,
@@ -74,25 +73,24 @@ module.exports = postcss.plugin(name, (options) => {
7473
// Check selector parent for any at rule
7574
if (rule.parent.type === 'atrule') {
7675
// Use name and query params as the key
77-
const query = rule.parent.name.toLowerCase()
78-
+ rule.parent.params.replace(/\s+/g, '');
76+
const query =
77+
rule.parent.name.toLowerCase() +
78+
rule.parent.params.replace(/\s+/g, '');
7979

8080
// See if this query key is already in the map table
81-
map = mapTable.has(query) ?
82-
// If it is use it
83-
mapTable.get(query) :
84-
// if not set it and get it
85-
mapTable.set(query, new Map()).get(query);
81+
map = mapTable.has(query)
82+
? // If it is use it
83+
mapTable.get(query)
84+
: // if not set it and get it
85+
mapTable.set(query, new Map()).get(query);
8686
} else {
8787
// Otherwise we are dealing with a selector in the root
8888
map = mapTable.get('root');
8989
}
9090

91-
const selector = uniformStyle.processSync(
92-
rule.selector, {
93-
lossless: false,
94-
}
95-
);
91+
const selector = uniformStyle.processSync(rule.selector, {
92+
lossless: false,
93+
});
9694

9795
if (map.has(selector)) {
9896
// store original rule as destination

test/duplicated-css.js

+14
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ test(
208208
'.one.two {}'
209209
);
210210

211+
test(
212+
'selector groups',
213+
css,
214+
'.one .two, .one .three {} .one .two, .one .three {}',
215+
'.one .two, .one .three {}'
216+
);
217+
218+
test(
219+
'selector groups with different order',
220+
css,
221+
'.one .two, .one .three {} .one .three, .one .two {}',
222+
'.one .two, .one .three {}'
223+
);
224+
211225
test(
212226
'selectors and seperately selectors within media query',
213227
css,

test/duplicated-extension.js

+7
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ test(
5555
'.one.two {} .two{&.one {}}',
5656
'.one.two {}'
5757
);
58+
59+
test(
60+
'nested selector grouping',
61+
[nestedCSS, scss],
62+
'.one {&.two, .two& {}} .one {.two&, &.two {}}',
63+
'.one.two, .two.one {}'
64+
);

test/unique-css.js

+7
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,10 @@ test(
163163
'@keyframes a {0% {} 100% {}} @-webkit-keyframes a {0% {} 100% {}}',
164164
'@keyframes a {0% {} 100% {}} @-webkit-keyframes a {0% {} 100% {}}'
165165
);
166+
167+
test(
168+
'selector groups partially overlapping',
169+
css,
170+
'.one, .two {} .one, .two, .three {}',
171+
'.one, .two {} .one, .two, .three {}'
172+
);

0 commit comments

Comments
 (0)