mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Clean up cleanupID logic, use null prototype for maps
Reduce string creation Add test for __proto__
This commit is contained in:
@ -36,8 +36,8 @@ exports.fn = function(data, params) {
|
|||||||
|
|
||||||
var currentID,
|
var currentID,
|
||||||
currentIDstring,
|
currentIDstring,
|
||||||
IDs = {},
|
IDs = Object.create(null),
|
||||||
referencesIDs = {},
|
referencesIDs = Object.create(null),
|
||||||
idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work
|
idPrefix = 'id-', // prefix IDs so that values like '__proto__' don't break the work
|
||||||
hasStyleOrScript = false;
|
hasStyleOrScript = false;
|
||||||
|
|
||||||
@ -57,58 +57,58 @@ exports.fn = function(data, params) {
|
|||||||
// check if <style> of <script> presents
|
// check if <style> of <script> presents
|
||||||
if (item.isElem(styleOrScript)) {
|
if (item.isElem(styleOrScript)) {
|
||||||
hasStyleOrScript = true;
|
hasStyleOrScript = true;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// …and don't remove any ID if yes
|
// …and don't remove any ID if yes
|
||||||
if (!hasStyleOrScript) {
|
if (item.isElem()) {
|
||||||
|
|
||||||
if (item.isElem()) {
|
item.eachAttr(function(attr) {
|
||||||
|
var key;
|
||||||
|
// save IDs
|
||||||
|
if (attr.name === 'id') {
|
||||||
|
key = idPrefix + attr.value;
|
||||||
|
if (key in IDs) {
|
||||||
|
item.removeAttr('id');
|
||||||
|
} else {
|
||||||
|
IDs[key] = item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
item.eachAttr(function(attr) {
|
// save IDs url() references
|
||||||
// save IDs
|
else if (referencesProps.indexOf(attr.name) > -1) {
|
||||||
if (attr.name === 'id') {
|
match = attr.value.match(regReferencesUrl);
|
||||||
if (idPrefix + attr.value in IDs) {
|
|
||||||
item.removeAttr('id');
|
if (match) {
|
||||||
|
key = idPrefix + match[2];
|
||||||
|
if (referencesIDs[key]) {
|
||||||
|
referencesIDs[key].push(attr);
|
||||||
} else {
|
} else {
|
||||||
IDs[idPrefix + attr.value] = item;
|
referencesIDs[key] = [attr];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// save IDs url() references
|
// save IDs href references
|
||||||
else if (referencesProps.indexOf(attr.name) > -1) {
|
else if (
|
||||||
match = attr.value.match(regReferencesUrl);
|
attr.name === 'xlink:href' && (match = attr.value.match(regReferencesHref)) ||
|
||||||
|
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
|
||||||
if (match) {
|
) {
|
||||||
if (referencesIDs[idPrefix + match[2]]) {
|
key = idPrefix + match[1];
|
||||||
referencesIDs[idPrefix + match[2]].push(attr);
|
if (referencesIDs[key]) {
|
||||||
} else {
|
referencesIDs[key].push(attr);
|
||||||
referencesIDs[idPrefix + match[2]] = [attr];
|
} else {
|
||||||
}
|
referencesIDs[key] = [attr];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// save IDs href references
|
});
|
||||||
else if (
|
|
||||||
attr.name === 'xlink:href' && (match = attr.value.match(regReferencesHref)) ||
|
|
||||||
attr.name === 'begin' && (match = attr.value.match(regReferencesBegin))
|
|
||||||
) {
|
|
||||||
if (referencesIDs[idPrefix + match[1]]) {
|
|
||||||
referencesIDs[idPrefix + match[1]].push(attr);
|
|
||||||
} else {
|
|
||||||
referencesIDs[idPrefix + match[1]] = [attr];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// go deeper
|
|
||||||
if (item.content) {
|
|
||||||
monkeys(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go deeper
|
||||||
|
if (item.content) {
|
||||||
|
monkeys(item);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return items;
|
return items;
|
||||||
@ -117,40 +117,39 @@ exports.fn = function(data, params) {
|
|||||||
|
|
||||||
data = monkeys(data);
|
data = monkeys(data);
|
||||||
|
|
||||||
if (!hasStyleOrScript) {
|
if (hasStyleOrScript) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var k in referencesIDs) {
|
||||||
|
if (IDs[k]) {
|
||||||
|
|
||||||
for (var k in referencesIDs) {
|
// replace referenced IDs with the minified ones
|
||||||
if (IDs[k]) {
|
if (params.minify) {
|
||||||
|
|
||||||
// replace referenced IDs with the minified ones
|
currentIDstring = getIDstring(currentID = generateID(currentID), params);
|
||||||
if (params.minify) {
|
IDs[k].attr('id').value = currentIDstring;
|
||||||
|
|
||||||
currentIDstring = getIDstring(currentID = generateID(currentID), params);
|
referencesIDs[k].forEach(function(attr) {
|
||||||
IDs[k].attr('id').value = currentIDstring;
|
k = k.replace(idPrefix, '');
|
||||||
|
attr.value = attr.value
|
||||||
referencesIDs[k].forEach(function(attr) {
|
.replace('#' + k, '#' + currentIDstring)
|
||||||
k = k.replace(idPrefix, '');
|
.replace(k + '.', currentIDstring + '.');
|
||||||
attr.value = attr.value
|
});
|
||||||
.replace('#' + k, '#' + currentIDstring)
|
|
||||||
.replace(k + '.', currentIDstring + '.');
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// don't remove referenced IDs
|
|
||||||
delete IDs[idPrefix + k];
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// don't remove referenced IDs
|
||||||
|
delete IDs[idPrefix + k];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// remove non-referenced IDs attributes from elements
|
// remove non-referenced IDs attributes from elements
|
||||||
if (params.remove) {
|
if (params.remove) {
|
||||||
|
|
||||||
for(var ID in IDs) {
|
|
||||||
IDs[ID].removeAttr('id');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for(var ID in IDs) {
|
||||||
|
IDs[ID].removeAttr('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
<defs>
|
<defs>
|
||||||
<text id="test01">
|
<text id="__proto__">
|
||||||
referenced text
|
referenced text
|
||||||
</text>
|
</text>
|
||||||
<text id="test02">
|
<text id="test02">
|
||||||
@ -160,9 +160,9 @@
|
|||||||
referenced text
|
referenced text
|
||||||
</text>
|
</text>
|
||||||
</defs>
|
</defs>
|
||||||
<tref xlink:href="#test01"/>
|
<tref xlink:href="#__proto__"/>
|
||||||
<tref xlink:href="#test01"/>
|
<tref xlink:href="#__proto__"/>
|
||||||
<tref xlink:href="#test01"/>
|
<tref xlink:href="#__proto__"/>
|
||||||
<tref xlink:href="#test02"/>
|
<tref xlink:href="#test02"/>
|
||||||
<tref xlink:href="#test03"/>
|
<tref xlink:href="#test03"/>
|
||||||
<tref xlink:href="#test04"/>
|
<tref xlink:href="#test04"/>
|
||||||
|
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Reference in New Issue
Block a user