Skip to content

Commit

Permalink
Refactor to simplify has handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Dec 31, 2022
1 parent 79cf28e commit bbe3479
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 58 deletions.
15 changes: 3 additions & 12 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('./types.js').Selectors} Selectors
* @typedef {import('./types.js').RuleSet} RuleSet
*/

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

/**
* @param {string} selector
* @returns {Selectors}
* @returns {Selectors | RuleSet | undefined}
*/
export function parse(selector) {
if (typeof selector !== 'string') {
throw new TypeError('Expected `string` as selector, not `' + selector + '`')
}

const query = parser.parse(selector)
// Empty selectors object doesn’t match anything.
if (!query) {
return {type: 'selectors', selectors: []}
}

if (query.type === 'selectors') {
return query
}

return {type: 'selectors', selectors: [query]}
return parser.parse(selector)
}
51 changes: 5 additions & 46 deletions lib/pseudo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('./types.js').Rule} Rule
* @typedef {import('./types.js').RuleSet} RuleSet
* @typedef {import('./types.js').RulePseudo} RulePseudo
* @typedef {import('./types.js').RulePseudoSelector} RulePseudoSelector
* @typedef {import('./types.js').Parent} Parent
Expand Down Expand Up @@ -90,8 +91,7 @@ export function pseudo(query, node, index, parent, state) {
* @returns {boolean}
*/
function matches(query, node, _1, _2, state) {
const shallow = state.shallow
const one = state.one
const {shallow, one} = state

state.shallow = false
state.one = true
Expand Down Expand Up @@ -339,17 +339,14 @@ function assertDeep(state, query) {
* @returns {boolean}
*/
function hasSelector(query, node, _1, _2, state) {
const shallow = state.shallow
const one = state.one
const scopeNodes = state.scopeNodes
const value = appendScope(query.value)
const anything = state.any
const fragment = {type: 'root', children: parent(node) ? node.children : []}
const {shallow, one, scopeNodes, any} = state

state.shallow = false
state.one = true
state.scopeNodes = [node]

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

state.shallow = shallow
state.one = one
Expand All @@ -358,44 +355,6 @@ function hasSelector(query, node, _1, _2, state) {
return result
}

/**
* @param {Selector} value
*/
function appendScope(value) {
/** @type {Selectors} */
const selector =
value.type === 'ruleSet' ? {type: 'selectors', selectors: [value]} : value
let index = -1
/** @type {Rule} */
let rule

while (++index < selector.selectors.length) {
rule = selector.selectors[index].rule
rule.nestingOperator = null

// Needed if new pseudo’s are added that accepts commas (such as
// `:lang(en, nl)`)
/* c8 ignore else */
if (
!rule.pseudos ||
rule.pseudos.length !== 1 ||
rule.pseudos[0].name !== 'scope'
) {
selector.selectors[index] = {
type: 'ruleSet',
rule: {
type: 'rule',
rule,
// @ts-expect-error pseudos are fine w/ just a name!
pseudos: [{name: 'scope'}]
}
}
}
}

return selector
}

/**
* @param {RulePseudo} query
* @returns {(value: number) => boolean}
Expand Down

0 comments on commit bbe3479

Please sign in to comment.