-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
57 lines (48 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const postcss = require('postcss')
const Selector = require('postcss-selector-parser')
module.exports = postcssPrefix
postcssPrefix.postcss = true
function postcssPrefix (prefix, options) {
options = options || {}
return {
postcssPlugin: 'postcss-prefix',
Rule: function (rule) {
const selector = Selector(transformSelectors)
.processSync(rule.selector)
rule.selector = selector
}
}
function transformSelectors (selectors) {
selectors.walk(function (selector) {
// don't prefix if parent is not selector
if (selector.parent.type !== 'selector') return
// don't prefix pseudo selector args unless it's `:not`
if (selector.parent.parent.type === 'pseudo' && selector.parent.parent.value !== ':not') {
return
}
if (selector.type === 'pseudo' && selector.value === ':host') {
const prefixNode = getPrefixNode(prefix)
const replacement = Selector.selector()
replacement.nodes = [prefixNode].concat(selector.clone().nodes)
selector.replaceWith(replacement)
} else if (selector.type === 'pseudo' && selector.value === ':host-context') {
const prefixNode = getPrefixNode(prefix)
const replacement = Selector.selector()
const prevNodes = selector.clone().nodes
replacement.nodes = prevNodes.concat(' ', prefixNode, ', ', prefixNode, prevNodes)
selector.replaceWith(replacement)
}
})
}
}
function getPrefixNode (prefix) {
const sigil = prefix[0]
const value = prefix.slice(1)
switch (sigil) {
case '#':
return Selector.id({ value: value })
case '.':
return Selector.className({ value: value })
}
}