-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathattribute.js
74 lines (61 loc) · 1.7 KB
/
attribute.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
/**
* @typedef {import('css-selector-parser').AstAttribute} AstAttribute
* @typedef {import('css-selector-parser').AstRule} AstRule
* @typedef {import('./types.js').Node} Node
*/
import {ok as assert} from 'devlop'
import {indexable} from './util.js'
/**
* @param {AstAttribute} query
* Query.
* @param {Node} node
* Node.
* @returns {boolean}
* Whether `node` matches `query`.
*/
export function attribute(query, node) {
indexable(node)
const value = node[query.name]
// Exists.
if (!query.value) {
return value !== null && value !== undefined
}
assert(query.value.type === 'String', 'expected plain string')
let key = query.value.value
let normal = value === null || value === undefined ? undefined : String(value)
// Case-sensitivity.
if (query.caseSensitivityModifier === 'i') {
key = key.toLowerCase()
if (normal) {
normal = normal.toLowerCase()
}
}
if (value !== undefined) {
switch (query.operator) {
// Exact.
case '=': {
return typeof normal === 'string' && key === normal
}
// Ends.
case '$=': {
return typeof value === 'string' && value.slice(-key.length) === key
}
// Contains.
case '*=': {
return typeof value === 'string' && value.includes(key)
}
// Begins.
case '^=': {
return typeof value === 'string' && key === value.slice(0, key.length)
}
// Space-separated list.
case '~=': {
// type-coverage:ignore-next-line -- some bug with TS.
return (Array.isArray(value) && value.includes(key)) || normal === key
}
// Other values are not yet supported by CSS.
// No default
}
}
return false
}