From a4f01c04ede5561840e2096f14d1c7ae49d2736a Mon Sep 17 00:00:00 2001 From: Kuitos Date: Tue, 16 May 2023 16:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20support=20Node.prototype.compare?= =?UTF-8?q?DocumentPosition=20in=20sandbox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamicAppend/forStrictSandbox.ts | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts index 93ac9bf2e..ac79c949e 100644 --- a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts @@ -44,11 +44,7 @@ const proxyAttachContainerConfigMap: WeakMap = const elementAttachContainerConfigMap = new WeakMap(); const docCreatePatchedMap = new WeakMap(); -const mutationObserverPatchedMap = new WeakMap< - typeof MutationObserver.prototype.observe, - typeof MutationObserver.prototype.observe ->(); -const parentNodePatchedMap = new WeakMap(); +const patchMap = new WeakMap(); function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) { const { sandbox, speedy } = cfg; @@ -129,20 +125,30 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) { // patch MutationObserver.prototype.observe to avoid type error // https://github.com/umijs/qiankun/issues/2406 const nativeMutationObserverObserveFn = MutationObserver.prototype.observe; - if (!mutationObserverPatchedMap.has(nativeMutationObserverObserveFn)) { + if (!patchMap.has(nativeMutationObserverObserveFn)) { const observe = function observe(this: MutationObserver, target: Node, options: MutationObserverInit) { const realTarget = target instanceof Document ? nativeDocument : target; return nativeMutationObserverObserveFn.call(this, realTarget, options); }; MutationObserver.prototype.observe = observe; - mutationObserverPatchedMap.set(nativeMutationObserverObserveFn, observe); + patchMap.set(nativeMutationObserverObserveFn, observe); + } + + // patch Node.prototype.compareDocumentPosition to avoid type error + const prevCompareDocumentPosition = Node.prototype.compareDocumentPosition; + if (!patchMap.has(prevCompareDocumentPosition)) { + Node.prototype.compareDocumentPosition = function compareDocumentPosition(this: Node, node) { + const realNode = node instanceof Document ? nativeDocument : node; + return prevCompareDocumentPosition.call(this, realNode); + }; + patchMap.set(prevCompareDocumentPosition, Node.prototype.compareDocumentPosition); } // patch parentNode getter to avoid document === html.parentNode // https://github.com/umijs/qiankun/issues/2408#issuecomment-1446229105 const parentNodeDescriptor = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode'); - if (parentNodeDescriptor && !parentNodePatchedMap.has(parentNodeDescriptor)) { + if (parentNodeDescriptor && !patchMap.has(parentNodeDescriptor)) { const { get: parentNodeGetter, configurable } = parentNodeDescriptor; if (parentNodeGetter && configurable) { const patchedParentNodeDescriptor = { @@ -161,17 +167,20 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) { }; Object.defineProperty(Node.prototype, 'parentNode', patchedParentNodeDescriptor); - parentNodePatchedMap.set(parentNodeDescriptor, patchedParentNodeDescriptor); + patchMap.set(parentNodeDescriptor, patchedParentNodeDescriptor); } } return () => { MutationObserver.prototype.observe = nativeMutationObserverObserveFn; - mutationObserverPatchedMap.delete(nativeMutationObserverObserveFn); + patchMap.delete(nativeMutationObserverObserveFn); + + Node.prototype.compareDocumentPosition = prevCompareDocumentPosition; + patchMap.delete(prevCompareDocumentPosition); if (parentNodeDescriptor) { Object.defineProperty(Node.prototype, 'parentNode', parentNodeDescriptor); - parentNodePatchedMap.delete(parentNodeDescriptor); + patchMap.delete(parentNodeDescriptor); } }; }