mirror of
https://github.com/svg/svgo.git
synced 2025-07-29 20:21:14 +03:00
Add descendants test and universal selector support
This commit is contained in:
26
lib/xast.js
26
lib/xast.js
@ -30,7 +30,7 @@ const trimQuotes = (string) => {
|
||||
*/
|
||||
const elementMatches = (csstreeNode, xastElement) => {
|
||||
if (csstreeNode.type === 'TypeSelector') {
|
||||
return csstreeNode.name === xastElement.name;
|
||||
return csstreeNode.name === '*' || csstreeNode.name === xastElement.name;
|
||||
}
|
||||
if (csstreeNode.type === 'IdSelector') {
|
||||
return csstreeNode.name === xastElement.attributes.id;
|
||||
@ -90,7 +90,7 @@ const elementMatches = (csstreeNode, xastElement) => {
|
||||
/**
|
||||
* @type {(
|
||||
* startNode: XastParent,
|
||||
* descendants: Array<XastElement>,
|
||||
* descendants: Set<XastElement>,
|
||||
* parents: WeakMap<XastElement, XastParent>
|
||||
* ) => void}
|
||||
*/
|
||||
@ -98,7 +98,7 @@ const collectDescendantElements = (startNode, descendants, parents) => {
|
||||
for (const childNode of startNode.children) {
|
||||
if (childNode.type === 'element') {
|
||||
parents.set(childNode, startNode);
|
||||
descendants.push(childNode);
|
||||
descendants.add(childNode);
|
||||
collectDescendantElements(childNode, descendants, parents);
|
||||
}
|
||||
}
|
||||
@ -107,7 +107,7 @@ const collectDescendantElements = (startNode, descendants, parents) => {
|
||||
/**
|
||||
* @type {(
|
||||
* startNodes: Array<XastElement>,
|
||||
* children: Array<XastElement>,
|
||||
* children: Set<XastElement>,
|
||||
* parents: WeakMap<XastElement, XastParent>
|
||||
* ) => void}
|
||||
*/
|
||||
@ -116,7 +116,7 @@ const collectChildrenElements = (startNodes, children, parents) => {
|
||||
for (const child of startNode.children) {
|
||||
if (child.type === 'element') {
|
||||
parents.set(child, startNode);
|
||||
children.push(child);
|
||||
children.add(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -125,7 +125,7 @@ const collectChildrenElements = (startNodes, children, parents) => {
|
||||
/**
|
||||
* @type {(
|
||||
* startNodes: Array<XastElement>,
|
||||
* siblings: Array<XastElement>,
|
||||
* siblings: Set<XastElement>,
|
||||
* parents: WeakMap<XastElement, XastParent>
|
||||
* ) => void}
|
||||
*/
|
||||
@ -139,7 +139,7 @@ const collectAdjacentSiblings = (startNodes, siblings, parents) => {
|
||||
for (const adjacentSibling of adjacentSiblings) {
|
||||
if (adjacentSibling.type === 'element') {
|
||||
parents.set(adjacentSibling, parentNode);
|
||||
siblings.push(adjacentSibling);
|
||||
siblings.add(adjacentSibling);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,7 +149,7 @@ const collectAdjacentSiblings = (startNodes, siblings, parents) => {
|
||||
/**
|
||||
* @type {(
|
||||
* startNodes: Array<XastElement>,
|
||||
* siblings: Array<XastElement>,
|
||||
* siblings: Set<XastElement>,
|
||||
* parents: WeakMap<XastElement, XastParent>
|
||||
* ) => void}
|
||||
*/
|
||||
@ -163,7 +163,7 @@ const collectGeneralSiblings = (startNodes, siblings, parents) => {
|
||||
for (const generalSibling of generalSiblings) {
|
||||
if (generalSibling.type === 'element') {
|
||||
parents.set(generalSibling, parentNode);
|
||||
siblings.push(generalSibling);
|
||||
siblings.add(generalSibling);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -175,9 +175,9 @@ const collectGeneralSiblings = (startNodes, siblings, parents) => {
|
||||
*/
|
||||
const combination = (csstreeNodes, xastNode) => {
|
||||
/**
|
||||
* @type {Array<XastElement>}
|
||||
* @type {Set<XastElement>}
|
||||
*/
|
||||
let candidateNodes = [];
|
||||
let candidateNodes = new Set();
|
||||
/**
|
||||
* @type {WeakMap<XastElement, XastParent>}
|
||||
*/
|
||||
@ -189,12 +189,12 @@ const combination = (csstreeNodes, xastNode) => {
|
||||
let lastMatchedNodes = [];
|
||||
for (const csstreeChild of csstreeNodes) {
|
||||
if (csstreeChild.type === 'WhiteSpace') {
|
||||
candidateNodes = new Set();
|
||||
for (const node of lastMatchedNodes) {
|
||||
candidateNodes = [];
|
||||
collectDescendantElements(node, candidateNodes, candidateParents);
|
||||
}
|
||||
} else if (csstreeChild.type === 'Combinator') {
|
||||
candidateNodes = [];
|
||||
candidateNodes = new Set();
|
||||
if (csstreeChild.name === '>') {
|
||||
collectChildrenElements(
|
||||
lastMatchedNodes,
|
||||
|
Reference in New Issue
Block a user