gc virtual style map when DOM has been removed

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent f0f0106749
commit 5db9e807af
2 changed files with 16 additions and 13 deletions

View File

@@ -922,7 +922,6 @@ export class Replayer {
case MouseInteractions.TouchStart: case MouseInteractions.TouchStart:
case MouseInteractions.TouchEnd: case MouseInteractions.TouchEnd:
if (isSync) { if (isSync) {
let touchActive: boolean | undefined;
if (d.type === MouseInteractions.TouchStart) { if (d.type === MouseInteractions.TouchStart) {
this.touchActive = true; this.touchActive = true;
} else if (d.type === MouseInteractions.TouchEnd) { } else if (d.type === MouseInteractions.TouchEnd) {
@@ -1263,6 +1262,9 @@ export class Replayer {
} }
return this.warnNodeNotFound(d, mutation.id); return this.warnNodeNotFound(d, mutation.id);
} }
if (this.virtualStyleRulesMap.has(target)) {
this.virtualStyleRulesMap.delete(target);
}
let parent: INode | null | ShadowRoot = this.mirror.getNode( let parent: INode | null | ShadowRoot = this.mirror.getNode(
mutation.parentId, mutation.parentId,
); );

View File

@@ -65,18 +65,22 @@ export function applyVirtualStyleRulesToNode(
storedRules: VirtualStyleRules, storedRules: VirtualStyleRules,
styleNode: HTMLStyleElement, styleNode: HTMLStyleElement,
) { ) {
const { sheet } = styleNode;
if (!sheet) {
// styleNode without sheet means the DOM has been removed
// so the rules no longer need to be applied
return;
}
storedRules.forEach((rule) => { storedRules.forEach((rule) => {
if (rule.type === StyleRuleType.Insert) { if (rule.type === StyleRuleType.Insert) {
try { try {
if (Array.isArray(rule.index)) { if (Array.isArray(rule.index)) {
const { positions, index } = getPositionsAndIndex(rule.index); const { positions, index } = getPositionsAndIndex(rule.index);
const nestedRule = getNestedRule( const nestedRule = getNestedRule(sheet.cssRules, positions);
styleNode.sheet!.cssRules,
positions,
);
nestedRule.insertRule(rule.cssText, index); nestedRule.insertRule(rule.cssText, index);
} else { } else {
styleNode.sheet?.insertRule(rule.cssText, rule.index); sheet.insertRule(rule.cssText, rule.index);
} }
} catch (e) { } catch (e) {
/** /**
@@ -88,13 +92,10 @@ export function applyVirtualStyleRulesToNode(
try { try {
if (Array.isArray(rule.index)) { if (Array.isArray(rule.index)) {
const { positions, index } = getPositionsAndIndex(rule.index); const { positions, index } = getPositionsAndIndex(rule.index);
const nestedRule = getNestedRule( const nestedRule = getNestedRule(sheet.cssRules, positions);
styleNode.sheet!.cssRules,
positions,
);
nestedRule.deleteRule(index || 0); nestedRule.deleteRule(index || 0);
} else { } else {
styleNode.sheet?.deleteRule(rule.index); sheet.deleteRule(rule.index);
} }
} catch (e) { } catch (e) {
/** /**
@@ -106,13 +107,13 @@ export function applyVirtualStyleRulesToNode(
restoreSnapshotOfStyleRulesToNode(rule.cssTexts, styleNode); restoreSnapshotOfStyleRulesToNode(rule.cssTexts, styleNode);
} else if (rule.type === StyleRuleType.SetProperty) { } else if (rule.type === StyleRuleType.SetProperty) {
const nativeRule = (getNestedRule( const nativeRule = (getNestedRule(
styleNode.sheet!.cssRules, sheet.cssRules,
rule.index, rule.index,
) as unknown) as CSSStyleRule; ) as unknown) as CSSStyleRule;
nativeRule.style.setProperty(rule.property, rule.value, rule.priority); nativeRule.style.setProperty(rule.property, rule.value, rule.priority);
} else if (rule.type === StyleRuleType.RemoveProperty) { } else if (rule.type === StyleRuleType.RemoveProperty) {
const nativeRule = (getNestedRule( const nativeRule = (getNestedRule(
styleNode.sheet!.cssRules, sheet.cssRules,
rule.index, rule.index,
) as unknown) as CSSStyleRule; ) as unknown) as CSSStyleRule;
nativeRule.style.removeProperty(rule.property); nativeRule.style.removeProperty(rule.property);