Skip to content

Commit

Permalink
Update css-selector-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Nov 6, 2023
1 parent ad8a91d commit 6f16614
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 78 deletions.
24 changes: 1 addition & 23 deletions lib/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@
import {ok as assert} from 'devlop'
import {indexable} from './util.js'

/**
* @param {AstRule} query
* Query.
* @param {Node} node
* Node.
* @returns {boolean}
* Whether `node` matches `query`.
*/
export function attributes(query, node) {
let index = -1

if (query.attributes) {
while (++index < query.attributes.length) {
if (!attribute(query.attributes[index], node)) {
return false
}
}
}

return true
}

/**
* @param {AstAttribute} query
* Query.
Expand All @@ -38,7 +16,7 @@ export function attributes(query, node) {
* Whether `node` matches `query`.
*/

function attribute(query, node) {
export function attribute(query, node) {
indexable(node)
const value = node[query.name]

Expand Down
42 changes: 1 addition & 41 deletions lib/pseudo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
* @typedef {import('css-selector-parser').AstRule} AstRule
* @typedef {import('css-selector-parser').AstPseudoClass} AstPseudoClass
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
Expand All @@ -17,7 +16,7 @@ import {walk} from './walk.js'
const nthCheck = fauxEsmNthCheck.default || fauxEsmNthCheck

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

pseudo.needsIndex = [
'any',
'first-child',
'first-of-type',
'last-child',
'last-of-type',
'is',
'not',
'nth-child',
'nth-last-child',
'nth-of-type',
'nth-last-of-type',
'only-child',
'only-of-type'
]

/**
* Check whether an node matches pseudo selectors.
*
* @param {AstRule} query
* @param {Node} node
* @param {number | undefined} index
* @param {Parent | undefined} parent
* @param {SelectState} state
* @returns {boolean}
*/
export function pseudo(query, node, index, parent, state) {
let offset = -1

if (query.pseudoClasses) {
while (++offset < query.pseudoClasses.length) {
if (!handle(query.pseudoClasses[offset], node, index, parent, state))
return false
}
}

return true
}

/**
* Check whether a node matches an `:empty` pseudo.
*
Expand Down
32 changes: 19 additions & 13 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @typedef {import('./types.js').SelectState} SelectState
*/

import {attributes} from './attribute.js'
import {attribute} from './attribute.js'
import {pseudo} from './pseudo.js'

/**
Expand All @@ -17,18 +17,24 @@ import {pseudo} from './pseudo.js'
* @returns {boolean}
*/
export function test(query, node, index, parent, state) {
if (query.ids) throw new Error('Invalid selector: id')
if (query.classNames) throw new Error('Invalid selector: class')
if (query.pseudoElement) {
throw new Error('Invalid selector: `::' + query.pseudoElement + '`')
for (const item of query.items) {
// eslint-disable-next-line unicorn/prefer-switch
if (item.type === 'Attribute') {
if (!attribute(item, node)) return false
} else if (item.type === 'Id') {
throw new Error('Invalid selector: id')
} else if (item.type === 'ClassName') {
throw new Error('Invalid selector: class')
} else if (item.type === 'PseudoClass') {
if (!pseudo(item, node, index, parent, state)) return false
} else if (item.type === 'PseudoElement') {
throw new Error('Invalid selector: `::' + item.name + '`')
} else if (item.type === 'TagName') {
if (item.name !== node.type) return false
} else {
// Otherwise `item.type` is `WildcardTag`, which matches.
}
}

return Boolean(
node &&
(!query.tag ||
query.tag.type === 'WildcardTag' ||
query.tag.name === node.type) &&
(!query.attributes || attributes(query, node)) &&
(!query.pseudoClasses || pseudo(query, node, index, parent, state))
)
return true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
],
"dependencies": {
"@types/unist": "^3.0.0",
"css-selector-parser": "^2.0.0",
"css-selector-parser": "^3.0.0",
"devlop": "^1.1.0",
"nth-check": "^2.0.0",
"zwitch": "^2.0.0"
Expand Down

0 comments on commit 6f16614

Please sign in to comment.