Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d20a835

Browse files
committedNov 6, 2023
Update css-selector-parser
1 parent 3032a27 commit d20a835

File tree

7 files changed

+36
-89
lines changed

7 files changed

+36
-89
lines changed
 

‎lib/attribute.js

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/**
22
* @typedef {import('css-selector-parser').AstAttribute} AstAttribute
3-
* @typedef {import('css-selector-parser').AstRule} AstRule
43
*
54
* @typedef {import('hast').Element} Element
65
* @typedef {import('hast').Properties} Properties
@@ -14,30 +13,6 @@ import {ok as assert} from 'devlop'
1413
import {find} from 'property-information'
1514
import * as spaces from 'space-separated-tokens'
1615

17-
/**
18-
* @param {AstRule} query
19-
* Query.
20-
* @param {Element} element
21-
* Element.
22-
* @param {Schema} schema
23-
* Schema of element.
24-
* @returns {boolean}
25-
* Whether `element` matches `query`.
26-
*/
27-
export function attributes(query, element, schema) {
28-
let index = -1
29-
30-
if (query.attributes) {
31-
while (++index < query.attributes.length) {
32-
if (!attribute(query.attributes[index], element, schema)) {
33-
return false
34-
}
35-
}
36-
}
37-
38-
return true
39-
}
40-
4116
/**
4217
* @param {AstAttribute} query
4318
* Query.
@@ -48,7 +23,7 @@ export function attributes(query, element, schema) {
4823
* @returns {boolean}
4924
* Whether `element` matches `query`.
5025
*/
51-
function attribute(query, element, schema) {
26+
export function attribute(query, element, schema) {
5227
const info = find(schema, query.name)
5328
const propertyValue = element.properties[info.property]
5429
let value = normalizeValue(propertyValue, info)

‎lib/class-name.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {import('css-selector-parser').AstRule} AstRule
2+
* @typedef {import('css-selector-parser').AstClassName} AstClassName
33
* @typedef {import('hast').Element} Element
44
*/
55

@@ -9,7 +9,7 @@ const emptyClassNames = []
99
/**
1010
* Check whether an element has all class names.
1111
*
12-
* @param {AstRule} query
12+
* @param {AstClassName} query
1313
* AST rule (with `classNames`).
1414
* @param {Element} element
1515
* Element.
@@ -21,13 +21,6 @@ export function className(query, element) {
2121
const value = /** @type {Readonly<Array<string>>} */ (
2222
element.properties.className || emptyClassNames
2323
)
24-
let index = -1
2524

26-
if (query.classNames) {
27-
while (++index < query.classNames.length) {
28-
if (!value.includes(query.classNames[index])) return false
29-
}
30-
}
31-
32-
return true
25+
return value.includes(query.name)
3326
}

‎lib/id.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
/**
2-
* @typedef {import('css-selector-parser').AstRule} AstRule
2+
* @typedef {import('css-selector-parser').AstId} AstId
33
*
44
* @typedef {import('hast').Element} Element
55
*/
66

7-
import {ok as assert} from 'devlop'
7+
// Workaround to show references to above types in VS Code.
8+
''
89

910
/**
1011
* Check whether an element has an ID.
1112
*
12-
* @param {AstRule} query
13+
* @param {AstId} query
1314
* AST rule (with `ids`).
1415
* @param {Element} element
1516
* Element.
1617
* @returns {boolean}
1718
* Whether `element` matches `query`.
1819
*/
1920
export function id(query, element) {
20-
const ids = query.ids
21-
assert(ids, 'expected `ids`')
22-
return ids.length === 1 && element.properties.id === ids[0]
21+
return element.properties.id === query.name
2322
}

‎lib/name.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/**
2-
* @typedef {import('css-selector-parser').AstRule} AstRule
2+
* @typedef {import('css-selector-parser').AstTagName} AstTagName
33
*
44
* @typedef {import('hast').Element} Element
55
*/
66

7-
import {ok as assert} from 'devlop'
7+
// Workaround to show references to above types in VS Code.
8+
''
89

910
/**
1011
* Check whether an element has a tag name.
1112
*
12-
* @param {AstRule} query
13+
* @param {AstTagName} query
1314
* AST rule (with `tag`).
1415
* @param {Element} element
1516
* Element.
1617
* @returns {boolean}
1718
* Whether `element` matches `query`.
1819
*/
1920
export function name(query, element) {
20-
assert(query.tag, 'expected `tag`')
21-
return query.tag.type === 'WildcardTag' || query.tag.name === element.tagName
21+
return query.name === element.tagName
2222
}

‎lib/pseudo.js

Lines changed: 2 additions & 31 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
*
54
* @typedef {import('hast').Element} Element
@@ -22,8 +21,8 @@ import {walk} from './walk.js'
2221
// @ts-expect-error: types are broken.
2322
const nthCheck = fauxEsmNthCheck.default || fauxEsmNthCheck
2423

25-
/** @type {(rule: AstPseudoClass | AstRule, element: Element, index: number | undefined, parent: Parents | undefined, state: State) => boolean} */
26-
const handle = zwitch('name', {
24+
/** @type {(rule: AstPseudoClass, element: Element, index: number | undefined, parent: Parents | undefined, state: State) => boolean} */
25+
export const pseudo = zwitch('name', {
2726
handlers: {
2827
'any-link': anyLink,
2928
blank,
@@ -57,34 +56,6 @@ const handle = zwitch('name', {
5756
unknown: unknownPseudo
5857
})
5958

60-
/**
61-
* Check whether an element matches pseudo selectors.
62-
*
63-
* @param {AstRule} query
64-
* AST rule (with `pseudoClasses`).
65-
* @param {Element} element
66-
* Element.
67-
* @param {number | undefined} index
68-
* Index of `element` in `parent`.
69-
* @param {Parents | undefined} parent
70-
* Parent of `element`.
71-
* @param {State} state
72-
* State.
73-
* @returns {boolean}
74-
* Whether `element` matches `query`.
75-
*/
76-
export function pseudo(query, element, index, parent, state) {
77-
const pseudos = query.pseudoClasses
78-
assert(pseudos, 'expected `pseudoClasses`')
79-
let offset = -1
80-
81-
while (++offset < pseudos.length) {
82-
if (!handle(pseudos[offset], element, index, parent, state)) return false
83-
}
84-
85-
return true
86-
}
87-
8859
/**
8960
* Check whether an element matches an `:any-link` pseudo.
9061
*

‎lib/test.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @typedef {import('./index.js').State} State
88
*/
99

10-
import {attributes} from './attribute.js'
10+
import {attribute} from './attribute.js'
1111
import {className} from './class-name.js'
1212
import {id} from './id.js'
1313
import {name} from './name.js'
@@ -30,15 +30,24 @@ import {pseudo} from './pseudo.js'
3030
* Whether `element` matches `query`.
3131
*/
3232
export function test(query, element, index, parent, state) {
33-
if (query.pseudoElement) {
34-
throw new Error('Invalid selector: `::' + query.pseudoElement + '`')
33+
for (const item of query.items) {
34+
// eslint-disable-next-line unicorn/prefer-switch
35+
if (item.type === 'Attribute') {
36+
if (!attribute(item, element, state.schema)) return false
37+
} else if (item.type === 'Id') {
38+
if (!id(item, element)) return false
39+
} else if (item.type === 'ClassName') {
40+
if (!className(item, element)) return false
41+
} else if (item.type === 'PseudoClass') {
42+
if (!pseudo(item, element, index, parent, state)) return false
43+
} else if (item.type === 'PseudoElement') {
44+
throw new Error('Invalid selector: `::' + item.name + '`')
45+
} else if (item.type === 'TagName') {
46+
if (!name(item, element)) return false
47+
} else {
48+
// Otherwise `item.type` is `WildcardTag`, which matches.
49+
}
3550
}
3651

37-
return Boolean(
38-
(!query.tag || name(query, element)) &&
39-
(!query.classNames || className(query, element)) &&
40-
(!query.ids || id(query, element)) &&
41-
(!query.attributes || attributes(query, element, state.schema)) &&
42-
(!query.pseudoClasses || pseudo(query, element, index, parent, state))
43-
)
52+
return true
4453
}

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"@types/unist": "^3.0.0",
4242
"bcp-47-match": "^2.0.0",
4343
"comma-separated-tokens": "^2.0.0",
44-
"css-selector-parser": "^2.0.0",
44+
"css-selector-parser": "^3.0.0",
4545
"devlop": "^1.0.0",
4646
"direction": "^2.0.0",
4747
"hast-util-has-property": "^3.0.0",

0 commit comments

Comments
 (0)
Please sign in to comment.