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

Disable removeUselessStrokeAndFill if SVG contains animations. #2063

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions plugins/removeUselessStrokeAndFill.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@ export const fn = (root, params) => {
removeNone = false,
} = params;

// style and script elements deoptimise this plugin
let hasStyleOrScript = false;
// style, animation, and script elements deoptimise this plugin
let deoptimized = false;
visit(root, {
element: {
enter: (node) => {
if (node.name === 'style' || hasScripts(node)) {
hasStyleOrScript = true;
if (
node.name === 'style' ||
hasScripts(node) ||
elemsGroups.animation.has(node.name)
) {
deoptimized = true;
}
},
},
});
if (hasStyleOrScript) {
if (deoptimized) {
return null;
}

Expand Down
34 changes: 34 additions & 0 deletions test/plugins/removeUselessStrokeAndFill.06.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Issue 1677: don't remove fill from animated elements.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 24">
<animate href="#a" attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze" />
<animate href="#b" attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze" />
<circle cx="12" cy="12" r="8" fill="red" fill-opacity="0">
<animate attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze" />
</circle>
<circle id="a" cx="36" cy="12" r="8" fill="blue" fill-opacity="0"/>
<g id="b" fill-opacity="0">
<g>
<circle cx="60" cy="12" r="8" fill="green"/>
</g>
</g>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 24">
<animate href="#a" attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze"/>
<animate href="#b" attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze"/>
<circle cx="12" cy="12" r="8" fill="red" fill-opacity="0">
<animate attributeName="fill-opacity" values="0;1" dur="4s" fill="freeze"/>
</circle>
<circle id="a" cx="36" cy="12" r="8" fill="blue" fill-opacity="0"/>
<g id="b" fill-opacity="0">
<g>
<circle cx="60" cy="12" r="8" fill="green"/>
</g>
</g>
</svg>