Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions packages/rrweb/src/replay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
VirtualStyleRules,
VirtualStyleRulesMap,
getNestedRule,
getPositionsAndIndex,
} from './virtual-styles';

const SKIP_TIME_THRESHOLD = 10 * 1000;
Expand Down Expand Up @@ -976,23 +977,24 @@ export class Replayer {
}

if (d.adds) {
d.adds.forEach(({ rule, index }) => {
d.adds.forEach(({ rule, index: nestedIndex }) => {
if (styleSheet) {
try {
if (Array.isArray(index)) {
const positions = [...index];
const insertAt = positions.pop();
if (Array.isArray(nestedIndex)) {
const { positions, index } = getPositionsAndIndex(
nestedIndex,
);
const nestedRule = getNestedRule(
styleSheet.cssRules,
positions,
);
nestedRule.insertRule(rule, insertAt);
nestedRule.insertRule(rule, index);
} else {
const _index =
index === undefined
? undefined
: Math.min(index, styleSheet.cssRules.length);
styleSheet.insertRule(rule, _index);
const index =
nestedIndex === undefined
? undefined
: Math.min(nestedIndex, styleSheet.cssRules.length);
styleSheet.insertRule(rule, index);
}
} catch (e) {
/**
Expand All @@ -1005,27 +1007,32 @@ export class Replayer {
*/
}
} else {
rules?.push({ cssText: rule, index, type: StyleRuleType.Insert });
rules?.push({
cssText: rule,
index: nestedIndex,
type: StyleRuleType.Insert,
});
}
});
}

if (d.removes) {
d.removes.forEach(({ index }) => {
d.removes.forEach(({ index: nestedIndex }) => {
if (usingVirtualParent) {
rules?.push({ index, type: StyleRuleType.Remove });
rules?.push({ index: nestedIndex, type: StyleRuleType.Remove });
} else {
try {
if (Array.isArray(index)) {
const positions = [...index];
const deleteAt = positions.pop();
if (Array.isArray(nestedIndex)) {
const { positions, index } = getPositionsAndIndex(
nestedIndex,
);
const nestedRule = getNestedRule(
styleSheet!.cssRules,
positions,
);
nestedRule.deleteRule(deleteAt || 0);
nestedRule.deleteRule(index || 0);
} else {
styleSheet?.deleteRule(index);
styleSheet?.deleteRule(nestedIndex);
}
} catch (e) {
/**
Expand Down
16 changes: 10 additions & 6 deletions packages/rrweb/src/replay/virtual-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export function getNestedRule(
}
}

export function getPositionsAndIndex(nestedIndex: number[]) {
const positions = [...nestedIndex];
const index = positions.pop();
return { positions, index };
}

export function applyVirtualStyleRulesToNode(
storedRules: VirtualStyleRules,
styleNode: HTMLStyleElement,
Expand All @@ -47,13 +53,12 @@ export function applyVirtualStyleRulesToNode(
if (rule.type === StyleRuleType.Insert) {
try {
if (Array.isArray(rule.index)) {
const positions = [...rule.index];
const insertAt = positions.pop();
const { positions, index } = getPositionsAndIndex(rule.index);
const nestedRule = getNestedRule(
styleNode.sheet!.cssRules,
positions,
);
nestedRule.insertRule(rule.cssText, insertAt);
nestedRule.insertRule(rule.cssText, index);
} else {
styleNode.sheet?.insertRule(rule.cssText, rule.index);
}
Expand All @@ -66,13 +71,12 @@ export function applyVirtualStyleRulesToNode(
} else if (rule.type === StyleRuleType.Remove) {
try {
if (Array.isArray(rule.index)) {
const positions = [...rule.index];
const deleteAt = positions.pop();
const { positions, index } = getPositionsAndIndex(rule.index);
const nestedRule = getNestedRule(
styleNode.sheet!.cssRules,
positions,
);
nestedRule.deleteRule(deleteAt || 0);
nestedRule.deleteRule(index || 0);
} else {
styleNode.sheet?.deleteRule(rule.index);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/rrweb/typings/replay/virtual-styles.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ export declare enum StyleRuleType {
declare type InsertRule = {
cssText: string;
type: StyleRuleType.Insert;
index?: number;
index?: number | number[];
};
declare type RemoveRule = {
type: StyleRuleType.Remove;
index: number;
index: number | number[];
};
declare type SnapshotRule = {
type: StyleRuleType.Snapshot;
cssTexts: string[];
};
export declare type VirtualStyleRules = Array<InsertRule | RemoveRule | SnapshotRule>;
export declare type VirtualStyleRulesMap = Map<INode, VirtualStyleRules>;
export declare function getNestedRule(rules: CSSRuleList, position: number[]): CSSGroupingRule;
export declare function applyVirtualStyleRulesToNode(storedRules: VirtualStyleRules, styleNode: HTMLStyleElement): void;
export declare function storeCSSRules(parentElement: HTMLStyleElement, virtualStyleRulesMap: VirtualStyleRulesMap): void;
export {};
4 changes: 2 additions & 2 deletions packages/rrweb/typings/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ export declare type scrollPosition = {
export declare type scrollCallback = (p: scrollPosition) => void;
export declare type styleSheetAddRule = {
rule: string;
index?: number;
index?: number | number[];
};
export declare type styleSheetDeleteRule = {
index: number;
index: number | number[];
};
export declare type styleSheetRuleParam = {
id: number;
Expand Down