Skip to content

Commit

Permalink
new plugin: remove unused IDs (close #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Dec 11, 2012
1 parent b3d8240 commit 7797f68
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .svgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ plugins:
active: true
type: full

- name: removeUnusedIDs
active: true
type: full

svg2js:

strict: true
Expand Down
13 changes: 13 additions & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,19 @@ exports.editorNamespaces = [
'http://ns.adobe.com/XPath/1.0/'
];

// http://www.w3.org/TR/SVG/linking.html#processingIRI
exports.referencesProps = [
'clip-path',
'color-profile',
'fill',
'filter',
'marker-start',
'marker-mid',
'marker-end',
'mask',
'stroke'
];

// http://www.w3.org/TR/SVG/styling.html#SVGStylingProperties
exports.stylingProps = [
'font',
Expand Down
113 changes: 113 additions & 0 deletions plugins/removeUnusedIDs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
'use strict';

var referencesProps = require('./_collections').referencesProps,
regReferencesUrl = /^url\(#(.+?)\)$/,
regReferencesHref = /^#(.+?)$/,
styleOrScript = ['style', 'script'],
hasStyleOrScript = false;

/**
* Remove unused IDs
* (only if there are no any <style> or <script>).
*
* @param {Object} item current iteration item
* @return {Boolean} if false, item will be filtered out
*
* @author Kir Belevich
*/
exports.removeUnusedIDs = function(data) {

var IDs = {},
referencesIDs = [];

/**
* Bananas!
*
* @param {Array} items input items
* @return {Array} output items
*/
function monkeys(items) {

var i = 0,
length = items.content.length;

while(i < length) {

var item = items.content[i],
match;

// check if <style> of <script> presents
if (item.isElem(styleOrScript)) {
hasStyleOrScript = true;
}

// …and don't remove any ID if yes
if (!hasStyleOrScript) {

if (item.isElem()) {

item.eachAttr(function(attr) {
// save IDs
if (attr.name === 'id') {
IDs[item.attr('id').value] = item;
}

// save IDs url() references
else if (referencesProps.indexOf(attr.name) > -1) {
match = attr.value.match(regReferencesUrl);

if (match && referencesIDs.indexOf(match[1]) === -1) {
referencesIDs.push(match[1]);
}
}

// save IDs href references
else if (attr.name === 'xlink:href') {
match = attr.value.match(regReferencesHref);

if (match && referencesIDs.indexOf(match[1]) === -1) {
referencesIDs.push(match[1]);
}
}
});

}

// go deeper
if (item.content) {
monkeys(item);
}

}

i++;

}

return items;

}

data = monkeys(data);

if (!hasStyleOrScript) {

// don't remove referenced IDs
if (referencesIDs.length) {
referencesIDs.forEach(function(referencesID) {
delete IDs[referencesID];
});
}

// remove not referenced IDs from elements
if (Object.keys(IDs).length) {
for(var ID in IDs) {
IDs[ID].removeAttr('id');
}
}

}

return data;

};
15 changes: 15 additions & 0 deletions test/plugins/removeUnusedIDs.01.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/plugins/removeUnusedIDs.02.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/plugins/removeUnusedIDs.02.should.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/plugins/removeUnusedIDs.03.orig.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/plugins/removeUnusedIDs.03.should.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/plugins/removeunusedIDs.01.should.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 7797f68

Please sign in to comment.