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 has a concept of "safe" and "unsafe" deprecated attributes. A
safe attribute is on that, when removed, does not alter the rendering.
An example is the "version" attribute described above. An unsafe
attribute is one that does change the rendering. By default, the plugin
only removes the safe attributes. The param "removeUnsafe" can be used
to also remove the unsafe ones.

Deprecated attributes may also appear in the SVG stylesheet as a
selector. When this occurs, the attribute won't be removed from the
document to avoid additional rendering changes.

The "xml:lang" attribute is deprecated but also has a equivalent
replacement, "lang", so this attribute is treated as a special case.
Rather than being removed, it will by replaced by its equivalent.

The plugin is built for easy expansion as new deprecated attributes are
discovered or announced. Simply add to ones of the "deprecated" sets in
plugins/_collections.js.

Fixes #1701
  • Loading branch information
jdufresne committed Dec 31, 2023
1 parent 2661dac commit 85ef1cc
Show file tree
Hide file tree
Showing 15 changed files with 417 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/03-plugins/remove-deprecated-attrs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: Remove Deprecated Attributes
svgo:
pluginId: removeDeprecatedAttrs
defaultPlugin: true
parameters:
removeAny:
description: By default, this plugin only removes safe deprecated attributes that do not change the rendered image. Enabling this will remove all deprecated attributes which may impact rendering.
type: boolean
default: false
---

Removes 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
102 changes: 102 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,35 @@ exports.attrsGroupsDefaults = {
},
};

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

/**
* @type {Record<string, {
* attrsGroups: Set<string>,
* attrs?: Set<string>,
* defaults?: Record<string, string>,
* deprecated?: {
* safe?: Set<string>,
* unsafe?: Set<string>,
* },
* contentGroups?: Set<string>,
* content?: Set<string>,
* }>}
Expand Down Expand Up @@ -580,6 +604,7 @@ exports.elems = {
name: 'sRGB',
'rendering-intent': 'auto',
},
deprecated: { unsafe: new Set(['name']) },
contentGroups: new Set(['descriptive']),
},
cursor: {
Expand Down Expand Up @@ -964,6 +989,7 @@ exports.elems = {
width: '120%',
height: '120%',
},
deprecated: { unsafe: new Set(['filterRes']) },
contentGroups: new Set(['descriptive', 'filterPrimitive']),
content: new Set(['animate', 'set']),
},
Expand All @@ -984,6 +1010,15 @@ exports.elems = {
'horiz-origin-x': '0',
'horiz-origin-y': '0',
},
deprecated: {
unsafe: new Set([
'horiz-origin-x',
'horiz-origin-y',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
]),
},
contentGroups: new Set(['descriptive']),
content: new Set(['font-face', 'glyph', 'hkern', 'missing-glyph', 'vkern']),
},
Expand Down Expand Up @@ -1034,6 +1069,31 @@ exports.elems = {
'panose-1': '0 0 0 0 0 0 0 0 0 0',
slope: '0',
},
deprecated: {
unsafe: new Set([
'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: new Set(['descriptive']),
content: new Set([
// TODO: "at most one 'font-face-src' element"
Expand All @@ -1044,10 +1104,12 @@ exports.elems = {
'font-face-format': {
attrsGroups: new Set(['core']),
attrs: new Set(['string']),
deprecated: { unsafe: new Set(['string']) },
},
'font-face-name': {
attrsGroups: new Set(['core']),
attrs: new Set(['name']),
deprecated: { unsafe: new Set(['name']) },
},
'font-face-src': {
attrsGroups: new Set(['core']),
Expand Down Expand Up @@ -1140,6 +1202,18 @@ exports.elems = {
defaults: {
'arabic-form': 'initial',
},
deprecated: {
unsafe: new Set([
'arabic-form',
'glyph-name',
'horiz-adv-x',
'orientation',
'unicode',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
]),
},
contentGroups: new Set([
'animation',
'descriptive',
Expand Down Expand Up @@ -1179,6 +1253,14 @@ exports.elems = {
'vert-origin-x',
'vert-origin-y',
]),
deprecated: {
unsafe: new Set([
'horiz-adv-x',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
]),
},
contentGroups: new Set([
'animation',
'descriptive',
Expand Down Expand Up @@ -1242,6 +1324,7 @@ exports.elems = {
hkern: {
attrsGroups: new Set(['core']),
attrs: new Set(['u1', 'g1', 'u2', 'g2', 'k']),
deprecated: { unsafe: new Set(['g1', 'g2', 'k', 'u1', 'u2']) },
},
image: {
attrsGroups: new Set([
Expand Down Expand Up @@ -1436,6 +1519,14 @@ exports.elems = {
'vert-origin-x',
'vert-origin-y',
]),
deprecated: {
unsafe: new Set([
'horiz-adv-x',
'vert-adv-y',
'vert-origin-x',
'vert-origin-y',
]),
},
contentGroups: new Set([
'animation',
'descriptive',
Expand Down Expand Up @@ -1716,6 +1807,15 @@ exports.elems = {
contentScriptType: 'application/ecmascript',
contentStyleType: 'text/css',
},
deprecated: {
safe: new Set(['version']),
unsafe: new Set([
'baseProfile',
'contentScriptType',
'contentStyleType',
'zoomAndPan',
]),
},
contentGroups: new Set([
'animation',
'descriptive',
Expand Down Expand Up @@ -1962,11 +2062,13 @@ exports.elems = {
'viewTarget',
'zoomAndPan',
]),
deprecated: { unsafe: new Set(['viewTarget', 'zoomAndPan']) },
contentGroups: new Set(['descriptive']),
},
vkern: {
attrsGroups: new Set(['core']),
attrs: new Set(['u1', 'g1', 'u2', 'g2', 'k']),
deprecated: { unsafe: new Set(['g1', 'g2', 'k', 'u1', 'u2']) },
},
};

Expand Down
3 changes: 3 additions & 0 deletions plugins/plugins-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ type DefaultPlugins = {
removeComments: {
preservePatterns: Array<RegExp | string> | false;
};
removeDeprecatedAttrs: {
removeUnsafe?: boolean;
};
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 @@ -5,6 +5,7 @@ const { createPreset } = require('../lib/svgo/plugins.js');
const removeDoctype = require('./removeDoctype.js');
const removeXMLProcInst = require('./removeXMLProcInst.js');
const removeComments = require('./removeComments.js');
const removeDeprecatedAttrs = require('./removeDeprecatedAttrs.js');
const removeMetadata = require('./removeMetadata.js');
const removeEditorsNSData = require('./removeEditorsNSData.js');
const cleanupAttrs = require('./cleanupAttrs.js');
Expand Down Expand Up @@ -44,6 +45,7 @@ const presetDefault = createPreset({
removeDoctype,
removeXMLProcInst,
removeComments,
removeDeprecatedAttrs,
removeMetadata,
removeEditorsNSData,
cleanupAttrs,
Expand Down
122 changes: 122 additions & 0 deletions plugins/removeDeprecatedAttrs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
'use strict';

const csswhat = require('css-what');
const { attrsGroupsDeprecated, elems } = require('./_collections');
const { collectStylesheet } = require('../lib/style');

exports.name = 'removeDeprecatedAttrs';
exports.description = 'removes deprecated attributes';

/**
* @typedef {{ safe?: Set<string>; unsafe?: Set<string> }} DeprecatedAttrs
* @typedef {import('../lib/types').XastElement} XastElement
*/

