1
0
mirror of https://github.com/svg/svgo.git synced 2026-01-27 07:02:06 +03:00
Files
svgo/plugins/removeDesc.js
Seth Falco e529c6674d fix(removeDesc): removeAny should be disabled by default (#1806)
As the description of the plugin states, it should only remove the
description if we can see it's just editor attribution, since this
element is used for accessibility.

In general, accessibility is more important than optimizations, so while
I could update the documentation to reflect this behavior, I believe the
default should be changed back.
2023-10-04 11:45:06 +01:00

40 lines
1019 B
JavaScript

'use strict';
const { detachNodeFromParent } = require('../lib/xast.js');
exports.name = 'removeDesc';
exports.description = 'removes <desc>';
const standardDescs = /^(Created with|Created using)/;
/**
* Removes <desc>.
* Removes only standard editors content or empty elements 'cause it can be used for accessibility.
* Enable parameter 'removeAny' to remove any description.
*
* https://developer.mozilla.org/en-US/docs/Web/SVG/Element/desc
*
* @author Daniel Wabyick
*
* @type {import('./plugins-types').Plugin<'removeDesc'>}
*/
exports.fn = (root, params) => {
const { removeAny = false } = params;
return {
element: {
enter: (node, parentNode) => {
if (node.name === 'desc') {
if (
removeAny ||
node.children.length === 0 ||
(node.children[0].type === 'text' &&
standardDescs.test(node.children[0].value))
) {
detachNodeFromParent(node, parentNode);
}
}
},
},
};
};