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 8, 2023
1 parent 8390add commit 614bffb
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/03-plugins/remove-deprecated-attrs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Remove Deprecated Attributes
svgo:
pluginId: removeDeprecatedAttrs
---

Remove deprecated attributes from elements in the document.

This plugin does not remove attributes from the deprecated XLink namespace. To remove them, use the [Remove XLink](/docs/plugins/remove-xlink/) 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
75 changes: 75 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,30 @@ exports.attrsGroupsDefaults = {
},
};

/**
* @type {Record<string, Array<string>>}
* @see https://www.w3.org/TR/SVG11/intro.html#Definitions
*/
exports.attrsGroupsDeprecated = {
animationAttributeTarget: ['attributeType'],
conditionalProcessing: ['requiredFeatures'],
core: ['xml:base', 'xml:lang', 'xml:space'],
presentation: [
'clip',
'color-profile',
'enable-background',
'glyph-orientation-horizontal',
'glyph-orientation-vertical',
'kerning',
],
};

/**
* @type {Record<string, {
* attrsGroups: Array<string>,
* attrs?: Array<string>,
* defaults?: Record<string, string>,
* deprecated?: Array<string>,
* contentGroups?: Array<string>,
* content?: Array<string>,
* }>}
Expand Down Expand Up @@ -568,6 +587,7 @@ exports.elems = {
name: 'sRGB',
'rendering-intent': 'auto',
},
deprecated: ['name'],
contentGroups: ['descriptive'],
},
cursor: {
Expand Down Expand Up @@ -937,6 +957,7 @@ exports.elems = {
width: '120%',
height: '120%',
},
deprecated: ['filterRes'],
contentGroups: ['descriptive', 'filterPrimitive'],
content: ['animate', 'set'],
},
Expand All @@ -957,6 +978,13 @@ exports.elems = {
'horiz-origin-x': '0',
'horiz-origin-y': '0',
},
deprecated: [
'horiz-origin-x',
'horiz-origin-y',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
],
contentGroups: ['descriptive'],
content: ['font-face', 'glyph', 'hkern', 'missing-glyph', 'vkern'],
},
Expand Down Expand Up @@ -1007,6 +1035,29 @@ exports.elems = {
'panose-1': '0 0 0 0 0 0 0 0 0 0',
slope: '0',
},
deprecated: [
'accent-height',
'alphabetic',
'ascent',
'bbox',
'cap-height',
'descent',
'hanging',
'ideographic',
'mathematical',
'panose-1',
'slope',
'stemh',
'stemv',
'unicode-range',
'units-per-em',
'v-alphabetic',
'v-hanging',
'v-ideographic',
'v-mathematical',
'widths',
'x-height',
],
contentGroups: ['descriptive'],
content: [
// TODO: "at most one 'font-face-src' element"
Expand All @@ -1017,10 +1068,12 @@ exports.elems = {
'font-face-format': {
attrsGroups: ['core'],
attrs: ['string'],
deprecated: ['string'],
},
'font-face-name': {
attrsGroups: ['core'],
attrs: ['name'],
deprecated: ['name'],
},
'font-face-src': {
attrsGroups: ['core'],
Expand Down Expand Up @@ -1108,6 +1161,16 @@ exports.elems = {
defaults: {
'arabic-form': 'initial',
},
deprecated: [
'arabic-form',
'glyph-name',
'horiz-adv-x',
'orientation',
'unicode',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
],
contentGroups: [
'animation',
'descriptive',
Expand Down Expand Up @@ -1147,6 +1210,7 @@ exports.elems = {
'vert-origin-y',
'vert-adv-y',
],
deprecated: ['horiz-adv-x', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y'],
contentGroups: [
'animation',
'descriptive',
Expand Down Expand Up @@ -1210,6 +1274,7 @@ exports.elems = {
hkern: {
attrsGroups: ['core'],
attrs: ['u1', 'g1', 'u2', 'g2', 'k'],
deprecated: ['g1', 'g2', 'k', 'u1', 'u2'],
},
image: {
attrsGroups: [
Expand Down Expand Up @@ -1404,6 +1469,7 @@ exports.elems = {
'vert-origin-y',
'vert-adv-y',
],
deprecated: ['horiz-adv-x', 'vert-adv-y', 'vert-origin-x', 'vert-origin-y'],
contentGroups: [
'animation',
'descriptive',
Expand Down Expand Up @@ -1679,6 +1745,13 @@ exports.elems = {
contentScriptType: 'application/ecmascript',
contentStyleType: 'text/css',
},
deprecated: [
'baseProfile',
'contentScriptType',
'contentStyleType',
'version',
'zoomAndPan',
],
contentGroups: [
'animation',
'descriptive',
Expand Down Expand Up @@ -1920,11 +1993,13 @@ exports.elems = {
'zoomAndPan',
'viewTarget',
],
deprecated: ['viewTarget', 'zoomAndPan'],
contentGroups: ['descriptive'],
},
vkern: {
attrsGroups: ['core'],
attrs: ['u1', 'g1', 'u2', 'g2', 'k'],
deprecated: ['g1', 'g2', 'k', 'u1', 'u2'],
},
};

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
44 changes: 44 additions & 0 deletions plugins/removeDeprecatedAttrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

const { attrsGroupsDeprecated, 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;
}

elemConfig.attrsGroups.forEach((attrsGroup) => {
const deprecated = attrsGroupsDeprecated[attrsGroup];
if (!deprecated) {
return;
}

deprecated.forEach((name) => {
delete node.attributes[name];
});
});

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

deprecated.forEach((name) => {
delete node.attributes[name];
});
},
},
};
};
16 changes: 16 additions & 0 deletions test/plugins/_collections.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { elems } = require('../../plugins/_collections');

describe('elems.deprecated', () => {
Object.entries(elems).forEach(([tagName, elemConfig]) => {
const deprecated = elemConfig.deprecated;
if (!deprecated) {
return;
}

test(`${tagName} deprecated attributes are all known attributes`, () => {
deprecated.forEach((attr) => {
expect(elemConfig.attrs).toContain(attr);
});
});
});
});
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.
23 changes: 23 additions & 0 deletions test/plugins/removeDeprecatedAttrs.03.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 614bffb

Please sign in to comment.