Skip to content

Commit

Permalink
fix(removeHiddenElems): dont remove node if children have referenced …
Browse files Browse the repository at this point in the history
…id (#1925)
  • Loading branch information
johnkenny54 committed Jan 6, 2024
1 parent 6afc7bd commit 4da8b38
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
26 changes: 18 additions & 8 deletions plugins/removeHiddenElems.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/plugins/removeHiddenElems.16.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/removeHiddenElems.17.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/plugins/removeHiddenElems.18.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4da8b38

Please sign in to comment.