Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removeHiddenElems is not idempotent #1955

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 72 additions & 11 deletions plugins/removeHiddenElems.js
Expand Up @@ -78,8 +78,11 @@ export const fn = (root, params) => {
*/
const allDefs = new Map();

/** @type {Set<string>} */
const allReferences = new Set();
/** @type {Map<string,Set<XastElement>>} */
const allReferences = new Map();

/** @type {Map<XastElement,string[]>} */
const referencedIdsByNode = new Map();

/**
* @type {Map<string, Array<{ node: XastElement, parentNode: XastParent }>>}
Expand All @@ -97,7 +100,8 @@ export const fn = (root, params) => {
* @returns boolean
*/
function canRemoveNonRenderingNode(node) {
if (allReferences.has(node.attributes.id)) {
const refs = allReferences.get(node.attributes.id);
if (refs && refs.size) {
return false;
}
for (const child of node.children) {
Expand All @@ -108,6 +112,25 @@ export const fn = (root, params) => {
return true;
}

/**
* Retrieve information about all IDs referenced by an element and its children.
* @param {XastElement} node
* @returns {XastElement[]}
*/
function getNodesReferencedByBranch(node) {
const allIds = [];
const thisNodeIds = referencedIdsByNode.get(node);
if (thisNodeIds) {
allIds.push(node);
}
for (const child of node.children) {
if (child.type === 'element') {
allIds.push(...getNodesReferencedByBranch(child));
}
}
return allIds;
}

/**
* @param {XastChild} node
* @param {XastParent} parentNode
Expand Down Expand Up @@ -410,13 +433,26 @@ export const fn = (root, params) => {
return;
}

const allIds = [];
for (const [name, value] of Object.entries(node.attributes)) {
const ids = findReferences(name, value);

// Record which other nodes are referenced by this node.
for (const id of ids) {
allReferences.add(id);
allIds.push(id);
const refs = allReferences.get(id);
if (refs) {
refs.add(node);
} else {
allReferences.set(id, new Set([node]));
}
}
}

// Record which ids are referenced by this node.
if (allIds.length) {
referencedIdsByNode.set(node, allIds);
}
},
},
root: {
Expand All @@ -431,14 +467,39 @@ export const fn = (root, params) => {
}

if (!deoptimized) {
for (const [
nonRenderedNode,
nonRenderedParent,
] of nonRenderedNodes.entries()) {
if (canRemoveNonRenderingNode(nonRenderedNode)) {
detachNodeFromParent(nonRenderedNode, nonRenderedParent);
let tryAgain;
do {
tryAgain = false;
for (const [
nonRenderedNode,
nonRenderedParent,
] of nonRenderedNodes.entries()) {
if (canRemoveNonRenderingNode(nonRenderedNode)) {
detachNodeFromParent(nonRenderedNode, nonRenderedParent);
nonRenderedNodes.delete(nonRenderedNode);

// For any elements referenced by the just-deleted node and its children, remove the node from the list of referencing nodes.
const deletedReferenceNodes =
getNodesReferencedByBranch(nonRenderedNode);
for (const deletedNode of deletedReferenceNodes) {
const referencedIds = referencedIdsByNode.get(deletedNode);
if (referencedIds) {
for (const id of referencedIds) {
const referencingNodes = allReferences.get(id);
if (referencingNodes) {
referencingNodes.delete(deletedNode);
if (referencingNodes.size === 0) {
tryAgain = true;
}
}
}
}
}

nonRenderedNodes.delete(nonRenderedNode);
}
}
}
} while (tryAgain);
}

for (const [node, parentNode] of allDefs.entries()) {
Expand Down
24 changes: 24 additions & 0 deletions test/plugins/removeHiddenElems.20.svg.txt
@@ -0,0 +1,24 @@
Remove all unreferenced elements which are only referenced by other unreferenced elements.

===

<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<clipPath clipPathUnits="userSpaceOnUse" id="clipPath3889">
<path d="M200 200 l50 -300" style="fill:url(#linearGradient3893)"/>
</clipPath>
<linearGradient xlink:href="#linearGradient6131" id="linearGradient3893" gradientUnits="userSpaceOnUse" gradientTransform="scale(2)" x1="86" y1="93" x2="95" y2="102" />
<linearGradient id="linearGradient6131">
<stop id="stop6133" offset="0" style="stop-color:#fcfcfc;stop-opacity:1" />
<stop id="stop6137" offset="1" style="stop-color:#cecbcb;stop-opacity:1" />
</linearGradient>
</defs>
<path d="M200 200 l50 -300" />
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<path d="M200 200 l50 -300"/>
</svg>