Skip to content

Commit

Permalink
Plugin to remove elements in <defs> without id. Resolves svg#310
Browse files Browse the repository at this point in the history
  • Loading branch information
GreLI committed Mar 28, 2015
1 parent 36003b1 commit c47fa38
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ plugins:
- convertStyleToAttrs
- cleanupIDs
- removeRasterImages
- removeUselessDefs
- cleanupNumericValues
- cleanupListOfValues
- convertColors
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Today we have:
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeMetadata.js) ] remove `<metadata>`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeTitle.js) ] remove `<title>` (disabled by default)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeDesc.js) ] remove `<desc>` (disabled by default)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeUselessDefs.js) ] remove elements of `<defs>` without `id`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeEditorsNSData.js) ] remove editors namespaces, elements and attributes
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeEmptyAttrs.js) ] remove empty attributes
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeHiddenElems.js) ] remove hidden elements
Expand Down
1 change: 1 addition & 0 deletions README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SVGO имеет расширяемую архитектуру, в которой
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeMetadata.js) ] удаление `<metadata>`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeTitle.js) ] удаление `<title>` (отключена по умолчанию)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeDesc.js) ] удаление `<desc>` (отключена по умолчанию)
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeUselessDefs.js) ] удаление элементов в `<defs>` без `id`
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeEditorsNSData.js) ] удаление пространств имён различных редакторов, их элементов и атрибутов
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeEmptyAttrs.js) ] удаление пустых атрибутов
* [ [>](https://github.com/svg/svgo/blob/master/plugins/removeHiddenElems.js) ] удаление скрытых элементов
Expand Down
41 changes: 41 additions & 0 deletions plugins/removeUselessDefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";

exports.type = 'perItem';

exports.active = true;

/**
* Removes content of defs without ids and thus useless.
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author Lev Solntsev
*/
exports.fn = function(item) {

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

if (!item.isEmpty())
item.content = item.content.reduce(getUsefulItems, []);

if (item.isEmpty()) return false;

}

};

function getUsefulItems(usefulItems, item) {

if (item.hasAttr('id')) {

usefulItems.push(item);

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

item.content.reduce(getUsefulItems, usefulItems);

}

return usefulItems;
}
16 changes: 16 additions & 0 deletions test/plugins/removeUselessDefs.01.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c47fa38

Please sign in to comment.