🎨 optimize code and support for scenario of dynamic script comment as reference node (#2576)

This commit is contained in:
Kuitos 2023-08-07 19:40:40 +08:00 committed by GitHub
parent 425110e9c0
commit 3878cea04a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 7 deletions

View File

@ -22,12 +22,12 @@ type DynamicDomMutationTarget = 'head' | 'body';
declare global {
interface HTMLLinkElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number;
[styleElementRefNodeNo]?: Exclude<number, -1>;
}
interface HTMLStyleElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number;
[styleElementRefNodeNo]?: Exclude<number, -1>;
}
interface Function {

View File

@ -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;