1
0
mirror of https://github.com/svg/svgo.git synced 2026-01-27 07:02:06 +03:00

fix(removeHiddenElems): dont remove node if children have referenced id (#1925)

This commit is contained in:
johnkenny54
2024-01-06 06:55:24 -08:00
committed by GitHub
parent 6afc7bdb52
commit 4da8b38fee
4 changed files with 87 additions and 8 deletions

View File

@@ -91,6 +91,23 @@ export const fn = (root, params) => {
*/
let deoptimized = false;
/**
* Nodes can't be removed if they or any of their children have an id attribute that is referenced.
* @param {XastElement} node
* @returns boolean
*/
function canRemoveNonRenderingNode(node) {
if (allReferences.has(node.attributes.id)) {
return false;
}
for (const child of node.children) {
if (child.type === 'element' && !canRemoveNonRenderingNode(child)) {
return false;
}
}
return true;
}
/**
* @param {XastChild} node
* @param {XastParent} parentNode
@@ -113,11 +130,6 @@ export const fn = (root, params) => {
enter: (node, parentNode) => {
// transparent non-rendering elements still apply where referenced
if (nonRendering.has(node.name)) {
if (node.attributes.id == null) {
detachNodeFromParent(node, parentNode);
return visitSkip;
}
nonRenderedNodes.set(node, parentNode);
return visitSkip;
}
@@ -419,9 +431,7 @@ export const fn = (root, params) => {
nonRenderedNode,
nonRenderedParent,
] of nonRenderedNodes.entries()) {
const id = nonRenderedNode.attributes.id;
if (!allReferences.has(id)) {
if (canRemoveNonRenderingNode(nonRenderedNode)) {
detachNodeFromParent(nonRenderedNode, nonRenderedParent);
}
}

View File

@@ -0,0 +1,23 @@
Don't remove non-rendering elements if children have IDs.
===
<svg xmlns="http://www.w3.org/2000/svg">
<rect fill="url(#a)" width="64" height="64"/>
<symbol>
<linearGradient id="a">
<stop offset="5%" stop-color="gold" />
</linearGradient>
</symbol>
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg">
<rect fill="url(#a)" width="64" height="64"/>
<symbol>
<linearGradient id="a">
<stop offset="5%" stop-color="gold"/>
</linearGradient>
</symbol>
</svg>

View File

@@ -0,0 +1,23 @@
Don't remove used symbols.
===
<svg xmlns="http://www.w3.org/2000/svg">
<rect fill="url(#a)" width="64" height="64"/>
<g>
<linearGradient id="a">
<stop offset="5%" stop-color="gold" />
</linearGradient>
</g>
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg">
<rect fill="url(#a)" width="64" height="64"/>
<g>
<linearGradient id="a">
<stop offset="5%" stop-color="gold"/>
</linearGradient>
</g>
</svg>

View File

@@ -0,0 +1,23 @@
Preserve <defs> with referenced path.
===
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="test-body-content">
<defs>
<path id="reference" d="M240 1h239v358H240z"/>
</defs>
<use xlink:href="#reference" id="use" fill="gray" onclick="test(evt)"/>
</g>
</svg>
@@@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="test-body-content">
<defs>
<path id="reference" d="M240 1h239v358H240z"/>
</defs>
<use xlink:href="#reference" id="use" fill="gray" onclick="test(evt)"/>
</g>
</svg>