55 * @typedef {import('./types.js').SelectState } SelectState
66 */
77
8- import { attributes } from './attribute.js'
8+ import { attribute } from './attribute.js'
99import { pseudo } from './pseudo.js'
1010
1111/**
@@ -17,18 +17,24 @@ import {pseudo} from './pseudo.js'
1717 * @returns {boolean }
1818 */
1919export 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}
0 commit comments