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): handle defs better #1879

Merged
merged 8 commits into from
Dec 9, 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
88 changes: 63 additions & 25 deletions plugins/removeHiddenElems.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
} = require('../lib/xast.js');
const { collectStylesheet, computeStyle } = require('../lib/style.js');
const { parsePathData } = require('../lib/path.js');
const { hasScripts } = require('../lib/svgo/tools.js');

const nonRendering = elemsGroups.nonRendering;

Expand Down Expand Up @@ -74,6 +75,21 @@ exports.fn = (root, params) => {
*/
const removedDefIds = new Set();

/**
* @type {Map<XastElement, XastParent>}
*/
const allDefs = new Map();

/**
* @type {Map<string, Array<{ node: XastElement, parentNode: XastParent }>>}
*/
const referencesById = new Map();

/**
* If styles are present, we can't be sure if a definition is unused or not
*/
let deoptimized = false;

/**
* @param {XastChild} node
* @param {XastParent} parentNode
Expand Down Expand Up @@ -123,6 +139,33 @@ exports.fn = (root, params) => {
return {
element: {
enter: (node, parentNode) => {
if (
(node.name === 'style' && node.children.length !== 0) ||
hasScripts(node)
) {
deoptimized = true;
return;
}

if (node.name === 'defs') {
allDefs.set(node, parentNode);
}

if (node.name === 'use') {
for (const attr of Object.keys(node.attributes)) {
if (attr !== 'href' && !attr.endsWith(':href')) continue;
const value = node.attributes[attr];
const id = value.slice(1);

let refs = referencesById.get(id);
if (!refs) {
refs = [];
referencesById.set(id, refs);
}
refs.push({ node, parentNode });
}
}

// Removes hidden elements
// https://www.w3schools.com/cssref/pr_class_visibility.asp
const computedStyle = computeStyle(stylesheet, node);
Expand Down Expand Up @@ -350,46 +393,41 @@ exports.fn = (root, params) => {
removeElement(node, parentNode);
}
},

exit: (node, parentNode) => {
if (node.name === 'defs' && node.children.length === 0) {
removeElement(node, parentNode);
return;
}

if (node.name === 'use') {
const referencesRemovedDef = Object.entries(node.attributes).some(
([attrKey, attrValue]) =>
(attrKey === 'href' || attrKey.endsWith(':href')) &&
removedDefIds.has(
attrValue.slice(attrValue.indexOf('#') + 1).trim()
)
);

if (referencesRemovedDef) {
detachNodeFromParent(node, parentNode);
},
root: {
exit: () => {
for (const id of removedDefIds) {
const refs = referencesById.get(id);
if (refs) {
for (const { node, parentNode } of refs) {
detachNodeFromParent(node, parentNode);
}
}

return;
}

if (node.name === 'svg' && parentNode.type === 'root') {
if (!deoptimized) {
KTibow marked this conversation as resolved.
Show resolved Hide resolved
for (const [
nonRenderedNode,
nonRenderedParent,
] of nonRenderedNodes.entries()) {
const id = nonRenderedNode.attributes.id;
const selector = referencesProps
.map(
(attr) => `[${attr}="url(#${nonRenderedNode.attributes.id})"]`
)
.map((attr) => `[${attr}="url(#${id})"]`)
.concat(`[href="#${id}"]`, `[xlink\\:href="#${id}"]`)
.join(',');

const element = querySelector(root, selector);
if (element == null) {
detachNodeFromParent(node, nonRenderedParent);
detachNodeFromParent(nonRenderedNode, nonRenderedParent);
}
}
}

for (const [node, parentNode] of allDefs.entries()) {
if (node.children.length === 0) {
detachNodeFromParent(node, parentNode);
}
}
},
},
};
Expand Down
14 changes: 14 additions & 0 deletions test/plugins/removeHiddenElems.14.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/plugins/removeHiddenElems.15.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.