Skip to content

Commit 29ee4e9

Browse files
author
Kalle Ott
committed
fix: only use IE11 compatible APIs
1 parent c900765 commit 29ee4e9

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "react-lazy-svg",
3+
"version": "1.1.1",
34
"license": "MIT",
45
"repository": {
56
"url": "https://github.com/kaoDev/react-lazy-svg"
@@ -9,7 +10,6 @@
910
"url": "https://github.com/kaoDev/"
1011
},
1112
"description": "react-lazy-svg is a simple way to use SVGs with the performance benefits of a sprite-sheet and svg css styling possibilities. Without bloating the bundle. It automatically creates a sprite-sheet for all used SVGs on the client but also provides a function to create a server side rendered sprite-sheet for icons used in the first paint.",
12-
"version": "1.1.0",
1313
"main": "dist/index.js",
1414
"module": "dist/react-lazy-svg.esm.js",
1515
"typings": "dist/index.d.ts",
@@ -50,7 +50,8 @@
5050
"react-dom": "^16.13.1",
5151
"tsdx": "^0.13.2",
5252
"tslib": "^2.0.0",
53-
"typescript": "^3.9.5"
53+
"typescript": "^3.9.5",
54+
"w3c-xmlserializer": "^2.0.0"
5455
},
5556
"dependencies": {}
5657
}

src/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,16 @@ export const initOnClient = (
251251
) => {
252252
knownIcons.clear();
253253
const spriteSheet = document.getElementById(spriteSheetId);
254-
255254
if (spriteSheet) {
256-
const sprites = spriteSheet.querySelectorAll('symbol');
255+
const serializer = new XMLSerializer();
256+
const sprites = Array.from(spriteSheet.querySelectorAll('symbol'));
257+
258+
for (const node of sprites) {
259+
const innerHTML = Array.prototype.map
260+
.call(node.childNodes, (child) => serializer.serializeToString(child))
261+
.join('');
257262

258-
sprites.forEach((node) => {
259-
const { id, attributes: rawAttributes, innerHTML } = node;
263+
const { id, attributes: rawAttributes } = node;
260264
const attributes = mapNodeAttributes(rawAttributes);
261265
const iconData = {
262266
id,
@@ -266,6 +270,6 @@ export const initOnClient = (
266270
addIcon(iconData);
267271

268272
knownIcons.set(id, new Promise((resolve) => resolve(iconData)));
269-
});
273+
}
270274
}
271275
};

test/index.test.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ test('should fill the cache when an icon is rendered', async () => {
5959
});
6060

6161
test('client should be able to initiate the cache from a rendered dom', async () => {
62+
const serialize = require('w3c-xmlserializer');
63+
64+
window.XMLSerializer = class XMLSerializer {
65+
serializeToString = serialize;
66+
};
67+
6268
const cache: IconsCache = new Map();
6369
document.body.innerHTML = `<svg id="__SVG_SPRITE_SHEET__" style="display:none">
6470
<symbol
@@ -80,6 +86,6 @@ test('client should be able to initiate the cache from a rendered dom', async ()
8086
expect(iconData?.attributes.width).toBe(undefined);
8187
expect(iconData?.attributes.viewBox).toBe('0 0 24 24');
8288
expect(iconData?.svgString.__html).toBe(
83-
'<path d="M0 0h24v24H0z" fill="none"></path>',
89+
'<path xmlns="http://www.w3.org/2000/svg" d="M0 0h24v24H0z" fill="none"/>',
8490
);
8591
});

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5967,6 +5967,13 @@ w3c-hr-time@^1.0.1:
59675967
dependencies:
59685968
browser-process-hrtime "^0.1.2"
59695969

5970+
w3c-xmlserializer@^2.0.0:
5971+
version "2.0.0"
5972+
resolved "https://registry.yarnpkg.com/w3c-xmlserializer/-/w3c-xmlserializer-2.0.0.tgz#3e7104a05b75146cc60f564380b7f683acf1020a"
5973+
integrity sha512-4tzD0mF8iSiMiNs30BiLO3EpfGLZUT2MSX/G+o7ZywDzliWQ3OPtTZ0PTC3B3ca1UAf4cJMHB+2Bf56EriJuRA==
5974+
dependencies:
5975+
xml-name-validator "^3.0.0"
5976+
59705977
walker@^1.0.7, walker@~1.0.5:
59715978
version "1.0.7"
59725979
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"

0 commit comments

Comments
 (0)