@iconify/utils
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -9,7 +9,12 @@ "use strict"; | ||
/** | ||
* Regular expression for finding ids in animations | ||
* Match for allowed characters before and after id in replacement, including () for group | ||
*/ | ||
const animationsRegex = /\s(begin|end)="([^"]+)"/g; | ||
const animationValueRegex = /(")/; | ||
const replaceValue = '([^A-Za-z0-9_-])'; | ||
/** | ||
* Escape value for 'new RegExp()' | ||
*/ | ||
function escapeRegExp(str) { | ||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string | ||
} | ||
/** | ||
* New random-ish prefix for ids | ||
@@ -27,16 +32,2 @@ */ | ||
/** | ||
* Replace multiple occurance of same string | ||
*/ | ||
function strReplace(search, replace, subject) { | ||
let pos = 0; | ||
while ((pos = subject.indexOf(search, pos)) !== -1) { | ||
subject = | ||
subject.slice(0, pos) + | ||
replace + | ||
subject.slice(pos + search.length); | ||
pos += replace.length; | ||
} | ||
return subject; | ||
} | ||
/** | ||
* Replace IDs in SVG output with unique IDs | ||
@@ -55,48 +46,6 @@ * Fast replacement without parsing XML, assuming commonly used patterns and clean XML (icon should have been cleaned up with Iconify Tools or SVGO). | ||
} | ||
const animationIDs = []; | ||
const foundAnimations = body.match(animationsRegex); | ||
if (foundAnimations) { | ||
foundAnimations.forEach((item) => { | ||
// Split in 5 parts: tag, quote, value, quote, end | ||
const parts = item.split(animationValueRegex); | ||
if (parts.length !== 5) { | ||
return; | ||
} | ||
// Was it parsed? | ||
const match = parts.join(''); | ||
if (animationIDs.find((item) => item.match === match) !== void 0) { | ||
return; | ||
} | ||
// Get IDs from content by splitting it by ';' and finding text that has more than one part split by '.' | ||
const content = parts[2]; | ||
const ids = new Set(); | ||
content.split(';').forEach((part) => { | ||
const text = part.split('.'); | ||
if (text.length === 2) { | ||
// Got an id! | ||
ids.add(text[0]); | ||
} | ||
}); | ||
// Add match | ||
animationIDs.push({ | ||
match, | ||
ids, | ||
}); | ||
}); | ||
} | ||
// Replace with unique ids | ||
ids.forEach((id) => { | ||
const newID = typeof prefix === 'function' ? prefix() : prefix + counter++; | ||
body = strReplace('="' + id + '"', '="' + newID + '"', body); | ||
body = strReplace('="#' + id + '"', '="#' + newID + '"', body); | ||
body = strReplace('(#' + id + ')', '(#' + newID + ')', body); | ||
// Replace animations | ||
animationIDs.forEach((item) => { | ||
if (item.ids.has(id)) { | ||
const search = item.match; | ||
const replace = strReplace(id, newID, search); | ||
body = strReplace(search, replace, body); | ||
item.match = replace; | ||
} | ||
}); | ||
body = body.replace(new RegExp(replaceValue + '(' + escapeRegExp(id) + ')' + replaceValue, 'g'), '$1' + newID + '$3'); | ||
}); | ||
@@ -103,0 +52,0 @@ return body; |
@@ -5,3 +5,3 @@ { | ||
"author": "Vjacheslav Trushkin", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "bugs": "https://github.com/iconify/iconify/issues", |
Sorry, the diff of this file is not supported yet
109141
1580