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 f8e4fa6
Show file tree
Hide file tree
Showing 8 changed files with 131 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
20 changes: 20 additions & 0 deletions docs/03-plugins/remove-deprecated-attrs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Remove Deprecated Attributes
svgo:
pluginId: removeDeprecatedAttrs
defaultPlugin: true
---

Remove deprecated attributes from elements in the document.

## 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
64 changes: 64 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,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',
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
2 changes: 2 additions & 0 deletions plugins/preset-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -68,6 +69,7 @@ const presetDefault = createPreset({
collapseGroups,
convertPathData,
convertTransform,
removeDeprecatedAttrs,
removeEmptyAttrs,
removeEmptyContainers,
mergePaths,
Expand Down
29 changes: 29 additions & 0 deletions plugins/removeDeprecatedAttrs.js
Original file line number Diff line number Diff line change
@@ -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];
});
}
}
},
},
};
};
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.

0 comments on commit f8e4fa6

Please sign in to comment.