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. 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 to the "deprecated" array in
plugins/_collections.js.

Fixes #1701
  • Loading branch information
jdufresne committed Dec 4, 2023
1 parent d6ff70b commit e11379a
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
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
63 changes: 63 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
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
28 changes: 28 additions & 0 deletions plugins/removeDeprecatedAttrs.js
Original file line number Diff line number Diff line change
@@ -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];
});
},
},
};
};
13 changes: 13 additions & 0 deletions test/plugins/removeDeprecatedAttrs.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions test/plugins/removeDeprecatedAttrs.02.svg
Loading
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 e11379a

Please sign in to comment.