1
0
mirror of https://github.com/svg/svgo.git synced 2025-08-09 02:22:08 +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'); const { attrsGroups } = require('./_collections.js');
exports.type = 'visitor';
exports.name = 'removeEmptyAttrs'; exports.name = 'removeEmptyAttrs';
exports.type = 'perItem';
exports.active = true; exports.active = true;
exports.description = 'removes empty attributes'; exports.description = 'removes empty attributes';
/** /**
* Remove attributes with empty values. * Remove attributes with empty values.
* *
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich * @author Kir Belevich
*
* @type {import('../lib/types').Plugin<void>}
*/ */
exports.fn = function (item) { exports.fn = () => {
if (item.type === 'element') { return {
for (const [name, value] of Object.entries(item.attributes)) { element: {
enter: (node) => {
for (const [name, value] of Object.entries(node.attributes)) {
if ( if (
value === '' && value === '' &&
// empty conditional processing attributes prevents elements from rendering // empty conditional processing attributes prevents elements from rendering
attrsGroups.conditionalProcessing.includes(name) === false attrsGroups.conditionalProcessing.includes(name) === false
) { ) {
delete item.attributes[name]; delete node.attributes[name];
}
} }
} }
},
},
};
}; };

View File

@@ -1,3 +1,7 @@
Removes empty attributes
===
<svg xmlns="http://www.w3.org/2000/svg"> <svg xmlns="http://www.w3.org/2000/svg">
<g attr1="" attr2=""/> <g attr1="" attr2=""/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 138 B

After

Width:  |  Height:  |  Size: 169 B

View File

@@ -27,7 +27,6 @@
"plugins/moveGroupAttrsToElems.js", "plugins/moveGroupAttrsToElems.js",
"plugins/plugins.js", "plugins/plugins.js",
"plugins/removeDimensions.js", "plugins/removeDimensions.js",
"plugins/removeEmptyAttrs.js",
"plugins/removeNonInheritableGroupAttrs.js", "plugins/removeNonInheritableGroupAttrs.js",
"plugins/removeXMLNS.js", "plugins/removeXMLNS.js",
"plugins/inlineStyles.js", "plugins/inlineStyles.js",