Skip to content

Commit 88a023e

Browse files
committed
feat(prefix): drop support for non :host prefix
1 parent 9c2f906 commit 88a023e

File tree

3 files changed

+33
-47
lines changed

3 files changed

+33
-47
lines changed

index.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,34 @@ module.exports = postcss.plugin('postcss-prefix', postcssPrefix)
66
function postcssPrefix (prefix, options) {
77
options = options || {}
88

9-
return function (root) {
9+
return function walk (root) {
1010
root.walkRules(function (rule) {
11-
if (rule.parent && rule.parent.type === 'atrule' &&
12-
rule.parent.name.indexOf('keyframes') !== -1) {
13-
return
14-
}
15-
16-
const selector = Selector(
17-
transformSelectors
18-
).process(rule.selector).result
11+
const selector = Selector(transformSelectors)
12+
.process(rule.selector).result
1913

2014
rule.selector = selector
2115
})
2216
}
2317

2418
function transformSelectors (selectors) {
2519
selectors.eachInside(function (selector) {
26-
if (
27-
// if parent is not selector and
28-
selector.parent.type !== 'selector' ||
29-
// if not first node in container
30-
selector.parent.nodes[0] !== selector ||
31-
// don't prefix pseudo selector args unless it's `:not`
32-
(selector.parent.parent.type === 'pseudo' && selector.parent.parent.value !== ':not')
33-
) return
20+
// don't prefix if parent is not selector
21+
if (selector.parent.type !== 'selector') return
3422

35-
const prefixNode = getPrefixNode(prefix)
23+
// don't prefix if not first node in container
24+
if (selector.parent.nodes[0] !== selector) return
3625

37-
if (selector.type === 'pseudo') {
38-
switch (selector.value) {
39-
case ':root':
40-
return
41-
case ':host':
42-
const replacement = Selector.selector()
43-
replacement.nodes = [prefixNode].concat(selector.clone().nodes)
44-
selector.replaceWith(replacement)
45-
return
46-
}
26+
// don't prefix pseudo selector args unless it's `:not`
27+
if (selector.parent.parent.type === 'pseudo' && selector.parent.parent.value !== ':not') {
28+
return
4729
}
4830

49-
// prefix
50-
//
51-
// start by prepending a space combinator
52-
selector.parent.prepend(Selector.combinator({ value: ' ' }))
53-
// then prepend the prefix node, preserving spacing
54-
prefixNode.spaces.before = selector.spaces.before
55-
selector.spaces.before = ''
56-
selector.parent.prepend(prefixNode)
31+
if (selector.type === 'pseudo' && selector.value === ':host') {
32+
const prefixNode = getPrefixNode(prefix)
33+
const replacement = Selector.selector()
34+
replacement.nodes = [prefixNode].concat(selector.clone().nodes)
35+
selector.replaceWith(replacement)
36+
}
5737
})
5838
}
5939
}

test/fixture-out.css

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,26 @@
1313
#hello-world { color: green; }
1414
#hello-world:hover { color: blue; }
1515

16-
#hello-world h1 {}
17-
#hello-world h1.title {}
18-
#hello-world h2#thing {}
19-
#hello-world #thing h3 {}
16+
h1 {}
17+
h1.title {}
18+
h2#thing {}
19+
#thing h3 {}
2020

21-
#hello-world .a, #hello-world .b, #hello-world .c {}
21+
.a, .b, .c {}
2222

2323
@media screen and (max-width: 42rem) {
24-
#hello-world #another .thing > x-here {}
24+
#hello-world {
25+
.thing > x-here {}
26+
}
27+
#another .thing > x-here {}
2528
}
2629

27-
#hello-world .stuff:nth-child(even) {}
30+
.stuff:nth-child(even) {}
2831

29-
#hello-world .stuff:nth-last-child(even) {}
32+
.stuff:nth-last-child(even) {}
3033

31-
#hello-world .stuff:nth-of-type(2) {}
34+
.stuff:nth-of-type(2) {}
3235

33-
#hello-world .stuff:nth-last-of-type(2) {}
36+
.stuff:nth-last-of-type(2) {}
3437

35-
#hello-world .stuff:not(#hello-world p) {}
38+
.stuff:not(p) {}

test/fixture.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ h2#thing {}
2121
.a, .b, .c {}
2222

2323
@media screen and (max-width: 42rem) {
24+
:host {
25+
.thing > x-here {}
26+
}
2427
#another .thing > x-here {}
2528
}
2629

0 commit comments

Comments
 (0)