mirror of
https://github.com/svg/svgo.git
synced 2025-08-01 18:46:52 +03:00
Refactor removeEmptyAttrs (#1594)
- migrated to visitor plugin api - covered with tsdoc
This commit is contained in:
@ -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];
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
@ -1,3 +1,7 @@
|
||||
Removes empty attributes
|
||||
|
||||
===
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg">
|
||||
<g attr1="" attr2=""/>
|
||||
</svg>
|
||||
|
Before Width: | Height: | Size: 138 B After Width: | Height: | Size: 169 B |
@ -27,7 +27,6 @@
|
||||
"plugins/moveGroupAttrsToElems.js",
|
||||
"plugins/plugins.js",
|
||||
"plugins/removeDimensions.js",
|
||||
"plugins/removeEmptyAttrs.js",
|
||||
"plugins/removeNonInheritableGroupAttrs.js",
|
||||
"plugins/removeXMLNS.js",
|
||||
"plugins/inlineStyles.js",
|
||||
|
Reference in New Issue
Block a user