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: collapseGroups does not move attributes atomically #1930

Merged
merged 5 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 31 additions & 9 deletions plugins/collapseGroups.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import { inheritableAttrs, elemsGroups } from './_collections.js';

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

export const name = 'collapseGroups';
export const description = 'collapses useless groups';

/**
* @param {XastElement} node
* @param {XastElement} firstChild
* @returns boolean
*/
function canMoveAttributesToSingleChild(node, firstChild) {
for (const [name, value] of Object.entries(node.attributes)) {
// avoid copying to not conflict with animated attribute
if (hasAnimatedAttr(firstChild, name)) {
return false;
}
if (firstChild.attributes[name] == null) {
continue;
} else if (name === 'transform') {
continue;
} else if (firstChild.attributes[name] === 'inherit') {
continue;
} else if (
inheritableAttrs.has(name) === false &&
firstChild.attributes[name] !== value
) {
return false;
}
}
return true;
}

/**
* @type {(node: XastNode, name: string) => boolean}
*/
Expand Down Expand Up @@ -80,23 +108,17 @@ export const fn = () => {
node.attributes.transform == null &&
firstChild.attributes.transform == null))
) {
if (!canMoveAttributesToSingleChild(node, firstChild)) {
return;
}
for (const [name, value] of Object.entries(node.attributes)) {
SethFalco marked this conversation as resolved.
Show resolved Hide resolved
// avoid copying to not conflict with animated attribute
if (hasAnimatedAttr(firstChild, name)) {
return;
}
if (firstChild.attributes[name] == null) {
firstChild.attributes[name] = value;
} else if (name === 'transform') {
firstChild.attributes[name] =
value + ' ' + firstChild.attributes[name];
} else if (firstChild.attributes[name] === 'inherit') {
firstChild.attributes[name] = value;
} else if (
inheritableAttrs.has(name) === false &&
firstChild.attributes[name] !== value
) {
return;
}
delete node.attributes[name];
}
Expand Down
24 changes: 24 additions & 0 deletions test/plugins/collapseGroups.17.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Don't collapse group with a single child unless all attributes can be moved to the child.
See issue #1928 for context.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 88 88">
<filter id="a">
<feGaussianBlur stdDeviation="1"/>
</filter>
<g transform="matrix(0.6875,0,0,0.6875,20.34375,66.34375)" style="filter:url(#a)">
<path d="M 33.346591,-83.471591 L -10.744318,-36.471591 L -10.49989,-32.5" style="fill-opacity:1"/>
</g>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 88 88">
<filter id="a">
<feGaussianBlur stdDeviation="1"/>
</filter>
<g transform="matrix(0.6875,0,0,0.6875,20.34375,66.34375)" style="filter:url(#a)">
<path d="M 33.346591,-83.471591 L -10.744318,-36.471591 L -10.49989,-32.5" style="fill-opacity:1"/>
</g>
</svg>