1
0
mirror of https://github.com/svg/svgo.git synced 2025-07-29 20:21:14 +03:00

Revert changes in cleanupIDs requiring 0.12. Fixes #602

This commit is contained in:
Lev Solntsev
2016-09-27 11:24:36 +03:00
parent 04fa1d5e83
commit 8abdef7742
2 changed files with 38 additions and 33 deletions

View File

@ -64,7 +64,7 @@
"coveralls": "~2.11.12" "coveralls": "~2.11.12"
}, },
"engines": { "engines": {
"node": ">=0.12.0" "node": ">=0.10.0"
}, },
"license": "MIT" "license": "MIT"
} }

View File

@ -12,7 +12,7 @@ exports.params = {
prefix: '' prefix: ''
}; };
var referencesProps = new Set(require('./_collections').referencesProps), var referencesProps = require('./_collections').referencesProps,
regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/, regReferencesUrl = /\burl\(("|')?#(.+?)\1\)/,
regReferencesHref = /^#(.+?)$/, regReferencesHref = /^#(.+?)$/,
regReferencesBegin = /^(\w+?)\./, regReferencesBegin = /^(\w+?)\./,
@ -36,8 +36,9 @@ exports.fn = function(data, params) {
var currentID, var currentID,
currentIDstring, currentIDstring,
IDs = new Map(), IDs = Object.create(null),
referencesIDs = new Map(), referencesIDs = Object.create(null),
idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work
hasStyleOrScript = false; hasStyleOrScript = false;
/** /**
@ -66,24 +67,24 @@ exports.fn = function(data, params) {
var key; var key;
// save IDs // save IDs
if (attr.name === 'id') { if (attr.name === 'id') {
key = attr.value; key = idPrefix + attr.value;
if (IDs.has(key)) { if (key in IDs) {
item.removeAttr('id'); item.removeAttr('id');
} else { } else {
IDs.set(key, item); IDs[key] = item;
} }
} }
// save IDs url() references // save IDs url() references
else if (referencesProps.has(attr.name)) { else if (referencesProps.indexOf(attr.name) > -1) {
match = attr.value.match(regReferencesUrl); match = attr.value.match(regReferencesUrl);
if (match) { if (match) {
key = match[2]; key = idPrefix + match[2];
if (referencesIDs.has(key)) { if (referencesIDs[key]) {
referencesIDs.get(key).push(attr); referencesIDs[key].push(attr);
} else { } else {
referencesIDs.set(key, [attr]); referencesIDs[key] = [attr];
} }
} }
} }
@ -93,11 +94,11 @@ exports.fn = function(data, params) {
attr.local === 'href' && (match = attr.value.match(regReferencesHref)) || attr.local === 'href' && (match = attr.value.match(regReferencesHref)) ||
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin)) attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
) { ) {
key = match[1]; key = idPrefix + match[1];
if (referencesIDs.has(key)) { if (referencesIDs[key]) {
referencesIDs.get(key).push(attr); referencesIDs[key].push(attr);
} else { } else {
referencesIDs.set(key, [attr]); referencesIDs[key] = [attr];
} }
} }
}); });
@ -120,33 +121,37 @@ exports.fn = function(data, params) {
return data; return data;
} }
for (var ID of referencesIDs) { var idKey;
var key = ID[0], for (var k in referencesIDs) {
references = ID[1]; if (IDs[k]) {
idKey = k;
if (IDs.has(key)) { k = k.replace(idPrefix, '');
// replace referenced IDs with the minified ones // replace referenced IDs with the minified ones
if (params.minify) { if (params.minify) {
currentIDstring = getIDstring(currentID = generateID(currentID), params); currentIDstring = getIDstring(currentID = generateID(currentID), params);
IDs.get(key).attr('id').value = currentIDstring; IDs[idKey].attr('id').value = currentIDstring;
references.forEach(function(attr) { referencesIDs[idKey].forEach(function(attr) {
attr.value = attr.value attr.value = attr.value
.replace('#' + key, '#' + currentIDstring) .replace('#' + k, '#' + currentIDstring)
.replace(key + '.', currentIDstring + '.'); .replace(k + '.', currentIDstring + '.');
}); });
idKey = idPrefix + k;
} }
// don't remove referenced IDs // don't remove referenced IDs
IDs.delete(key); delete IDs[idKey];
} }
} }
// remove non-referenced IDs attributes from elements // remove non-referenced IDs attributes from elements
if (params.remove) { if (params.remove) {
for(var keyElem of IDs) {
keyElem[1].removeAttr('id'); for(var ID in IDs) {
IDs[ID].removeAttr('id');
} }
} }
return data; return data;