From eee89f6c00e377652d12a79949913c6213dab461 Mon Sep 17 00:00:00 2001 From: tangshengzhi Date: Fri, 13 Dec 2024 21:15:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AD=90=E5=BA=94=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=90=86cloneNode=E4=BA=8B=E4=BB=B6=E3=80=82=E8=A7=A3=E5=86=B3?= =?UTF-8?q?@svgdotjs/svg.js=20cloneNode=E5=A4=8D=E5=88=B6=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=98=BE=E7=A4=BA=E5=BC=82=E5=B8=B8=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/wujie-core/src/iframe.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/wujie-core/src/iframe.ts b/packages/wujie-core/src/iframe.ts index 9bd14805..a380eeff 100644 --- a/packages/wujie-core/src/iframe.ts +++ b/packages/wujie-core/src/iframe.ts @@ -567,6 +567,24 @@ function patchDocumentEffect(iframeWindow: Window): void { // 运行插件钩子函数 execHooks(iframeWindow.__WUJIE.plugins, "documentPropertyOverride", iframeWindow); } +/** + * 代理子应用cloneNode方法 + */ +function proxyNodeClone(node: Node, iframeWindow: Window): Node { + if (iframeWindow.document !== node?.ownerDocument) { + Object.defineProperty(node, "ownerDocument", { + get() { + return iframeWindow.document + } + }) + } + if (node instanceof Element) { + for (const subNode of node.children as any) { + proxyNodeClone(subNode, iframeWindow); + } + } + return node; +} /** * patch Node effect @@ -579,6 +597,7 @@ function patchNodeEffect(iframeWindow: Window): void { const rawAppendChild = iframeWindow.Node.prototype.appendChild; const rawInsertRule = iframeWindow.Node.prototype.insertBefore; const rawRemoveChild = iframeWindow.Node.prototype.removeChild; + const rawCloneNode = iframeWindow.Node.prototype.cloneNode; iframeWindow.Node.prototype.getRootNode = function (options?: GetRootNodeOptions): Node { const rootNode = rawGetRootNode.call(this, options); if (rootNode === iframeWindow.__WUJIE.shadowRoot) return iframeWindow.document; @@ -609,6 +628,10 @@ function patchNodeEffect(iframeWindow: Window): void { patchElementEffect(node, iframeWindow); return res; }; + iframeWindow.Node.prototype.cloneNode = function (deep) { + const node = rawCloneNode.call(this, deep) + return proxyNodeClone(node, iframeWindow) + } } /**