1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Refactor removeEmptyAttrs (#1594)

- migrated to visitor plugin api
- covered with tsdoc
This commit is contained in:
Bogdan Chadkin
2021-10-07 14:07:06 +03:00
committed by GitHub
parent 65b6bf4c16
commit 4377ea38c4
3 changed files with 23 additions and 20 deletions

View File

@ -2,32 +2,32 @@
const { attrsGroups } = require('./_collections.js');
exports.type = 'visitor';
exports.name = 'removeEmptyAttrs';
exports.type = 'perItem';
exports.active = true;
exports.description = 'removes empty attributes';
/**
* Remove attributes with empty values.
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*
* @type {import('../lib/types').Plugin<void>}
*/
exports.fn = function (item) {
if (item.type === 'element') {
for (const [name, value] of Object.entries(item.attributes)) {
if (
value === '' &&
// empty conditional processing attributes prevents elements from rendering
attrsGroups.conditionalProcessing.includes(name) === false
) {
delete item.attributes[name];
}
}
}
exports.fn = () => {
return {
element: {
enter: (node) => {
for (const [name, value] of Object.entries(node.attributes)) {
if (
value === '' &&
// empty conditional processing attributes prevents elements from rendering
attrsGroups.conditionalProcessing.includes(name) === false
) {
delete node.attributes[name];
}
}
},
},
};
};