Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 11 additions & 2 deletions plugins/collapseGroups.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { computeStyle, collectStylesheet } from '../lib/style.js';
import { inheritableAttrs, elemsGroups } from './_collections.js';

/**
Expand Down Expand Up @@ -50,7 +51,9 @@ const hasAnimatedAttr = (node, name) => {
*
* @type {import('./plugins-types.js').Plugin<'collapseGroups'>}
*/
export const fn = () => {
export const fn = (root) => {
const stylesheet = collectStylesheet(root);

return {
element: {
exit: (node, parentNode) => {
Expand All @@ -68,11 +71,17 @@ export const fn = () => {
node.children.length === 1
) {
const firstChild = node.children[0];
let nodeHasFilter = node.attributes.filter ? true : false;
if (!nodeHasFilter && node.attributes.style) {
if (computeStyle(stylesheet, node)['filter']) {
nodeHasFilter = true;
}
}
// TODO untangle this mess
if (
firstChild.type === 'element' &&
firstChild.attributes.id == null &&
node.attributes.filter == null &&
!nodeHasFilter &&
(node.attributes.class == null ||
firstChild.attributes.class == null) &&
((node.attributes['clip-path'] == null &&
Expand Down
45 changes: 45 additions & 0 deletions test/plugins/collapseGroups.18.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Don't collapse groups if outer group has filter (as style or attribute).

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<clipPath id="a">
<circle cx="25" cy="15" r="10"/>
</clipPath>
<filter id="b">
<feColorMatrix type="saturate"/>
</filter>
<g filter="url(#b)">
<g clip-path="url(#a)">
<circle cx="30" cy="10" r="10" fill="yellow" id="c1"/>
</g>
</g>
<g style="filter:url(#b)">
<g clip-path="url(#a)">
<circle cx="20" cy="10" r="10" fill="blue" id="c2"/>
</g>
</g>
<circle cx="25" cy="15" r="10" stroke="black" stroke-width=".1" fill="none"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<clipPath id="a">
<circle cx="25" cy="15" r="10"/>
</clipPath>
<filter id="b">
<feColorMatrix type="saturate"/>
</filter>
<g filter="url(#b)">
<g clip-path="url(#a)">
<circle cx="30" cy="10" r="10" fill="yellow" id="c1"/>
</g>
</g>
<g style="filter:url(#b)">
<g clip-path="url(#a)">
<circle cx="20" cy="10" r="10" fill="blue" id="c2"/>
</g>
</g>
<circle cx="25" cy="15" r="10" stroke="black" stroke-width=".1" fill="none"/>
</svg>