Skip to content

Commit

Permalink
Merge pull request svg#695 from caub/remDefs
Browse files Browse the repository at this point in the history
clean .reduce with side-effects
  • Loading branch information
GreLI authored Apr 2, 2017
2 parents d74f6a0 + 5c2a916 commit 4125d28
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions plugins/removeUselessDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ exports.active = true;

exports.description = 'removes elements in <defs> without id';

var nonRendering = require('./_collections').elemsGroups.nonRendering,
defs;
var nonRendering = require('./_collections').elemsGroups.nonRendering;

/**
* Removes content of defs and properties that aren't rendered directly without ids.
Expand All @@ -21,9 +20,10 @@ exports.fn = function(item) {

if (item.isElem('defs')) {

defs = item;
item.content = (item.content || []).reduce(getUsefulItems, []);

if (item.content) {
item.content = getUsefulItems(item, []);
}

if (item.isEmpty()) return false;

} else if (item.isElem(nonRendering) && !item.hasAttr('id')) {
Expand All @@ -34,18 +34,20 @@ exports.fn = function(item) {

};

function getUsefulItems(usefulItems, item) {
function getUsefulItems(item, usefulItems) {

if (item.hasAttr('id') || item.isElem('style')) {
item.content.forEach(function(child) {
if (child.hasAttr('id') || child.isElem('style')) {

usefulItems.push(item);
item.parentNode = defs;
usefulItems.push(child);
child.parentNode = item;

} else if (!item.isEmpty()) {
} else if (!child.isEmpty()) {

item.content.reduce(getUsefulItems, usefulItems);
child.content = getUsefulItems(child, usefulItems);

}
}
});

return usefulItems;
}

0 comments on commit 4125d28

Please sign in to comment.