-
-
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 (#1869)
- Loading branch information
Showing
15 changed files
with
416 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,120 @@ | ||
import * as csswhat from 'css-what'; | ||
import { attrsGroupsDeprecated, elems } from './_collections.js'; | ||
import { collectStylesheet } from '../lib/style.js'; | ||
|
||
export const name = 'removeDeprecatedAttrs'; | ||
export const description = 'removes deprecated attributes'; | ||
|
||
/** | ||
* @typedef {{ safe?: Set<string>; unsafe?: Set<string> }} DeprecatedAttrs | ||
* @typedef {import('../lib/types.js').XastElement} XastElement | ||
*/ | ||
|
||
/** | ||
* @param {import('../lib/types.js').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.js').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.js').Plugin<'removeDeprecatedAttrs'>} | ||
*/ | ||
export function 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.