Skip to content

Commit

Permalink
Refactor removeEmptyAttrs (#1594)
Browse files Browse the repository at this point in the history
- migrated to visitor plugin api
- covered with tsdoc
  • Loading branch information
TrySound authored Oct 7, 2021
1 parent 65b6bf4 commit 4377ea3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
38 changes: 19 additions & 19 deletions plugins/removeEmptyAttrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
},
},
};
};
4 changes: 4 additions & 0 deletions test/plugins/removeEmptyAttrs.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4377ea3

Please sign in to comment.