Skip to content

Commit

Permalink
Polyfill Node.prototype.cloneNode behavior for Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Jul 17, 2024
1 parent 1378411 commit 2287eda
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/famous-suns-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/dna': patch
---

Polyfill `Node.prototype.cloneNode` behavior for Safari.
10 changes: 9 additions & 1 deletion src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ function polyfillBuiltin() {
const customElements = window.customElements;
const define = customElements.define.bind(customElements);
const upgrade = customElements.upgrade.bind(customElements);
const cloneNode = Node.prototype.cloneNode;
const setInnerHTML = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
const filterBuiltinElement = (node: Node) =>
isElement(node) && node.getAttribute('is') ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
let childListObserver: MutationObserver;
Expand Down Expand Up @@ -181,6 +183,7 @@ function polyfillBuiltin() {
}

const upgradedNode = Reflect.construct(constructor, [root], constructor) as CustomElement;
upgradedNode.setAttribute(':ce-polyfill', '');
for (let i = 0, len = attributes.length; i < len; i++) {
const { name, value } = attributes[i];
if (upgradedNode.getAttribute(name) === value) {
Expand Down Expand Up @@ -235,7 +238,12 @@ function polyfillBuiltin() {
return nativeCreateElement.call(document, tagName, options);
};

const setInnerHTML = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML');
Node.prototype.cloneNode = function (deep?: boolean) {
const clone = cloneNode.call(this, deep);
polyfillUpgrade(clone as Element);
return clone;
};

if (setInnerHTML) {
Object.defineProperty(Element.prototype, 'innerHTML', {
...setInnerHTML,
Expand Down

0 comments on commit 2287eda

Please sign in to comment.