Skip to content

Commit

Permalink
Merge pull request svg#610 from Velenir/feature/cleanupIDs--force-and…
Browse files Browse the repository at this point in the history
…-except

Allow to force clean svg with style or script, excepting custom ids (in cleanupIDs)
  • Loading branch information
GreLI authored Jan 31, 2017
2 parents ad05378 + f3bff1f commit d4ff31d
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 5 deletions.
15 changes: 10 additions & 5 deletions plugins/cleanupIDs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ exports.description = 'removes unused IDs and minifies used';
exports.params = {
remove: true,
minify: true,
prefix: ''
prefix: '',
force: false,
except: []
};

var referencesProps = require('./_collections').referencesProps,
Expand Down Expand Up @@ -39,7 +41,8 @@ exports.fn = function(data, params) {
IDs = Object.create(null),
referencesIDs = Object.create(null),
idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work
hasStyleOrScript = false;
hasStyleOrScript = false,
exceptIDs = Array.isArray(params.except) ? params.except : [params.except];

/**
* Bananas!
Expand All @@ -55,7 +58,7 @@ exports.fn = function(data, params) {
match;

// check if <style> of <script> presents
if (item.isElem(styleOrScript)) {
if (!params.force && item.isElem(styleOrScript)) {
hasStyleOrScript = true;
continue;
}
Expand Down Expand Up @@ -127,7 +130,7 @@ exports.fn = function(data, params) {
idKey = k;
k = k.replace(idPrefix, '');
// replace referenced IDs with the minified ones
if (params.minify) {
if (params.minify && exceptIDs.indexOf(k) === -1) {
currentIDstring = getIDstring(currentID = generateID(currentID), params);
IDs[idKey].attr('id').value = currentIDstring;

Expand All @@ -149,7 +152,9 @@ exports.fn = function(data, params) {
if (params.remove) {

for(var ID in IDs) {
IDs[ID].removeAttr('id');
if (exceptIDs.indexOf(ID.replace(idPrefix, '')) === -1) {
IDs[ID].removeAttr('id');
}
}

}
Expand Down
19 changes: 19 additions & 0 deletions test/plugins/cleanupIDs.06.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/plugins/cleanupIDs.07.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions test/plugins/cleanupIDs.08.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions test/plugins/cleanupIDs.09.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions test/plugins/cleanupIDs.10.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 d4ff31d

Please sign in to comment.