-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.js
164 lines (145 loc) · 4.27 KB
/
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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import walk from 'css-tree/walker'
import { startsWith, strEquals } from '../string-utils.js'
import { hasVendorPrefix } from '../vendor-prefix.js'
/**
*
* @param {import('css-tree').SelectorList} selectorListAst
* @returns {import('css-tree').Selector[]} Analyzed selectors in the selectorList
*/
function analyzeList(selectorListAst, cb) {
let childSelectors = []
walk(selectorListAst, {
visit: 'Selector',
enter: function (node) {
childSelectors.push(cb(node))
}
})
return childSelectors
}
function isPseudoFunction(name) {
return (
strEquals(name, 'not')
|| strEquals(name, 'nth-child')
|| strEquals(name, 'nth-last-child')
|| strEquals(name, 'where')
|| strEquals(name, 'is')
|| strEquals(name, 'has')
|| strEquals(name, 'matches')
|| strEquals(name, '-webkit-any')
|| strEquals(name, '-moz-any')
)
}
/** @param {import('css-tree').Selector} selector */
export function isAccessibility(selector) {
let isA11y = false
walk(selector, function (node) {
if (node.type === 'AttributeSelector') {
if (strEquals('role', node.name.name) || startsWith('aria-', node.name.name)) {
isA11y = true
return this.break
}
}
// Test for [aria-] or [role] inside :is()/:where() and friends
else if (node.type === 'PseudoClassSelector') {
if (isPseudoFunction(node.name)) {
let list = analyzeList(node, isAccessibility)
if (list.some(b => b === true)) {
isA11y = true
return this.skip
}
return this.skip
}
}
})
return isA11y;
}
/**
* Get the Complexity for the AST of a Selector Node
* @param {import('css-tree').Selector} selector - AST Node for a Selector
* @return {[number, boolean]} - The numeric complexity of the Selector and whether it's prefixed or not
*/
export function getComplexity(selector) {
let complexity = 0
let isPrefixed = false
walk(selector, function (node) {
if (node.type === 'Selector' || node.type === 'Nth') return
complexity++
if (node.type === 'IdSelector'
|| node.type === 'ClassSelector'
|| node.type === 'PseudoElementSelector'
|| node.type === 'TypeSelector'
|| node.type === 'PseudoClassSelector'
) {
if (hasVendorPrefix(node.name)) {
isPrefixed = true
complexity++
}
}
if (node.type === 'AttributeSelector') {
if (Boolean(node.value)) {
complexity++
}
if (hasVendorPrefix(node.name.name)) {
isPrefixed = true
complexity++
}
return this.skip
}
if (node.type === 'PseudoClassSelector') {
if (isPseudoFunction(node.name)) {
let list = analyzeList(node, getComplexity)
// Bail out for empty/non-existent :nth-child() params
if (list.length === 0) return
list.forEach(([c, p]) => {
complexity += c
if (p === true) isPrefixed = true
})
return this.skip
}
}
})
return [complexity, isPrefixed]
}
/**
* Walk a selector node and trigger a callback every time a Combinator was found
* We need create the `loc` for descendant combinators manually, because CSSTree
* does not keep track of whitespace for us. We'll assume that the combinator is
* alwas a single ` ` (space) character, even though there could be newlines or
* multiple spaces
* @param {import('css-tree').CssNode} node
* @param {*} onMatch
*/
export function getCombinators(node, onMatch) {
walk(node, function (
/** @type {import('css-tree').CssNode} */ selectorNode,
/** @type {import('css-tree').ListItem} */ item
) {
if (selectorNode.type === 'Combinator') {
// .loc is null when selectorNode.name === ' '
if (selectorNode.loc === null) {
let previousLoc = item.prev.data.loc.end
let start = {
offset: previousLoc.offset,
line: previousLoc.line,
column: previousLoc.column
}
onMatch({
name: selectorNode.name,
loc: {
start,
end: {
offset: start.offset + 1,
line: start.line,
column: start.column + 1
}
}
})
} else {
onMatch({
name: selectorNode.name,
loc: selectorNode.loc
})
}
}
})
}