5
5
* @typedef {import('./types.js').SelectState } SelectState
6
6
*/
7
7
8
- import { attributes } from './attribute.js'
8
+ import { attribute } from './attribute.js'
9
9
import { pseudo } from './pseudo.js'
10
10
11
11
/**
@@ -17,18 +17,24 @@ import {pseudo} from './pseudo.js'
17
17
* @returns {boolean }
18
18
*/
19
19
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
+ }
24
37
}
25
38
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
34
40
}
0 commit comments