Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Mar 29, 2023
1 parent 75d0f22 commit 87aef2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/lib/expandApplyAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ function processApply(root, context, localCache) {
// Move pseudo elements to the end of the selector (if necessary)
let selector = parser().astSync(rule.selector)
selector.each((sel) => {
let [pseudoElements] = collectPseudoElements(sel, true, [':is', ':has'])
let [pseudoElements] = collectPseudoElements(sel)
if (pseudoElements.length > 0) {
sel.nodes.push(...pseudoElements.sort(sortSelector))
}
Expand Down
17 changes: 10 additions & 7 deletions src/util/formatVariantSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export function finalizeSelector(current, formats, { context, candidate, base })
selector.each((sel) => {
let [pseudoElements] = collectPseudoElements(sel)
if (pseudoElements.length > 0) {
sel.nodes.push(pseudoElements.sort(sortSelector))
sel.nodes.push(...pseudoElements.sort(sortSelector))
}
})

Expand Down Expand Up @@ -365,15 +365,11 @@ export function containsNode(selector, values) {
* @param {boolean} force
* @param {string[]|null} safelist
**/
export function collectPseudoElements(selector, force = false, safelist = null) {
export function collectPseudoElements(selector, force = false) {
/** @type {Node[]} */
let nodes = []
let seenPseudoElement = null

if (safelist !== null && !containsNode(selector, safelist)) {
return [[], seenPseudoElement]
}

for (let node of [...selector.nodes]) {
if (isPseudoElement(node, force)) {
nodes.push(node)
Expand All @@ -389,7 +385,14 @@ export function collectPseudoElements(selector, force = false, safelist = null)
}

if (node?.nodes) {
let [collected, seenPseudoElementInSelector] = collectPseudoElements(node, force)
let hasPseudoElementRestrictions =
node.type === 'pseudo' && (node.value === ':is' || node.value === ':has')

let [collected, seenPseudoElementInSelector] = collectPseudoElements(
node,
force || hasPseudoElementRestrictions
)

if (seenPseudoElementInSelector) {
seenPseudoElement = seenPseudoElementInSelector
}
Expand Down

0 comments on commit 87aef2f

Please sign in to comment.