22import walk from 'css-tree/walker'
33import { startsWith , strEquals } from '../string-utils.js'
44import { hasVendorPrefix } from '../vendor-prefix.js'
5+ import { KeywordSet } from '../keyword-set.js'
56import {
67 PseudoClassSelector ,
78 PseudoElementSelector ,
@@ -29,20 +30,17 @@ function analyzeList(selectorListAst, cb) {
2930 return childSelectors
3031}
3132
32- /** @param {string } name */
33- function isPseudoFunction ( name ) {
34- return (
35- strEquals ( name , 'not' )
36- || strEquals ( name , 'nth-child' )
37- || strEquals ( name , 'nth-last-child' )
38- || strEquals ( name , 'where' )
39- || strEquals ( name , 'is' )
40- || strEquals ( name , 'has' )
41- || strEquals ( name , 'matches' )
42- || strEquals ( name , '-webkit-any' )
43- || strEquals ( name , '-moz-any' )
44- )
45- }
33+ const PSEUDO_FUNCTIONS = new KeywordSet ( [
34+ 'nth-child' ,
35+ 'where' ,
36+ 'not' ,
37+ 'is' ,
38+ 'has' ,
39+ 'nth-last-child' ,
40+ 'matches' ,
41+ '-webkit-any' ,
42+ '-moz-any' ,
43+ ] )
4644
4745/** @param {import('css-tree').Selector } selector */
4846export function isAccessibility ( selector ) {
@@ -57,17 +55,17 @@ export function isAccessibility(selector) {
5755 }
5856 }
5957 // Test for [aria-] or [role] inside :is()/:where() and friends
60- else if ( node . type === PseudoClassSelector ) {
61- if ( isPseudoFunction ( node . name ) ) {
62- let list = analyzeList ( node , isAccessibility )
58+ else if ( node . type === PseudoClassSelector && PSEUDO_FUNCTIONS . has ( node . name ) ) {
59+ let list = analyzeList ( node , isAccessibility )
6360
64- if ( list . some ( b => b === true ) ) {
61+ for ( let c of list ) {
62+ if ( c === true ) {
6563 isA11y = true
66- return this . skip
64+ break
6765 }
68-
69- return this . skip
7066 }
67+
68+ return this . skip
7169 }
7270 } )
7371
@@ -82,15 +80,17 @@ export function isPrefixed(selector) {
8280 let isPrefixed = false
8381
8482 walk ( selector , function ( node ) {
85- if ( node . type === PseudoElementSelector
86- || node . type === TypeSelector
87- || node . type === PseudoClassSelector
83+ let type = node . type
84+
85+ if ( type === PseudoElementSelector
86+ || type === TypeSelector
87+ || type === PseudoClassSelector
8888 ) {
8989 if ( hasVendorPrefix ( node . name ) ) {
9090 isPrefixed = true
9191 return this . break
9292 }
93- } else if ( node . type === AttributeSelector ) {
93+ } else if ( type === AttributeSelector ) {
9494 if ( hasVendorPrefix ( node . name . name ) ) {
9595 isPrefixed = true
9696 return this . break
@@ -110,20 +110,21 @@ export function getComplexity(selector) {
110110 let complexity = 0
111111
112112 walk ( selector , function ( node ) {
113- if ( node . type === Selector || node . type === Nth ) return
113+ let type = node . type
114+ if ( type === Selector || type === Nth ) return
114115
115116 complexity ++
116117
117- if ( node . type === PseudoElementSelector
118- || node . type === TypeSelector
119- || node . type === PseudoClassSelector
118+ if ( type === PseudoElementSelector
119+ || type === TypeSelector
120+ || type === PseudoClassSelector
120121 ) {
121122 if ( hasVendorPrefix ( node . name ) ) {
122123 complexity ++
123124 }
124125 }
125126
126- if ( node . type === AttributeSelector ) {
127+ if ( type === AttributeSelector ) {
127128 if ( node . value ) {
128129 complexity ++
129130 }
@@ -133,16 +134,16 @@ export function getComplexity(selector) {
133134 return this . skip
134135 }
135136
136- if ( node . type === PseudoClassSelector ) {
137- if ( isPseudoFunction ( node . name ) ) {
137+ if ( type === PseudoClassSelector ) {
138+ if ( PSEUDO_FUNCTIONS . has ( node . name ) ) {
138139 let list = analyzeList ( node , getComplexity )
139140
140141 // Bail out for empty/non-existent :nth-child() params
141142 if ( list . length === 0 ) return
142143
143- list . forEach ( ( c ) => {
144+ for ( let c of list ) {
144145 complexity += c
145- } )
146+ }
146147 return this . skip
147148 }
148149 }
0 commit comments