Skip to content

Commit

Permalink
fix: resolve regression test for removeHiddenElems (#1793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SethFalco authored Sep 25, 2023
1 parent 078a09c commit f07e9de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
45 changes: 43 additions & 2 deletions plugins/removeHiddenElems.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict';

/**
* @typedef {import('../lib/types').XastElement} XastElement
* @typedef {import('../lib/types').XastParent} XastParent
*/

const { elemsGroups, referencesProps } = require('./_collections.js');
const {
visit,
visitSkip,
Expand All @@ -9,6 +15,8 @@ const {
const { collectStylesheet, computeStyle } = require('../lib/style.js');
const { parsePathData } = require('../lib/path.js');

const nonRendering = elemsGroups.nonRendering;

exports.name = 'removeHiddenElems';
exports.description =
'removes hidden elements (zero sized, with absent attributes)';
Expand Down Expand Up @@ -50,11 +58,20 @@ exports.fn = (root, params) => {
} = params;
const stylesheet = collectStylesheet(root);

/**
* Skip non-rendered nodes initially, and only detach if they have no ID, or
* their ID is not referenced by another node.
*
* @type {Map<XastElement, XastParent>}
*/
const nonRenderedNodes = new Map();

visit(root, {
element: {
enter: (node, parentNode) => {
// transparent element inside clipPath still affect clipped elements
if (node.name === 'clipPath') {
// transparent non-rendering elements still apply where referenced
if (nonRendering.includes(node.name)) {
nonRenderedNodes.set(node, parentNode);
return visitSkip;
}
const computedStyle = computeStyle(stylesheet, node);
Expand Down Expand Up @@ -305,6 +322,30 @@ exports.fn = (root, params) => {
return;
}
},

exit: (node, parentNode) => {
if (node.name !== 'svg' || parentNode.type !== 'root') {
return;
}
for (const [
nonRenderedNode,
nonRenderedParent,
] of nonRenderedNodes.entries()) {
if (nonRenderedNode.attributes.id == null) {
detachNodeFromParent(node, nonRenderedParent);
continue;
}

const selector = referencesProps
.map((attr) => `[${attr}="url(#${nonRenderedNode.attributes.id})"]`)
.join(',');

const element = querySelector(root, selector);
if (element == null) {
detachNodeFromParent(node, nonRenderedParent);
}
}
},
},
};
};
2 changes: 0 additions & 2 deletions test/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ const runTests = async ({ list }) => {
if (
// hard to detect the end of animation
name.startsWith('w3c-svg-11-test-suite/svg/animate-') ||
// breaks because of optimisation despite of script
name === 'w3c-svg-11-test-suite/svg/interact-pointer-04-f.svg' ||
// messed gradients
name === 'w3c-svg-11-test-suite/svg/pservers-grad-18-b.svg' ||
// animated filter
Expand Down

0 comments on commit f07e9de

Please sign in to comment.