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

feat(removeHiddenElems): remove shapes that aren't rendered in any way #1894

Open
wants to merge 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/03-plugins/remove-hidden-elems.mdx
Expand Up @@ -10,6 +10,9 @@ svgo:
displayNone:
description: Removes elements where <a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/display" target="_blank"><code>display</code></a> is <code>none</code>.
default: true
noColor:
description: Removes shapes that aren't rendered in any way (no fill/stroke/markers).
default: true
opacity0:
description: Removes element where <a href="https://developer.mozilla.org/docs/Web/SVG/Attribute/opacity" target="_blank"><code>opacity</code></a> is <code>0</code>.
default: true
Expand Down
1 change: 1 addition & 0 deletions plugins/plugins-types.d.ts
Expand Up @@ -159,6 +159,7 @@ type DefaultPlugins = {
removeHiddenElems: {
isHidden?: boolean;
displayNone?: boolean;
noColor?: boolean;
opacity0?: boolean;
circleR0?: boolean;
ellipseRX0?: boolean;
Expand Down
23 changes: 23 additions & 0 deletions plugins/removeHiddenElems.js
Expand Up @@ -27,6 +27,7 @@ exports.description =
* Remove hidden elements with disabled rendering:
* - display="none"
* - opacity="0"
* - shapes with no fill/stroke/markers
* - circle with zero radius
* - ellipse with zero x-axis or y-axis radius
* - rectangle with zero width or height
Expand All @@ -44,6 +45,7 @@ exports.fn = (root, params) => {
const {
isHidden = true,
displayNone = true,
noColor = true,
opacity0 = true,
circleR0 = true,
ellipseRX0 = true,
Expand Down Expand Up @@ -201,6 +203,27 @@ exports.fn = (root, params) => {
return;
}

// Shapes that aren't rendered in any way
if (
noColor &&
elemsGroups.shape.has(node.name) &&
computedStyle.fill &&
computedStyle.fill.type === 'static' &&
computedStyle.fill.value === 'none' &&
(computedStyle.stroke
? computedStyle.stroke.type === 'static' &&
computedStyle.stroke.value === 'none'
: true) &&
!computedStyle['marker-start'] &&
!computedStyle['marker-mid'] &&
!computedStyle['marker-end'] &&
(parentNode.type !== 'element' ||
(parentNode.name !== 'defs' && parentNode.name !== 'clipPath'))
) {
removeElement(node, parentNode);
return;
}

// Circles with zero radius
//
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
Expand Down
11 changes: 11 additions & 0 deletions test/plugins/removeHiddenElems.16.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.