@iconify/utils
Advanced tools
Comparing version 1.0.10 to 1.0.11
@@ -16,4 +16,3 @@ /** | ||
* Replace IDs in SVG output with unique IDs | ||
* Fast replacement without parsing XML, assuming commonly used patterns and clean XML (icon should have been cleaned up with Iconify Tools or SVGO). | ||
*/ | ||
export declare function replaceIDs(body: string, prefix?: string | (() => string)): string; | ||
export declare function replaceIDs(body: string, prefix?: string | ((id: string) => string)): string; |
@@ -34,10 +34,3 @@ "use strict"; | ||
/** | ||
* Check if character is a quote | ||
*/ | ||
function isQuote(char) { | ||
return char === '"' || char === "'"; | ||
} | ||
/** | ||
* Replace IDs in SVG output with unique IDs | ||
* Fast replacement without parsing XML, assuming commonly used patterns and clean XML (icon should have been cleaned up with Iconify Tools or SVGO). | ||
*/ | ||
@@ -54,38 +47,10 @@ function replaceIDs(body, prefix = randomPrefix) { | ||
} | ||
// Sort ids by length | ||
ids.sort((a, b) => b.length - a.length); | ||
// Replace with unique ids | ||
ids.forEach((id) => { | ||
const newID = typeof prefix === 'function' ? prefix() : prefix + counter++; | ||
const parts = body.split(id); | ||
let lastPart = parts.shift(); | ||
body = lastPart; | ||
parts.forEach((part) => { | ||
if (!part.length) { | ||
// Two ids in a row? Not possible | ||
body += id + part; | ||
lastPart += id + part; | ||
return; | ||
} | ||
const lastChar = lastPart.slice(lastPart.length - 1); | ||
const nextChar = part.slice(0, 1); | ||
// Test if characters around ID are | ||
function test() { | ||
if (lastChar === '#') { | ||
// xlink:href="#{id}" | ||
// url(#{id}) | ||
return isQuote(nextChar) || nextChar === ')'; | ||
} | ||
const isAnimationDot = nextChar === '.' && !!part.slice(1, 2).match(/\w/); | ||
if (isQuote(lastChar) || lastChar === ';') { | ||
// id="{id}" | ||
// begin="0;{id}.end" (coverts all animations) | ||
return isQuote(nextChar) || isAnimationDot; | ||
} | ||
return false; | ||
} | ||
const success = test(); | ||
body += (success ? newID : id) + part; | ||
lastPart = part; | ||
}); | ||
const newID = typeof prefix === 'function' ? prefix(id) : prefix + counter++; | ||
const escapedID = id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
body = body.replace( | ||
// Allowed characters before id: [#;"] | ||
// Allowed characters after id: [)"], .[a-z] | ||
new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', 'g'), '$1' + newID + '$3'); | ||
}); | ||
@@ -92,0 +57,0 @@ return body; |
@@ -5,3 +5,3 @@ { | ||
"author": "Vjacheslav Trushkin", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"license": "MIT", | ||
@@ -50,2 +50,6 @@ "bugs": "https://github.com/iconify/iconify/issues", | ||
}, | ||
"./lib/icon-set/convert-info": { | ||
"require": "./lib/icon-set/convert-info.js", | ||
"import": "./lib/icon-set/convert-info.mjs" | ||
}, | ||
"./lib/icon-set/expand": { | ||
@@ -52,0 +56,0 @@ "require": "./lib/icon-set/expand.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83184
59