Skip to content

Commit

Permalink
fix: only use IE11 compatible APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalle Ott committed Jul 1, 2020
1 parent c900765 commit 29ee4e9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "react-lazy-svg",
"version": "1.1.1",
"license": "MIT",
"repository": {
"url": "https://github.com/kaoDev/react-lazy-svg"
Expand All @@ -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",
Expand Down Expand Up @@ -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": {}
}
14 changes: 9 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -266,6 +270,6 @@ export const initOnClient = (
addIcon(iconData);

knownIcons.set(id, new Promise((resolve) => resolve(iconData)));
});
}
}
};
8 changes: 7 additions & 1 deletion test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<svg id="__SVG_SPRITE_SHEET__" style="display:none">
<symbol
Expand All @@ -80,6 +86,6 @@ test('client should be able to initiate the cache from a rendered dom', async ()
expect(iconData?.attributes.width).toBe(undefined);
expect(iconData?.attributes.viewBox).toBe('0 0 24 24');
expect(iconData?.svgString.__html).toBe(
'<path d="M0 0h24v24H0z" fill="none"></path>',
'<path xmlns="http://www.w3.org/2000/svg" d="M0 0h24v24H0z" fill="none"/>',
);
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5967,6 +5967,13 @@ w3c-hr-time@^1.0.1:
dependencies:
browser-process-hrtime "^0.1.2"

w3c-xmlserializer@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/w3c-xmlserializer/-/w3c-xmlserializer-2.0.0.tgz#3e7104a05b75146cc60f564380b7f683acf1020a"
integrity sha512-4tzD0mF8iSiMiNs30BiLO3EpfGLZUT2MSX/G+o7ZywDzliWQ3OPtTZ0PTC3B3ca1UAf4cJMHB+2Bf56EriJuRA==
dependencies:
xml-name-validator "^3.0.0"

walker@^1.0.7, walker@~1.0.5:
version "1.0.7"
resolved "https://registry.yarnpkg.com/walker/-/walker-1.0.7.tgz#2f7f9b8fd10d677262b18a884e28d19618e028fb"
Expand Down

0 comments on commit 29ee4e9

Please sign in to comment.