-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(removeDeprecatedAttrs): new removeDeprecatedAttrs plugin
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 replacement, "lang", so this attribute is treated as a special case and is safe when its replacement exists. 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
Showing
15 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}, | ||
}, | ||
}; | ||
}; |
Oops, something went wrong.