From 29ee4e99e49efe5924e7c7e9e24535938098c5c1 Mon Sep 17 00:00:00 2001 From: Kalle Ott Date: Wed, 1 Jul 2020 17:37:57 +0200 Subject: [PATCH] fix: only use IE11 compatible APIs --- package.json | 5 +++-- src/index.tsx | 14 +++++++++----- test/index.test.tsx | 8 +++++++- yarn.lock | 7 +++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c397c93..eeae7a0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "react-lazy-svg", + "version": "1.1.1", "license": "MIT", "repository": { "url": "https://github.com/kaoDev/react-lazy-svg" @@ -9,7 +10,6 @@ "url": "https://github.com/kaoDev/" }, "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.", - "version": "1.1.0", "main": "dist/index.js", "module": "dist/react-lazy-svg.esm.js", "typings": "dist/index.d.ts", @@ -50,7 +50,8 @@ "react-dom": "^16.13.1", "tsdx": "^0.13.2", "tslib": "^2.0.0", - "typescript": "^3.9.5" + "typescript": "^3.9.5", + "w3c-xmlserializer": "^2.0.0" }, "dependencies": {} } diff --git a/src/index.tsx b/src/index.tsx index 6583f32..80de8e2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -251,12 +251,16 @@ export const initOnClient = ( ) => { knownIcons.clear(); const spriteSheet = document.getElementById(spriteSheetId); - if (spriteSheet) { - const sprites = spriteSheet.querySelectorAll('symbol'); + const serializer = new XMLSerializer(); + const sprites = Array.from(spriteSheet.querySelectorAll('symbol')); + + for (const node of sprites) { + const innerHTML = Array.prototype.map + .call(node.childNodes, (child) => serializer.serializeToString(child)) + .join(''); - sprites.forEach((node) => { - const { id, attributes: rawAttributes, innerHTML } = node; + const { id, attributes: rawAttributes } = node; const attributes = mapNodeAttributes(rawAttributes); const iconData = { id, @@ -266,6 +270,6 @@ export const initOnClient = ( addIcon(iconData); knownIcons.set(id, new Promise((resolve) => resolve(iconData))); - }); + } } }; diff --git a/test/index.test.tsx b/test/index.test.tsx index 1e46256..41d25b1 100644 --- a/test/index.test.tsx +++ b/test/index.test.tsx @@ -59,6 +59,12 @@ test('should fill the cache when an icon is rendered', async () => { }); test('client should be able to initiate the cache from a rendered dom', async () => { + const serialize = require('w3c-xmlserializer'); + + window.XMLSerializer = class XMLSerializer { + serializeToString = serialize; + }; + const cache: IconsCache = new Map(); document.body.innerHTML = `