From f8e4fa6371d9652b36d0f22f8dd1db9a5330790b 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 that have no effect on rendering. 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 a "deprecated" key to the elems data structure in plugins/_collections.js. Fixes #1701 --- docs/02-preset-default.mdx | 1 + docs/03-plugins/remove-deprecated-attrs.mdx | 20 +++++++ lib/builtin.js | 1 + plugins/_collections.js | 64 +++++++++++++++++++++ plugins/plugins-types.d.ts | 1 + plugins/preset-default.js | 2 + plugins/removeDeprecatedAttrs.js | 29 ++++++++++ test/plugins/removeDeprecatedAttrs.01.svg | 13 +++++ 8 files changed, 131 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 diff --git a/docs/02-preset-default.mdx b/docs/02-preset-default.mdx index f8d45c72c..343888de3 100644 --- a/docs/02-preset-default.mdx +++ b/docs/02-preset-default.mdx @@ -41,6 +41,7 @@ The following plugins are included in `preset-default`, in the order that they'r * [Collapse Groups](/docs/plugins/collapse-groups/) * [Convert Path Data](/docs/plugins/convert-path-data/) * [Convert Transform](/docs/plugins/convert-transform/) +* [Remove Deprecated Attributes](/docs/plugins/remove-deprecated-attrs/) * [Remove Empty Attributes](/docs/plugins/remove-empty-attrs/) * [Remove Empty Containers](/docs/plugins/remove-empty-containers/) * [Remove Unused Namespaces](/docs/plugins/remove-unused-namespaces/) diff --git a/docs/03-plugins/remove-deprecated-attrs.mdx b/docs/03-plugins/remove-deprecated-attrs.mdx new file mode 100644 index 000000000..4fe63b6bb --- /dev/null +++ b/docs/03-plugins/remove-deprecated-attrs.mdx @@ -0,0 +1,20 @@ +--- +title: Remove Deprecated Attributes +svgo: + pluginId: removeDeprecatedAttrs + defaultPlugin: true +--- + +Remove deprecated attributes from elements in the document. + +## 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..27c758d14 100644 --- a/plugins/_collections.js +++ b/plugins/_collections.js @@ -374,6 +374,7 @@ exports.attrsGroupsDefaults = { * attrsGroups: Array, * attrs?: Array, * defaults?: Record, + * deprecated?: Array, * contentGroups?: Array, * content?: Array, * }>} @@ -1679,6 +1680,69 @@ exports.elems = { contentScriptType: 'application/ecmascript', contentStyleType: 'text/css', }, + deprecated: [ + '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', + 'xlink:arcrole', + 'xlink:href', + 'xlink:show', + 'xlink:title', + 'xlink:type', + 'xml:base', + 'xml:lang', + 'xml:space', + 'zoomAndPan', + ], contentGroups: [ 'animation', 'descriptive', 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/preset-default.js b/plugins/preset-default.js index 86516fd72..442bdc3da 100644 --- a/plugins/preset-default.js +++ b/plugins/preset-default.js @@ -15,6 +15,7 @@ const cleanupIds = require('./cleanupIds.js'); const removeUselessDefs = require('./removeUselessDefs.js'); const cleanupNumericValues = require('./cleanupNumericValues.js'); const convertColors = require('./convertColors.js'); +const removeDeprecatedAttrs = require('./removeDeprecatedAttrs.js'); const removeUnknownsAndDefaults = require('./removeUnknownsAndDefaults.js'); const removeNonInheritableGroupAttrs = require('./removeNonInheritableGroupAttrs.js'); const removeUselessStrokeAndFill = require('./removeUselessStrokeAndFill.js'); @@ -68,6 +69,7 @@ const presetDefault = createPreset({ collapseGroups, convertPathData, convertTransform, + removeDeprecatedAttrs, removeEmptyAttrs, removeEmptyContainers, mergePaths, diff --git a/plugins/removeDeprecatedAttrs.js b/plugins/removeDeprecatedAttrs.js new file mode 100644 index 000000000..78089e137 --- /dev/null +++ b/plugins/removeDeprecatedAttrs.js @@ -0,0 +1,29 @@ +'use strict'; + +const { 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) { + const deprecated = elemConfig.deprecated; + if (deprecated) { + deprecated.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 + +=== + + + + + +@@@ + + + +