From fc83e00c6edb54d39e983c3374ae8be475760410 Mon Sep 17 00:00:00 2001 From: John Kenny Date: Mon, 11 Nov 2024 10:37:49 -0800 Subject: [PATCH] Get rid of call to findReferences. --- docs/04-plugins/removeEmptyText.mdx | 24 ------------ lib/builtin.js | 2 - plugins/plugins-types.d.ts | 5 --- plugins/removeEmptyContainers.js | 22 +++++------ plugins/removeEmptyText.js | 49 ------------------------- test/plugins/removeEmptyText.01.svg.txt | 11 ------ test/plugins/removeEmptyText.02.svg.txt | 11 ------ test/plugins/removeEmptyText.03.svg.txt | 11 ------ 8 files changed, 10 insertions(+), 125 deletions(-) delete mode 100644 docs/04-plugins/removeEmptyText.mdx delete mode 100644 plugins/removeEmptyText.js delete mode 100644 test/plugins/removeEmptyText.01.svg.txt delete mode 100644 test/plugins/removeEmptyText.02.svg.txt delete mode 100644 test/plugins/removeEmptyText.03.svg.txt diff --git a/docs/04-plugins/removeEmptyText.mdx b/docs/04-plugins/removeEmptyText.mdx deleted file mode 100644 index 1f1efe2..0000000 --- a/docs/04-plugins/removeEmptyText.mdx +++ /dev/null @@ -1,24 +0,0 @@ ---- -title: removeEmptyText -svgo: - pluginId: removeEmptyText - defaultPlugin: true - parameters: - text: - description: If to remove empty [``](https://developer.mozilla.org/docs/Web/SVG/Element/text) elements. - default: true - tspan: - description: If to remove empty [``](https://developer.mozilla.org/docs/Web/SVG/Element/tspan) elements. - default: true - tref: - description: If to remove empty [``](https://developer.mozilla.org/docs/Web/SVG/Element/tref) elements. - default: true ---- - -Removes empty [``](https://developer.mozilla.org/docs/Web/SVG/Element/text) and [``](https://developer.mozilla.org/docs/Web/SVG/Element/tspan) elements, and [``](https://developer.mozilla.org/docs/Web/SVG/Element/tref) elements that don't reference another node in the document. - -:::info - -No browsers supports ``, so it's best to avoid that element regardless. - -::: diff --git a/lib/builtin.js b/lib/builtin.js index cbfa502..73bb61f 100644 --- a/lib/builtin.js +++ b/lib/builtin.js @@ -38,7 +38,6 @@ import * as removeEditorsNSData from '../plugins/removeEditorsNSData.js'; import * as removeElementsByAttr from '../plugins/removeElementsByAttr.js'; import * as removeEmptyAttrs from '../plugins/removeEmptyAttrs.js'; import * as removeEmptyContainers from '../plugins/removeEmptyContainers.js'; -import * as removeEmptyText from '../plugins/removeEmptyText.js'; import * as removeHiddenElems from '../plugins/removeHiddenElems.js'; import * as removeMetadata from '../plugins/removeMetadata.js'; import * as removeNonInheritableGroupAttrs from '../plugins/removeNonInheritableGroupAttrs.js'; @@ -103,7 +102,6 @@ export const builtin = Object.freeze([ removeElementsByAttr, removeEmptyAttrs, removeEmptyContainers, - removeEmptyText, removeHiddenElems, removeMetadata, removeNonInheritableGroupAttrs, diff --git a/plugins/plugins-types.d.ts b/plugins/plugins-types.d.ts index 23982a2..a651ba1 100644 --- a/plugins/plugins-types.d.ts +++ b/plugins/plugins-types.d.ts @@ -100,11 +100,6 @@ type DefaultPlugins = { }; removeEmptyAttrs: void; removeEmptyContainers: void; - removeEmptyText: { - text?: boolean; - tspan?: boolean; - tref?: boolean; - }; removeHiddenElems: { isHidden?: boolean; displayNone?: boolean; diff --git a/plugins/removeEmptyContainers.js b/plugins/removeEmptyContainers.js index 89d3155..a4005fc 100644 --- a/plugins/removeEmptyContainers.js +++ b/plugins/removeEmptyContainers.js @@ -1,5 +1,5 @@ +import { getHrefId } from '../lib/svgo/tools.js'; import { detachNodeFromParent } from '../lib/xast.js'; -import { findReferences } from '../lib/svgo/tools.js'; export const name = 'removeEmptyContainers'; export const description = 'removes empty container elements'; @@ -35,19 +35,17 @@ export const fn = () => { return { element: { - enter: (node) => { - if (node.name === 'use') { + enter: (element) => { + if (element.name === 'use') { // Record uses so those referencing empty containers can be removed. - for (const [name, value] of Object.entries(node.attributes)) { - const ids = findReferences(name, value); - for (const id of ids) { - let references = usesById.get(id); - if (references === undefined) { - references = []; - usesById.set(id, references); - } - references.push(node); + const id = getHrefId(element); + if (id) { + let references = usesById.get(id); + if (references === undefined) { + references = []; + usesById.set(id, references); } + references.push(element); } } }, diff --git a/plugins/removeEmptyText.js b/plugins/removeEmptyText.js deleted file mode 100644 index 57d72aa..0000000 --- a/plugins/removeEmptyText.js +++ /dev/null @@ -1,49 +0,0 @@ -import { detachNodeFromParent } from '../lib/xast.js'; - -export const name = 'removeEmptyText'; -export const description = 'removes empty elements'; - -/** - * Remove empty Text elements. - * - * @see https://www.w3.org/TR/SVG11/text.html - * - * @example - * Remove empty text element: - * - * - * Remove empty tspan element: - * - * - * Remove tref with empty xlink:href attribute: - * - * - * @author Kir Belevich - * - * @type {import('./plugins-types.js').Plugin<'removeEmptyText'>} - */ -export const fn = (root, params) => { - const { text = true, tspan = true, tref = true } = params; - return { - element: { - enter: (node, parentNode) => { - // Remove empty text element - if (text && node.name === 'text' && node.children.length === 0) { - detachNodeFromParent(node, parentNode); - } - // Remove empty tspan element - if (tspan && node.name === 'tspan' && node.children.length === 0) { - detachNodeFromParent(node, parentNode); - } - // Remove tref with empty xlink:href attribute - if ( - tref && - node.name === 'tref' && - node.attributes['xlink:href'] == null - ) { - detachNodeFromParent(node, parentNode); - } - }, - }, - }; -}; diff --git a/test/plugins/removeEmptyText.01.svg.txt b/test/plugins/removeEmptyText.01.svg.txt deleted file mode 100644 index 4855024..0000000 --- a/test/plugins/removeEmptyText.01.svg.txt +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - -@@@ - - - - diff --git a/test/plugins/removeEmptyText.02.svg.txt b/test/plugins/removeEmptyText.02.svg.txt deleted file mode 100644 index 7908cd4..0000000 --- a/test/plugins/removeEmptyText.02.svg.txt +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - -@@@ - - - - diff --git a/test/plugins/removeEmptyText.03.svg.txt b/test/plugins/removeEmptyText.03.svg.txt deleted file mode 100644 index a88e6e6..0000000 --- a/test/plugins/removeEmptyText.03.svg.txt +++ /dev/null @@ -1,11 +0,0 @@ - - - ... - - - -@@@ - - - -