From 87aef2f497568c7699cd3309eedf8fa636ab5e58 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Wed, 29 Mar 2023 11:39:14 -0400 Subject: [PATCH] Refactor --- src/lib/expandApplyAtRules.js | 2 +- src/util/formatVariantSelector.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/lib/expandApplyAtRules.js b/src/lib/expandApplyAtRules.js index 836e9ba86c22..c6d53d80e29f 100644 --- a/src/lib/expandApplyAtRules.js +++ b/src/lib/expandApplyAtRules.js @@ -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)) } diff --git a/src/util/formatVariantSelector.js b/src/util/formatVariantSelector.js index 062437061f9d..dd6584db1fcc 100644 --- a/src/util/formatVariantSelector.js +++ b/src/util/formatVariantSelector.js @@ -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)) } }) @@ -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) @@ -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 }