Skip to content

Commit 6f16614

Browse files
committed
Update css-selector-parser
1 parent ad8a91d commit 6f16614

File tree

4 files changed

+22
-78
lines changed

4 files changed

+22
-78
lines changed

lib/attribute.js

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,6 @@
77
import {ok as assert} from 'devlop'
88
import {indexable} from './util.js'
99

10-
/**
11-
* @param {AstRule} query
12-
* Query.
13-
* @param {Node} node
14-
* Node.
15-
* @returns {boolean}
16-
* Whether `node` matches `query`.
17-
*/
18-
export function attributes(query, node) {
19-
let index = -1
20-
21-
if (query.attributes) {
22-
while (++index < query.attributes.length) {
23-
if (!attribute(query.attributes[index], node)) {
24-
return false
25-
}
26-
}
27-
}
28-
29-
return true
30-
}
31-
3210
/**
3311
* @param {AstAttribute} query
3412
* Query.
@@ -38,7 +16,7 @@ export function attributes(query, node) {
3816
* Whether `node` matches `query`.
3917
*/
4018

41-
function attribute(query, node) {
19+
export function attribute(query, node) {
4220
indexable(node)
4321
const value = node[query.name]
4422

lib/pseudo.js

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @typedef {import('css-selector-parser').AstRule} AstRule
32
* @typedef {import('css-selector-parser').AstPseudoClass} AstPseudoClass
43
* @typedef {import('unist').Node} Node
54
* @typedef {import('unist').Parent} Parent
@@ -17,7 +16,7 @@ import {walk} from './walk.js'
1716
const nthCheck = fauxEsmNthCheck.default || fauxEsmNthCheck
1817

1918
/** @type {(rule: AstPseudoClass, node: Node, index: number | undefined, parent: Parent | undefined, state: SelectState) => boolean} */
20-
const handle = zwitch('name', {
19+
export const pseudo = zwitch('name', {
2120
// @ts-expect-error: always known.
2221
unknown: unknownPseudo,
2322
invalid: invalidPseudo,
@@ -42,45 +41,6 @@ const handle = zwitch('name', {
4241
}
4342
})
4443

45-
pseudo.needsIndex = [
46-
'any',
47-
'first-child',
48-
'first-of-type',
49-
'last-child',
50-
'last-of-type',
51-
'is',
52-
'not',
53-
'nth-child',
54-
'nth-last-child',
55-
'nth-of-type',
56-
'nth-last-of-type',
57-
'only-child',
58-
'only-of-type'
59-
]
60-
61-
/**
62-
* Check whether an node matches pseudo selectors.
63-
*
64-
* @param {AstRule} query
65-
* @param {Node} node
66-
* @param {number | undefined} index
67-
* @param {Parent | undefined} parent
68-
* @param {SelectState} state
69-
* @returns {boolean}
70-
*/
71-
export function pseudo(query, node, index, parent, state) {
72-
let offset = -1
73-
74-
if (query.pseudoClasses) {
75-
while (++offset < query.pseudoClasses.length) {
76-
if (!handle(query.pseudoClasses[offset], node, index, parent, state))
77-
return false
78-
}
79-
}
80-
81-
return true
82-
}
83-
8444
/**
8545
* Check whether a node matches an `:empty` pseudo.
8646
*

lib/test.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @typedef {import('./types.js').SelectState} SelectState
66
*/
77

8-
import {attributes} from './attribute.js'
8+
import {attribute} from './attribute.js'
99
import {pseudo} from './pseudo.js'
1010

1111
/**
@@ -17,18 +17,24 @@ import {pseudo} from './pseudo.js'
1717
* @returns {boolean}
1818
*/
1919
export function test(query, node, index, parent, state) {
20-
if (query.ids) throw new Error('Invalid selector: id')
21-
if (query.classNames) throw new Error('Invalid selector: class')
22-
if (query.pseudoElement) {
23-
throw new Error('Invalid selector: `::' + query.pseudoElement + '`')
20+
for (const item of query.items) {
21+
// eslint-disable-next-line unicorn/prefer-switch
22+
if (item.type === 'Attribute') {
23+
if (!attribute(item, node)) return false
24+
} else if (item.type === 'Id') {
25+
throw new Error('Invalid selector: id')
26+
} else if (item.type === 'ClassName') {
27+
throw new Error('Invalid selector: class')
28+
} else if (item.type === 'PseudoClass') {
29+
if (!pseudo(item, node, index, parent, state)) return false
30+
} else if (item.type === 'PseudoElement') {
31+
throw new Error('Invalid selector: `::' + item.name + '`')
32+
} else if (item.type === 'TagName') {
33+
if (item.name !== node.type) return false
34+
} else {
35+
// Otherwise `item.type` is `WildcardTag`, which matches.
36+
}
2437
}
2538

26-
return Boolean(
27-
node &&
28-
(!query.tag ||
29-
query.tag.type === 'WildcardTag' ||
30-
query.tag.name === node.type) &&
31-
(!query.attributes || attributes(query, node)) &&
32-
(!query.pseudoClasses || pseudo(query, node, index, parent, state))
33-
)
39+
return true
3440
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
],
4949
"dependencies": {
5050
"@types/unist": "^3.0.0",
51-
"css-selector-parser": "^2.0.0",
51+
"css-selector-parser": "^3.0.0",
5252
"devlop": "^1.1.0",
5353
"nth-check": "^2.0.0",
5454
"zwitch": "^2.0.0"

0 commit comments

Comments
 (0)