-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
52 lines (40 loc) · 904 Bytes
/
utils.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
const css = require('css')
/**
* Returns an array of selectors from a given CSS string.
*
* @param {string} globalStyles - The CSS string to parse.
* @returns {string[]} - An array of selectors.
*/
function getWPSafelist(globalStyles) {
/**
* @type {string[]}
*/
const safelistSelectors = []
if (!globalStyles) {
return []
}
const ast = css.parse(globalStyles)
const rules = ast?.stylesheet?.rules
if (!rules) {
return []
}
for (let i = 0; i < rules.length; i++) {
const rule = rules[i]
// skip non rules
if (!rule || !('selectors' in rule) || !('declarations' in rule)) {
continue
}
const selectors = rule.selectors
const declarations = rule.declarations
if (!selectors || !declarations) {
continue
}
for (const selector of selectors) {
safelistSelectors.push(selector)
}
}
return safelistSelectors
}
module.exports = {
getWPSafelist,
}