Skip to content

Commit

Permalink
fix: collapseGroups does not move attributes atomically (#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Jan 7, 2024
1 parent 6747e3a commit 3e9ad5e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
23 changes: 14 additions & 9 deletions plugins/collapseGroups.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inheritableAttrs, elemsGroups } from './_collections.js';

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

Expand Down Expand Up @@ -80,26 +81,30 @@ export const fn = () => {
node.attributes.transform == null &&
firstChild.attributes.transform == null))
) {
const newChildElemAttrs = { ...firstChild.attributes };

for (const [name, value] of Object.entries(node.attributes)) {
// avoid copying to not conflict with animated attribute
if (hasAnimatedAttr(firstChild, name)) {
return;
}
if (firstChild.attributes[name] == null) {
firstChild.attributes[name] = value;

if (newChildElemAttrs[name] == null) {
newChildElemAttrs[name] = value;
} else if (name === 'transform') {
firstChild.attributes[name] =
value + ' ' + firstChild.attributes[name];
} else if (firstChild.attributes[name] === 'inherit') {
firstChild.attributes[name] = value;
newChildElemAttrs[name] = value + ' ' + newChildElemAttrs[name];
} else if (newChildElemAttrs[name] === 'inherit') {
newChildElemAttrs[name] = value;
} else if (
inheritableAttrs.has(name) === false &&
firstChild.attributes[name] !== value
!inheritableAttrs.has(name) &&
newChildElemAttrs[name] !== value
) {
return;
}
delete node.attributes[name];
}

node.attributes = {};
firstChild.attributes = newChildElemAttrs;
}
}

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>

0 comments on commit 3e9ad5e

Please sign in to comment.