mirror of
https://github.com/svg/svgo.git
synced 2025-07-31 07:44:22 +03:00
Replace attributes iterators with for/of (#1431)
These iterators allows to directly manipulate passed value which does not let us to get rid from legacy "attrs" field. Object.entries makes it easier to get an access to both attribute name and value.
This commit is contained in:
@ -117,8 +117,10 @@ exports.fn = function (data, params) {
|
||||
* @return {Array} output items
|
||||
*/
|
||||
function monkeys(items) {
|
||||
for (var i = 0; i < items.children.length && !hasStyleOrScript; i++) {
|
||||
var item = items.children[i];
|
||||
for (const item of items.children) {
|
||||
if (hasStyleOrScript === true) {
|
||||
break;
|
||||
}
|
||||
|
||||
// quit if <style> or <script> present ('force' param prevents quitting)
|
||||
if (!params.force) {
|
||||
@ -144,43 +146,42 @@ exports.fn = function (data, params) {
|
||||
}
|
||||
// …and don't remove any ID if yes
|
||||
if (item.type === 'element') {
|
||||
item.eachAttr(function (attr) {
|
||||
var key, match;
|
||||
for (const [name, value] of Object.entries(item.attributes)) {
|
||||
let key;
|
||||
let match;
|
||||
|
||||
// save IDs
|
||||
if (attr.name === 'id') {
|
||||
key = attr.value;
|
||||
if (name === 'id') {
|
||||
key = value;
|
||||
if (IDs.has(key)) {
|
||||
item.removeAttr('id'); // remove repeated id
|
||||
} else {
|
||||
IDs.set(key, item);
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
// save references
|
||||
const { local } = parseName(name);
|
||||
if (
|
||||
referencesProps.has(name) &&
|
||||
(match = value.match(regReferencesUrl))
|
||||
) {
|
||||
key = match[2]; // url() reference
|
||||
} else if (
|
||||
(local === 'href' && (match = value.match(regReferencesHref))) ||
|
||||
(name === 'begin' && (match = value.match(regReferencesBegin)))
|
||||
) {
|
||||
key = match[1]; // href reference
|
||||
}
|
||||
if (key) {
|
||||
const refs = referencesIDs.get(key) || [];
|
||||
refs.push({ element: item, name, value });
|
||||
referencesIDs.set(key, refs);
|
||||
}
|
||||
}
|
||||
// save references
|
||||
const { local } = parseName(attr.name);
|
||||
if (
|
||||
referencesProps.has(attr.name) &&
|
||||
(match = attr.value.match(regReferencesUrl))
|
||||
) {
|
||||
key = match[2]; // url() reference
|
||||
} else if (
|
||||
(local === 'href' &&
|
||||
(match = attr.value.match(regReferencesHref))) ||
|
||||
(attr.name === 'begin' &&
|
||||
(match = attr.value.match(regReferencesBegin)))
|
||||
) {
|
||||
key = match[1]; // href reference
|
||||
}
|
||||
if (key) {
|
||||
var ref = referencesIDs.get(key) || [];
|
||||
ref.push(attr);
|
||||
referencesIDs.set(key, ref);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
// go deeper
|
||||
if (item.children) {
|
||||
if (item.type === 'root' || item.type === 'element') {
|
||||
monkeys(item);
|
||||
}
|
||||
}
|
||||
@ -196,9 +197,7 @@ exports.fn = function (data, params) {
|
||||
const idPreserved = (id) =>
|
||||
preserveIDs.has(id) || idMatchesPrefix(preserveIDPrefixes, id);
|
||||
|
||||
for (var ref of referencesIDs) {
|
||||
var key = ref[0];
|
||||
|
||||
for (const [key, refs] of referencesIDs) {
|
||||
if (IDs.has(key)) {
|
||||
// replace referenced IDs with the minified ones
|
||||
if (params.minify && !idPreserved(key)) {
|
||||
@ -211,13 +210,13 @@ exports.fn = function (data, params) {
|
||||
|
||||
IDs.get(key).attr('id').value = currentIDstring;
|
||||
|
||||
for (var attr of ref[1]) {
|
||||
attr.value = attr.value.includes(idValuePrefix)
|
||||
? attr.value.replace(
|
||||
for (const { element, name, value } of refs) {
|
||||
element.attributes[name] = value.includes(idValuePrefix)
|
||||
? value.replace(
|
||||
idValuePrefix + key,
|
||||
idValuePrefix + currentIDstring
|
||||
)
|
||||
: attr.value.replace(
|
||||
: value.replace(
|
||||
key + idValuePostfix,
|
||||
currentIDstring + idValuePostfix
|
||||
);
|
||||
|
Reference in New Issue
Block a user