From 73d3cd89019abe2eeb5606542ac6d6fd13519239 Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Tue, 23 Nov 2021 20:10:09 -0330 Subject: [PATCH] Polyfill replaceChildren (#81) * Polyfill replaceChildren * Update util.js Co-authored-by: Mark Stacey * Rename replaceChildrenPolyfill to replaceChildrenPonyfill Co-authored-by: Mark Stacey --- util.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/util.js b/util.js index 185a7834..dbbf9adc 100644 --- a/util.js +++ b/util.js @@ -7,6 +7,18 @@ const transform = require('gl-vec3/transformMat4'); const SVG_NS = 'http://www.w3.org/2000/svg'; +// Taken from https://github.com/yuzhe-han/ParentNode-replaceChildren +// This is to support browsers that do not yet support `replaceChildren` +const replaceChildrenPonyfill = function (...addNodes) { + while (this.lastChild) { + this.removeChild(this.lastChild); + } + + if (addNodes.length > 0) { + this.append(...addNodes); + } +}; + module.exports = { calculateSizingOptions, createLogoViewer, @@ -497,7 +509,15 @@ function createFaceUpdater(container, polygons, transformed) { const newPolygons = toDraw.map((poly) => poly.svg); const defs = container.getElementsByTagName('defs'); const maskChildren = container.getElementsByTagName('mask'); - container.replaceChildren(...defs, ...maskChildren, ...newPolygons); + if (container.replaceChildren) { + container.replaceChildren(...defs, ...maskChildren, ...newPolygons); + } else { + replaceChildrenPonyfill.bind(container)( + ...defs, + ...maskChildren, + ...newPolygons, + ); + } }; }