Skip to content

Commit

Permalink
Polyfill replaceChildren (#81)
Browse files Browse the repository at this point in the history
* Polyfill replaceChildren

* Update util.js

Co-authored-by: Mark Stacey <[email protected]>

* Rename replaceChildrenPolyfill to replaceChildrenPonyfill

Co-authored-by: Mark Stacey <[email protected]>
  • Loading branch information
danjm and Gudahtt authored Nov 23, 2021
1 parent 5d918e6 commit 73d3cd8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
}
};
}

Expand Down

0 comments on commit 73d3cd8

Please sign in to comment.