-
Notifications
You must be signed in to change notification settings - Fork 1
/
postcss.js
132 lines (124 loc) · 6.01 KB
/
postcss.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
** style-scope -- PostCSS and PostHTML plugins for locally scoped style
** Copyright (c) 2017-2024 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const postcssSelectorParser = require("postcss-selector-parser")
/* PostCSS Plugin SPI */
module.exports = (options = {}) => {
/* default options */
options = Object.assign({}, {
atDirective: "scope",
atDirectiveEsc: "scope-esc",
attrPrefix: "scope-",
rootScope: "none"
}, options)
/* provide tree processor */
return {
postcssPlugin: "postcss-scope",
Once (css /*, result */) {
/* list of nodes to remove */
var remove = []
/* walk down a single CSS node */
const walkNode = (node, scope) => {
var scopeLocal = scope
if (typeof node === "object" && node.type === "atrule" && node.name === options.atDirective) {
/* @<directive> <id> */
var arg = node.params.replace(/^["']/, "").replace(/['"]$/, "")
if (arg === "")
throw node.error(`postcss-scope: WARNING: missing scope argument on directive @${options.atDirective}`)
if (typeof node.nodes === "object" && node.nodes instanceof Array && node.nodes.length > 0)
/* @<directive> <id> { ... } */
scopeLocal = arg
else
/* @<directive> <id>; */
scope = arg
remove.push(node)
}
else if (typeof node === "object" && node.type === "atrule" && node.name === options.atDirectiveEsc) {
/* rename escaped directive to non-escaped directive */
node.name = options.atDirective
}
else if (typeof node === "object" && node.type === "rule" && scope !== "none") {
/* <selector> { ... } */
node.selector = postcssSelectorParser((node) => {
node.walk((node /*, index */) => {
if (node.type === "selector") {
if (node.parent && node.parent.type === "pseudo" && node.parent.value !== ":not")
return
const mkAttr = () =>
postcssSelectorParser.attribute({ attribute: `${options.attrPrefix}${scope}` })
let insertBefore = []
let handled = false
node.each((subnode) => {
if (subnode.type === "pseudo") {
if (!handled) {
insertBefore.push(subnode)
handled = true
}
}
else if (subnode.type === "combinator") {
if (!handled)
insertBefore.push(subnode)
handled = false
}
})
insertBefore.forEach((subnode) => {
node.insertBefore(subnode, mkAttr())
})
if (!handled)
node.append(mkAttr())
}
})
}).processSync(node.selector, { lossless: true })
}
/* walk down all child nodes */
if (typeof node.nodes === "object" && node.nodes instanceof Array) {
node.nodes.forEach((node) => {
scopeLocal = walkNode(node, scopeLocal) /* RECURSION */
})
}
return scope
}
walkNode(css, options.rootScope)
/* remove all previously processed @<directive> nodes */
const depth = (node) => {
var depth = 0;
while ((node = node.parent) !== undefined)
depth++;
return depth
}
remove.sort((a, b) => depth(b) - depth(a)).forEach((node) => {
if (typeof node.nodes === "object" && node.nodes instanceof Array && node.nodes.length > 0) {
var anchor = node
var nodes = node.nodes.slice(0)
nodes.forEach((node) => {
node.remove()
anchor.parent.insertAfter(anchor, node)
anchor = node
})
}
node.remove()
})
}
}
}
module.exports.postcss = true