MCPcopy
hub / github.com/Automattic/juice / removeInlinedSelectorsFromCSS

Function removeInlinedSelectorsFromCSS

lib/utils.js:355–396  ·  view source on GitHub ↗
(css, inlinedSelectors, options, ignoredPseudos)

Source from the content-addressed store, hash-verified

353 */
354
355export function removeInlinedSelectorsFromCSS(css, inlinedSelectors, options, ignoredPseudos) {
356 let root;
357 try {
358 root = parseRoot(css);
359 } catch (e) {
360 return '';
361 }
362
363 const keep = [];
364
365 for (const node of root.nodes) {
366 // Always preserve non-rule types (at-rules, comments)
367 if (node.type !== 'rule') {
368 keep.push(node);
369 continue;
370 }
371
372 // Keep rules with pseudos that were ignored during inlining
373 if (options.preservePseudos && matchesPseudo(node.selectors[0], ignoredPseudos)) {
374 keep.push(node);
375 continue;
376 }
377
378 // Keep rules that match preservedSelectors
379 if (options.preservedSelectors && node.selectors.some((sel) => matchesPreservedSelector(sel, options.preservedSelectors))) {
380 keep.push(node);
381 continue;
382 }
383
384 // Filter out selectors that were inlined
385 const remainingSelectors = node.selectors.filter((sel) => !inlinedSelectors.has(sel));
386
387 if (remainingSelectors.length > 0) {
388 const clone = node.clone();
389 clone.selectors = remainingSelectors;
390 keep.push(clone);
391 }
392 }
393
394 if (keep.length === 0) return '';
395 return stringifyNodes(keep);
396}
397
398/**
399 * Compares two specificity vectors, returning the winning one.

Callers

nothing calls this directly

Calls 4

parseRootFunction · 0.85
matchesPseudoFunction · 0.85
matchesPreservedSelectorFunction · 0.85
stringifyNodesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…