Skip to content

Commit bbe3479

Browse files
committed
Refactor to simplify has handling
1 parent 79cf28e commit bbe3479

File tree

2 files changed

+8
-58
lines changed

2 files changed

+8
-58
lines changed

lib/parse.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('./types.js').Selectors} Selectors
3+
* @typedef {import('./types.js').RuleSet} RuleSet
34
*/
45

56
import {CssSelectorParser} from 'css-selector-parser'
@@ -12,22 +13,12 @@ parser.registerNestingOperators('>', '+', '~')
1213

1314
/**
1415
* @param {string} selector
15-
* @returns {Selectors}
16+
* @returns {Selectors | RuleSet | undefined}
1617
*/
1718
export function parse(selector) {
1819
if (typeof selector !== 'string') {
1920
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
2021
}
2122

22-
const query = parser.parse(selector)
23-
// Empty selectors object doesn’t match anything.
24-
if (!query) {
25-
return {type: 'selectors', selectors: []}
26-
}
27-
28-
if (query.type === 'selectors') {
29-
return query
30-
}
31-
32-
return {type: 'selectors', selectors: [query]}
23+
return parser.parse(selector)
3324
}

lib/pseudo.js

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('./types.js').Rule} Rule
3+
* @typedef {import('./types.js').RuleSet} RuleSet
34
* @typedef {import('./types.js').RulePseudo} RulePseudo
45
* @typedef {import('./types.js').RulePseudoSelector} RulePseudoSelector
56
* @typedef {import('./types.js').Parent} Parent
@@ -90,8 +91,7 @@ export function pseudo(query, node, index, parent, state) {
9091
* @returns {boolean}
9192
*/
9293
function matches(query, node, _1, _2, state) {
93-
const shallow = state.shallow
94-
const one = state.one
94+
const {shallow, one} = state
9595

9696
state.shallow = false
9797
state.one = true
@@ -339,17 +339,14 @@ function assertDeep(state, query) {
339339
* @returns {boolean}
340340
*/
341341
function hasSelector(query, node, _1, _2, state) {
342-
const shallow = state.shallow
343-
const one = state.one
344-
const scopeNodes = state.scopeNodes
345-
const value = appendScope(query.value)
346-
const anything = state.any
342+
const fragment = {type: 'root', children: parent(node) ? node.children : []}
343+
const {shallow, one, scopeNodes, any} = state
347344

348345
state.shallow = false
349346
state.one = true
350347
state.scopeNodes = [node]
351348

352-
const result = Boolean(anything(value, node, state)[0])
349+
const result = any(query.value, fragment, state).length > 0
353350

354351
state.shallow = shallow
355352
state.one = one
@@ -358,44 +355,6 @@ function hasSelector(query, node, _1, _2, state) {
358355
return result
359356
}
360357

361-
/**
362-
* @param {Selector} value
363-
*/
364-
function appendScope(value) {
365-
/** @type {Selectors} */
366-
const selector =
367-
value.type === 'ruleSet' ? {type: 'selectors', selectors: [value]} : value
368-
let index = -1
369-
/** @type {Rule} */
370-
let rule
371-
372-
while (++index < selector.selectors.length) {
373-
rule = selector.selectors[index].rule
374-
rule.nestingOperator = null
375-
376-
// Needed if new pseudo’s are added that accepts commas (such as
377-
// `:lang(en, nl)`)
378-
/* c8 ignore else */
379-
if (
380-
!rule.pseudos ||
381-
rule.pseudos.length !== 1 ||
382-
rule.pseudos[0].name !== 'scope'
383-
) {
384-
selector.selectors[index] = {
385-
type: 'ruleSet',
386-
rule: {
387-
type: 'rule',
388-
rule,
389-
// @ts-expect-error pseudos are fine w/ just a name!
390-
pseudos: [{name: 'scope'}]
391-
}
392-
}
393-
}
394-
}
395-
396-
return selector
397-
}
398-
399358
/**
400359
* @param {RulePseudo} query
401360
* @returns {(value: number) => boolean}

0 commit comments

Comments
 (0)