From 3878cea04a2115ead265108533cf57309917f17c Mon Sep 17 00:00:00 2001 From: Kuitos Date: Mon, 7 Aug 2023 19:40:40 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20optimize=20code=20and=20support?= =?UTF-8?q?=20for=20scenario=20of=20dynamic=20script=20comment=20as=20refe?= =?UTF-8?q?rence=20node=20(#2576)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sandbox/patchers/dynamicAppend/common.ts | 4 ++-- src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/sandbox/patchers/dynamicAppend/common.ts b/src/sandbox/patchers/dynamicAppend/common.ts index 22fe001b5..eea4acb11 100644 --- a/src/sandbox/patchers/dynamicAppend/common.ts +++ b/src/sandbox/patchers/dynamicAppend/common.ts @@ -22,12 +22,12 @@ type DynamicDomMutationTarget = 'head' | 'body'; declare global { interface HTMLLinkElement { [styleElementTargetSymbol]: DynamicDomMutationTarget; - [styleElementRefNodeNo]?: number; + [styleElementRefNodeNo]?: Exclude; } interface HTMLStyleElement { [styleElementTargetSymbol]: DynamicDomMutationTarget; - [styleElementRefNodeNo]?: number; + [styleElementRefNodeNo]?: Exclude; } interface Function { diff --git a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts index 4d8622dfd..b86abbe8d 100644 --- a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts @@ -284,11 +284,10 @@ export function patchStrictSandbox( const refNo = stylesheetElement[styleElementRefNodeNo]; if (typeof refNo === 'number' && refNo !== -1) { - const refNode = mountDom.childNodes[refNo]; - if (refNode) { - rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode); - return true; - } + // the reference node may be dynamic script comment which is not rebuilt while remounting thus reference node no longer exists + const refNode = mountDom.childNodes[refNo] || null; + rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode); + return true; } else { rawHeadAppendChild.call(mountDom, stylesheetElement); return true;