From 006ffabae85d1cd220a132cff90f1f5d6244dd0c Mon Sep 17 00:00:00 2001 From: front-refined <229298746@qq.com> Date: Thu, 30 May 2024 02:27:51 +0800 Subject: [PATCH] fix: The operation before hijacking the ownerdocument of the node --- src/sandbox/iframe/element.ts | 7 +++++++ typings/global.d.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/sandbox/iframe/element.ts b/src/sandbox/iframe/element.ts index 31e26879..bb1d434d 100644 --- a/src/sandbox/iframe/element.ts +++ b/src/sandbox/iframe/element.ts @@ -13,6 +13,7 @@ import { isNode, isMicroAppBody, throttleDeferForSetAppName, + isFunction, } from '../../libs/utils' import { updateElementInfo, @@ -164,6 +165,12 @@ function patchIframeNode ( configurable: true, enumerable: true, get () { + if (isFunction(microApp.options.beforeHijackOwnerDocument)) { + const nodeRawOwnerDocument = rawOwnerDocumentDesc.get!.call(this) + if (microApp.options.beforeHijackOwnerDocument?.({ node: this, ownerDocument: nodeRawOwnerDocument, appName })) { + return nodeRawOwnerDocument + } + } return this.__PURE_ELEMENT__ || this === microDocument ? rawOwnerDocumentDesc.get!.call(this) : microDocument diff --git a/typings/global.d.ts b/typings/global.d.ts index 8be122fd..43effc93 100644 --- a/typings/global.d.ts +++ b/typings/global.d.ts @@ -359,6 +359,30 @@ declare module '@micro-app/types' { fetch?: fetchType globalAssets?: globalAssetsType, excludeAssetFilter?: (assetUrl: string) => boolean + /** + * The operation before hijacking the ownerdocument of the node + * true returns the ownerdocument of the node, false performs the next step + * + * Scenes: + * 1. tinymce editor + * + * Issue: https://github.com/micro-zoe/micro-app/issues/916 + * + * @example + * ```js + * microApp.start({ + * beforeHijackOwnerDocument({ node, ownerDocument, appName }) { + * return ownerDocument?.body?.id === 'tinymce' + * } + * }) + * ``` + * + * @param params + * @param params.node - current node + * @param params.ownerDocument - node raw ownerDocument + * @param params.appName - current appName + */ + beforeHijackOwnerDocument?: (params: { node: Node, ownerDocument: Document, appName: AppName }) => boolean getRootElementParentNode?: (node: Node, appName: AppName) => void customProxyDocumentProps?: Map void> }