2
0
mirror of https://github.com/iconify/iconify.git synced 2025-01-06 07:20:40 +00:00

fix(utils): replacing ids in svg after replacing same ids

This commit is contained in:
Vjacheslav Trushkin 2023-01-02 00:13:11 +02:00
parent b6effea974
commit 2e3ad11248
3 changed files with 37 additions and 5 deletions

View File

@ -3,7 +3,7 @@
"type": "module",
"description": "Common functions for working with Iconify icon sets used by various packages.",
"author": "Vjacheslav Trushkin",
"version": "2.0.8",
"version": "2.0.9",
"license": "MIT",
"bugs": "https://github.com/iconify/iconify/issues",
"homepage": "https://iconify.design/",

View File

@ -49,6 +49,10 @@ export function replaceIDs(
return body;
}
// Random text to make sure there are no conflicts between old and new ids
const suffix =
'suffix' + ((Math.random() * 0x1000000) | Date.now()).toString(16);
// Replace with unique ids
ids.forEach((id) => {
const newID =
@ -62,9 +66,10 @@ export function replaceIDs(
// Allowed characters before id: [#;"]
// Allowed characters after id: [)"], .[a-z]
new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', 'g'),
'$1' + newID + '$3'
'$1' + newID + suffix + '$3'
);
});
body = body.replace(new RegExp(suffix, 'g'), '');
return body;
}

View File

@ -25,9 +25,12 @@ describe('Testing replaceIDs', () => {
// Using callback
let counter = 0;
expect(
replaceIDs(body, () => 'callbackID' + (counter++).toString())
).toBe(
const replaced = replaceIDs(
body,
() => 'callbackID' + (counter++).toString()
);
expect(replaced).toBe(
body
.replace(/ssvg-id-1st-place-medala/g, 'callbackID0')
.replace(/ssvg-id-1st-place-medalb/g, 'callbackID7')
@ -46,6 +49,30 @@ describe('Testing replaceIDs', () => {
.replace(/ssvg-id-1st-place-medalo/g, 'callbackID9')
.replace(/ssvg-id-1st-place-medalp/g, 'callbackID6')
);
// Replacing it again to test double replacement with overlapping numbers
counter = 10;
expect(
replaceIDs(replaced, () => 'callbackID' + (counter++).toString())
).toBe(
body
.replace(/ssvg-id-1st-place-medala/g, 'callbackID10')
.replace(/ssvg-id-1st-place-medalb/g, 'callbackID17')
.replace(/ssvg-id-1st-place-medalc/g, 'callbackID20')
.replace(/ssvg-id-1st-place-medald/g, 'callbackID11')
.replace(/ssvg-id-1st-place-medale/g, 'callbackID21')
.replace(/ssvg-id-1st-place-medalf/g, 'callbackID12')
.replace(/ssvg-id-1st-place-medalg/g, 'callbackID22')
.replace(/ssvg-id-1st-place-medalh/g, 'callbackID13')
.replace(/ssvg-id-1st-place-medali/g, 'callbackID23')
.replace(/ssvg-id-1st-place-medalj/g, 'callbackID14')
.replace(/ssvg-id-1st-place-medalk/g, 'callbackID18')
.replace(/ssvg-id-1st-place-medall/g, 'callbackID24')
.replace(/ssvg-id-1st-place-medalm/g, 'callbackID15')
.replace(/ssvg-id-1st-place-medaln/g, 'callbackID25')
.replace(/ssvg-id-1st-place-medalo/g, 'callbackID19')
.replace(/ssvg-id-1st-place-medalp/g, 'callbackID16')
);
});
test('Matching parts', () => {