From 5ef2264d7b98c0484adec17c66b1035ae0d2f535 Mon Sep 17 00:00:00 2001 From: John Kenny Date: Thu, 19 Sep 2024 08:01:50 -0700 Subject: [PATCH] Restore logic to not remove defaults from elements with id attributes. --- docs/plugins/removeUnknownsAndDefaults.md | 3 +- plugins/removeUnknownsAndDefaults.js | 8 ++++-- .../removeUnknownsAndDefaults.01.svg.txt | 2 +- .../removeUnknownsAndDefaults.19.svg.txt | 28 +++++++++++++++++++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 test/plugins/removeUnknownsAndDefaults.19.svg.txt diff --git a/docs/plugins/removeUnknownsAndDefaults.md b/docs/plugins/removeUnknownsAndDefaults.md index 9a58f1c..621d3dc 100644 --- a/docs/plugins/removeUnknownsAndDefaults.md +++ b/docs/plugins/removeUnknownsAndDefaults.md @@ -23,4 +23,5 @@ Elements are removed if: Attributes set to a default value are removed if: - They are not overriding a value set by an ancestor, and -- they are not in an element which is referenced by a `` element (or a child of any such element) +- they are not in an element that has an id (e.g., a `linearGradient` or similar element which may be referenced elsewhere) +- they are not in an element which is referenced by a `` element (or a child of any such element). diff --git a/plugins/removeUnknownsAndDefaults.js b/plugins/removeUnknownsAndDefaults.js index 1c2b214..1a42567 100644 --- a/plugins/removeUnknownsAndDefaults.js +++ b/plugins/removeUnknownsAndDefaults.js @@ -197,7 +197,7 @@ export const fn = (root, params, info) => { const allowedChildren = allowedChildrenPerElement.get( parentNode.name, ); - if (allowedChildren == null || allowedChildren.size === 0) { + if (!allowedChildren || allowedChildren.size === 0) { // TODO: DO WE NEED THIS CHECK? SHOULDN'T IT HAVE BEEN HANDLED BY THE PARENT IN THE ELSE BLOCK BELOW? // remove unknown elements if (allowedChildrenPerElement.get(node.name) == null) { @@ -251,8 +251,13 @@ export const fn = (root, params, info) => { ) { delete node.attributes[name]; } + + // Don't remove default attributes from elements with an id attribute; they may be linearGradient, etc. + // where the attribute serves a purpose. If the id is unnecessary, it will be removed by another plugin + // and the attribute will then be removable. if ( defaultAttrs && + !node.attributes.id && attributesDefaults && attributesDefaults.get(name) === value ) { @@ -264,7 +269,6 @@ export const fn = (root, params, info) => { saveForUsageCheck(node, name); } } - // TODO: DO WE NEED THE ID CHECK? SEEMS LIKE THIS IS HANDLED BY CHECKING FOR USE if (uselessOverrides && node.attributes.id == null) { const computedValue = computedParentStyle ? computedParentStyle.get(name) diff --git a/test/plugins/removeUnknownsAndDefaults.01.svg.txt b/test/plugins/removeUnknownsAndDefaults.01.svg.txt index 05fafe4..a8980a5 100644 --- a/test/plugins/removeUnknownsAndDefaults.01.svg.txt +++ b/test/plugins/removeUnknownsAndDefaults.01.svg.txt @@ -7,5 +7,5 @@ - + diff --git a/test/plugins/removeUnknownsAndDefaults.19.svg.txt b/test/plugins/removeUnknownsAndDefaults.19.svg.txt new file mode 100644 index 0000000..3604916 --- /dev/null +++ b/test/plugins/removeUnknownsAndDefaults.19.svg.txt @@ -0,0 +1,28 @@ +Preserve defaults on linearGradient elements. + +=== + + + + + + + + + + + + +@@@ + + + + + + + + + + + +