/**
* @param {import('../lib/types').Stylesheet} stylesheet
* @returns {Set<string>}
*/
function extractAttributesInStylesheet(stylesheet) {
const attributesInStylesheet = new Set();

stylesheet.rules.forEach((rule) => {
const selectors = csswhat.parse(rule.selector);
selectors.forEach((subselector) => {
subselector.forEach((segment) => {
if (segment.type !== 'attribute') {
return;
}

attributesInStylesheet.add(segment.name);
});
});
});

return attributesInStylesheet;
}

/**
* @param {XastElement} node
* @param {DeprecatedAttrs | undefined} deprecatedAttrs
* @param {import('./plugins-types').DefaultPlugins['removeDeprecatedAttrs']} params
* @param {Set<string>} attributesInStylesheet
*/
function processAttributes(
node,
deprecatedAttrs,
params,
attributesInStylesheet,
) {
if (!deprecatedAttrs) {
return;
}

if (deprecatedAttrs.safe) {
deprecatedAttrs.safe.forEach((name) => {
if (attributesInStylesheet.has(name)) {
return;
}
delete node.attributes[name];
});
}

if (params.removeUnsafe && deprecatedAttrs.unsafe) {
deprecatedAttrs.unsafe.forEach((name) => {
if (attributesInStylesheet.has(name)) {
return;
}
delete node.attributes[name];
});
}
}

/**
* Remove deprecated attributes.
*
* @type {import('./plugins-types').Plugin<'removeDeprecatedAttrs'>}
*/
exports.fn = (root, params) => {
const stylesheet = collectStylesheet(root);
const attributesInStylesheet = extractAttributesInStylesheet(stylesheet);

return {
element: {
enter: (node) => {
const elemConfig = elems[node.name];
if (!elemConfig) {
return;
}

// Special cases

// Removing deprecated xml:lang is safe when the lang attribute exists.
if (
elemConfig.attrsGroups.has('core') &&
node.attributes['xml:lang'] &&
!attributesInStylesheet.has('xml:lang') &&
node.attributes['lang']
) {
delete node.attributes['xml:lang'];
}

// General cases

elemConfig.attrsGroups.forEach((attrsGroup) => {
processAttributes(
node,
attrsGroupsDeprecated[attrsGroup],
params,
attributesInStylesheet,
);
});

processAttributes(
node,
elemConfig.deprecated,
params,
attributesInStylesheet,
);
},
},
};
};
Loading

0 comments on commit 85ef1cc

Please sign in to comment.