1
0
mirror of https://github.com/svg/svgo.git synced 2026-01-27 07:02:06 +03:00
Files
svgo/plugins/removeViewBox.js
Seth Falco 66d503a48c chore: migrate spellchecking from github action to npm task (#2185)
Migrate from the cspell GitHub Action to the npm package.

* Easier to run spellchecking locally.
* More consistent setup between projects on GitHub or other forges.

While I was doing this, I also amended the config to allow "compound
words", then dropped any terms that were already in built-in
dictionaries and that became redundant to add in our config after
enabling compound words.
2025-11-01 22:34:20 +00:00

48 lines
1.3 KiB
JavaScript

export const name = 'removeViewBox';
export const description = 'removes viewBox attribute when possible';
const viewBoxElems = new Set(['pattern', 'svg', 'symbol']);
/**
* Remove viewBox attr which coincides with a width/height box.
*
* @see https://www.w3.org/TR/SVG11/coords.html#ViewBoxAttribute
*
* @example
* <svg width="100" height="50" viewBox="0 0 100 50">
* ⬇
* <svg width="100" height="50">
*
* @author Kir Belevich
*
* @type {import('../lib/types.js').Plugin}
*/
export const fn = () => {
return {
element: {
enter: (node, parentNode) => {
if (
viewBoxElems.has(node.name) &&
node.attributes.viewBox != null &&
node.attributes.width != null &&
node.attributes.height != null
) {
// TODO remove width/height for such case instead
if (node.name === 'svg' && parentNode.type !== 'root') {
return;
}
const numbers = node.attributes.viewBox.split(/[ ,]+/g);
if (
numbers[0] === '0' &&
numbers[1] === '0' &&
node.attributes.width.replace(/px$/, '') === numbers[2] && // could use parseFloat too
node.attributes.height.replace(/px$/, '') === numbers[3]
) {
delete node.attributes.viewBox;
}
}
},
},
};
};