🎨 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 { declare global {
interface HTMLLinkElement { interface HTMLLinkElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget; [styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number; [styleElementRefNodeNo]?: Exclude<number, -1>;
} }
interface HTMLStyleElement { interface HTMLStyleElement {
[styleElementTargetSymbol]: DynamicDomMutationTarget; [styleElementTargetSymbol]: DynamicDomMutationTarget;
[styleElementRefNodeNo]?: number; [styleElementRefNodeNo]?: Exclude<number, -1>;
} }
interface Function { interface Function {

View File

@ -284,11 +284,10 @@ export function patchStrictSandbox(
const refNo = stylesheetElement[styleElementRefNodeNo]; const refNo = stylesheetElement[styleElementRefNodeNo];
if (typeof refNo === 'number' && refNo !== -1) { if (typeof refNo === 'number' && refNo !== -1) {
const refNode = mountDom.childNodes[refNo]; // the reference node may be dynamic script comment which is not rebuilt while remounting thus reference node no longer exists
if (refNode) { const refNode = mountDom.childNodes[refNo] || null;
rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode); rawHeadInsertBefore.call(mountDom, stylesheetElement, refNode);
return true; return true;
}
} else { } else {
rawHeadAppendChild.call(mountDom, stylesheetElement); rawHeadAppendChild.call(mountDom, stylesheetElement);
return true; return true;