From e11379a4ee82db886d5959bd110bd1d99352ce70 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2023 08:30:39 -0800 Subject: [PATCH] feat(removeDeprecatedAttrs): new removeDeprecatedAttrs plugin The new removeDeprecatedAttributes removes deprecated attributes from the SVG document. For example, the "version" attribute on the "svg" element is deprecated and not recommended as documented on MDN: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/version > Deprecated: This feature is no longer recommended. Though some > browsers might still support it, it may have already been removed from > the relevant web standards, may be in the process of being dropped, or > may only be kept for compatibility purposes. Avoid using it, and > update existing code if possible; see the compatibility table at the > bottom of this page to guide your decision. Be aware that this feature > may cease to work at any time. The document: ``` ``` Becomes: ``` ``` The plugin is built for easy expansion as new deprecated attributes are discovered or announced. Simply add to the "deprecated" array in plugins/_collections.js. Fixes #1701 --- docs/03-plugins/remove-deprecated-attrs.mdx | 22 +++++++ lib/builtin.js | 1 + plugins/_collections.js | 63 +++++++++++++++++++++ plugins/plugins-types.d.ts | 1 + plugins/removeDeprecatedAttrs.js | 28 +++++++++ test/plugins/removeDeprecatedAttrs.01.svg | 13 +++++ test/plugins/removeDeprecatedAttrs.02.svg | 13 +++++ 7 files changed, 141 insertions(+) create mode 100644 docs/03-plugins/remove-deprecated-attrs.mdx create mode 100644 plugins/removeDeprecatedAttrs.js create mode 100644 test/plugins/removeDeprecatedAttrs.01.svg create mode 100644 test/plugins/removeDeprecatedAttrs.02.svg diff --git a/docs/03-plugins/remove-deprecated-attrs.mdx b/docs/03-plugins/remove-deprecated-attrs.mdx new file mode 100644 index 000000000..203d16ced --- /dev/null +++ b/docs/03-plugins/remove-deprecated-attrs.mdx @@ -0,0 +1,22 @@ +--- +title: Remove Deprecated Attributes +svgo: + pluginId: removeDeprecatedAttrs + defaultPlugin: true +--- + +Remove deprecated attributes from elements in the document. + +This plugin does not remove the deprecated `xlink:*` attributes. To remove them, use the `removeXlink` plugin. + +## Usage + + + +## Demo + + + +## Implementation + +* https://github.com/svg/svgo/blob/main/plugins/removeDeprecatedAttrs.js diff --git a/lib/builtin.js b/lib/builtin.js index 28a380ec9..ce9a43c46 100644 --- a/lib/builtin.js +++ b/lib/builtin.js @@ -27,6 +27,7 @@ exports.builtin = [ require('../plugins/removeAttributesBySelector.js'), require('../plugins/removeAttrs.js'), require('../plugins/removeComments.js'), + require('../plugins/removeDeprecatedAttrs.js'), require('../plugins/removeDesc.js'), require('../plugins/removeDimensions.js'), require('../plugins/removeDoctype.js'), diff --git a/plugins/_collections.js b/plugins/_collections.js index b9e67cb7c..555d35e50 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -2289,3 +2289,66 @@ exports.pseudoClasses = { userAction: ['hover', 'active', 'focus', 'focus-visible', 'focus-within'], functional: ['is', 'not', 'where', 'has'], }; + +/** + * @type {string[]} + * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute + */ +exports.deprecatedAttrs = [ + 'accent-height', + 'alphabetic', + 'arabic-form', + 'ascent', + 'attributeType', + 'baseProfile', + 'bbox', + 'cap-height', + 'clip', + 'color-profile', + 'contentScriptType', + 'contentStyleType', + 'descent', + 'enable-background', + 'filterRes', + 'g1', + 'g2', + 'glyph-name', + 'glyph-orientation-horizontal', + 'glyph-orientation-vertical', + 'hanging', + 'horiz-adv-x', + 'horiz-origin-x', + 'horiz-origin-y', + 'ideographic', + 'k', + 'kerning', + 'mathematical', + 'name', + 'orientation', + 'panose-1', + 'requiredFeatures', + 'slope', + 'stemh', + 'stemv', + 'string', + 'u1', + 'u2', + 'unicode', + 'unicode-range', + 'units-per-em', + 'v-alphabetic', + 'v-hanging', + 'v-ideographic', + 'v-mathematical', + 'version', + 'vert-adv-y', + 'vert-origin-x', + 'vert-origin-y', + 'viewTarget', + 'widths', + 'x-height', + 'xml:base', + 'xml:lang', + 'xml:space', + 'zoomAndPan', +]; diff --git a/plugins/plugins-types.d.ts b/plugins/plugins-types.d.ts index 0499f627f..8a2799b31 100644 --- a/plugins/plugins-types.d.ts +++ b/plugins/plugins-types.d.ts @@ -141,6 +141,7 @@ type DefaultPlugins = { removeComments: { preservePatterns: Array | false; }; + removeDeprecatedAttrs: void; removeDesc: { removeAny?: boolean; }; diff --git a/plugins/removeDeprecatedAttrs.js b/plugins/removeDeprecatedAttrs.js new file mode 100644 index 000000000..0ad0ce3fe --- /dev/null +++ b/plugins/removeDeprecatedAttrs.js @@ -0,0 +1,28 @@ +'use strict'; + +const { deprecatedAttrs, elems } = require('./_collections'); + +exports.name = 'removeDeprecatedAttrs'; +exports.description = 'removes deprecated attributes'; + +/** + * Remove deprecated attributes. + * + * @type {import('./plugins-types').Plugin<'removeDeprecatedAttrs'>} + */ +exports.fn = () => { + return { + element: { + enter: (node) => { + const elemConfig = elems[node.name]; + if (!elemConfig) { + return; + } + + deprecatedAttrs.forEach((name) => { + delete node.attributes[name]; + }); + }, + }, + }; +}; diff --git a/test/plugins/removeDeprecatedAttrs.01.svg b/test/plugins/removeDeprecatedAttrs.01.svg new file mode 100644 index 000000000..b5e92a964 --- /dev/null +++ b/test/plugins/removeDeprecatedAttrs.01.svg @@ -0,0 +1,13 @@ +Removes deprecated attributes + +=== + + + + + +@@@ + + + + diff --git a/test/plugins/removeDeprecatedAttrs.02.svg b/test/plugins/removeDeprecatedAttrs.02.svg new file mode 100644 index 000000000..2604bec0d --- /dev/null +++ b/test/plugins/removeDeprecatedAttrs.02.svg @@ -0,0 +1,13 @@ +Removes deprecated viewTarget attribute from view node. + +=== + + + + + +@@@ + + + +