mirror of
https://github.com/iconify/iconify.git
synced 2025-01-22 14:48:24 +00:00
fix(utils): replacing ids in svg after replacing same ids
This commit is contained in:
parent
b6effea974
commit
2e3ad11248
@ -3,7 +3,7 @@
|
|||||||
"type": "module",
|
"type": "module",
|
||||||
"description": "Common functions for working with Iconify icon sets used by various packages.",
|
"description": "Common functions for working with Iconify icon sets used by various packages.",
|
||||||
"author": "Vjacheslav Trushkin",
|
"author": "Vjacheslav Trushkin",
|
||||||
"version": "2.0.8",
|
"version": "2.0.9",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bugs": "https://github.com/iconify/iconify/issues",
|
"bugs": "https://github.com/iconify/iconify/issues",
|
||||||
"homepage": "https://iconify.design/",
|
"homepage": "https://iconify.design/",
|
||||||
|
@ -49,6 +49,10 @@ export function replaceIDs(
|
|||||||
return body;
|
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
|
// Replace with unique ids
|
||||||
ids.forEach((id) => {
|
ids.forEach((id) => {
|
||||||
const newID =
|
const newID =
|
||||||
@ -62,9 +66,10 @@ export function replaceIDs(
|
|||||||
// Allowed characters before id: [#;"]
|
// Allowed characters before id: [#;"]
|
||||||
// Allowed characters after id: [)"], .[a-z]
|
// Allowed characters after id: [)"], .[a-z]
|
||||||
new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', 'g'),
|
new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', 'g'),
|
||||||
'$1' + newID + '$3'
|
'$1' + newID + suffix + '$3'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
body = body.replace(new RegExp(suffix, 'g'), '');
|
||||||
|
|
||||||
return body;
|
return body;
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,12 @@ describe('Testing replaceIDs', () => {
|
|||||||
|
|
||||||
// Using callback
|
// Using callback
|
||||||
let counter = 0;
|
let counter = 0;
|
||||||
expect(
|
const replaced = replaceIDs(
|
||||||
replaceIDs(body, () => 'callbackID' + (counter++).toString())
|
body,
|
||||||
).toBe(
|
() => 'callbackID' + (counter++).toString()
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(replaced).toBe(
|
||||||
body
|
body
|
||||||
.replace(/ssvg-id-1st-place-medala/g, 'callbackID0')
|
.replace(/ssvg-id-1st-place-medala/g, 'callbackID0')
|
||||||
.replace(/ssvg-id-1st-place-medalb/g, 'callbackID7')
|
.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-medalo/g, 'callbackID9')
|
||||||
.replace(/ssvg-id-1st-place-medalp/g, 'callbackID6')
|
.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', () => {
|
test('Matching parts', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user