Skip to content

Commit

Permalink
feat(removeDeprecatedAttrs): new removeDeprecatedAttrs plugin
Browse files Browse the repository at this point in the history
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:

```
<svg version="1.1" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="80" height="80"/>
</svg>
```

Becomes:

```
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="80" height="80"/>
</svg>
```

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
  • Loading branch information
jdufresne committed Dec 3, 2023
1 parent d6ff70b commit 09a6402
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/02-preset-default.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
22 changes: 22 additions & 0 deletions docs/03-plugins/remove-deprecated-attrs.mdx
Original file line number Diff line number Diff line change
@@ -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

<PluginUsage/>

## Demo

<PluginDemo/>

## Implementation

* https://github.com/svg/svgo/blob/main/plugins/removeDeprecatedAttrs.js
1 change: 1 addition & 0 deletions lib/builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
59 changes: 59 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ exports.attrsGroupsDefaults = {
* attrsGroups: Array<string>,
* attrs?: Array<string>,
* defaults?: Record<string, string>,
* deprecated?: Array<string>,
* contentGroups?: Array<string>,
* content?: Array<string>,
* }>}
Expand Down Expand Up @@ -1679,6 +1680,64 @@ 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',
'xml:base',
'xml:lang',
'xml:space',
'zoomAndPan',
],
contentGroups: [
'animation',
'descriptive',
Expand Down
1 change: 1 addition & 0 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ type DefaultPlugins = {
removeComments: {
preservePatterns: Array<RegExp | string> | false;
};
removeDeprecatedAttrs: void;
removeDesc: {
removeAny?: boolean;
};
Expand Down
33 changes: 33 additions & 0 deletions plugins/removeDeprecatedAttrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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) {
return;
}

const deprecated = elemConfig.deprecated;
if (!deprecated) {
return;
}

deprecated.forEach((name) => {
delete node.attributes[name];
});
},
},
};
};
13 changes: 13 additions & 0 deletions test/plugins/removeDeprecatedAttrs.01.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 09a6402

Please sign in to comment.