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
Changes from 2 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
74 changes: 55 additions & 19 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,16 @@ exports.fn = (root, params) => {
*/
const removedDefIds = new Set();

/**
* @type {Map<string, Array<{node: XastElement, parentNode: XastParent }>>}
KTibow marked this conversation as resolved.
Show resolved Hide resolved
*/
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 @@ -104,6 +115,7 @@ exports.fn = (root, params) => {
nonRenderedNodes.set(node, parentNode);
return visitSkip;
}

const computedStyle = computeStyle(stylesheet, node);
// opacity="0"
//
Expand All @@ -123,6 +135,32 @@ exports.fn = (root, params) => {
return {
element: {
enter: (node, parentNode) => {
if (
(node.name === 'style' && node.children.length !== 0) ||
hasScripts(node)
) {
deoptimized = true;
return;
}

// Store uses so that they can be removed if broken
KTibow marked this conversation as resolved.
Show resolved Hide resolved
// and used to determine if a definition is unused
if (node.name == 'use') {
const reference = Object.keys(node.attributes).find(
KTibow marked this conversation as resolved.
Show resolved Hide resolved
(attr) => attr === 'href' || attr.endsWith('href')
SethFalco marked this conversation as resolved.
Show resolved Hide resolved
);
const referenceValue = reference && node.attributes[reference];
const referenceId = referenceValue && referenceValue.slice(1);
if (referenceId) {
let refs = referencesById.get(referenceId);
if (!refs) {
refs = [];
referencesById.set(referenceId, 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 @@ -356,37 +394,35 @@ exports.fn = (root, params) => {
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: () => {
// Remove uses of deleted definitions
KTibow marked this conversation as resolved.
Show resolved Hide resolved
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') {
// Remove definitions that are unused
KTibow marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
Expand Down
Loading