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: resolve regression test for removeHiddenElems #1793

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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