Skip to content

Commit 17459cd

Browse files
committed
The sheet was not available on the rebuild, so wait to call adaptStylesheetForReplay until the <style> element is in the DOM
1 parent 9c8511d commit 17459cd

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

packages/rrweb-snapshot/src/rebuild.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
serializedNodeWithId,
3+
serializedElementNodeWithId,
34
NodeType,
45
tagMap,
56
elementNode,
@@ -99,6 +100,7 @@ const MEDIA_SELECTOR_GLOBAL = new RegExp(MEDIA_SELECTOR.source, 'g');
99100
const HOVER_SELECTOR = /([^\\]):hover/;
100101
const HOVER_SELECTOR_GLOBAL = new RegExp(HOVER_SELECTOR.source, 'g');
101102
export function adaptStylesheetForReplay(cssRules: CSSRuleList) {
103+
// @ts-ignore ignore TS2488: Type 'CSSRuleList' must have a '[Symbol.iterator]()' method that returns an iterator ... it should be fixed in https://github.com/Microsoft/TypeScript/issues/23406
102104
for (const cssRule of cssRules) {
103105
if ('media' in cssRule) {
104106
const cssMediaRule = cssRule as CSSMediaRule;
@@ -507,12 +509,6 @@ export function buildNodeWithSN(
507509
} else {
508510
node.appendChild(childNode);
509511
}
510-
if (hackCss && childN.type === NodeType.Element && childN.tagName === 'STYLE') {
511-
const styleEl = childNode as HTMLStyleElement;
512-
if (styleEl.sheet) {
513-
adaptStylesheetForReplay(styleEl.sheet.cssRules);
514-
}
515-
}
516512
if (afterAppend) {
517513
afterAppend(childNode, childN.id);
518514
}
@@ -534,12 +530,7 @@ function visit(mirror: Mirror, onVisit: (node: Node) => void) {
534530
}
535531
}
536532

537-
function handleScroll(node: Node, mirror: Mirror) {
538-
const n = mirror.getMeta(node);
539-
if (n?.type !== NodeType.Element) {
540-
return;
541-
}
542-
const el = node as HTMLElement;
533+
function handleScroll(el: HTMLElement, n: serializedElementNodeWithId) {
543534
for (const name in n.attributes) {
544535
if (
545536
!(
@@ -590,7 +581,18 @@ function rebuild(
590581
if (onVisit) {
591582
onVisit(visitedNode);
592583
}
593-
handleScroll(visitedNode, mirror);
584+
const n = mirror.getMeta(visitedNode);
585+
if (n?.type === NodeType.Element) {
586+
const el = visitedNode as HTMLElement;
587+
handleScroll(el, n as serializedElementNodeWithId);
588+
if (hackCss && el.tagName === 'STYLE') {
589+
let styleEl = el as HTMLStyleElement;
590+
if (styleEl.sheet) {
591+
// cssRules are always available on inline style elements
592+
adaptStylesheetForReplay(styleEl.sheet.cssRules);
593+
}
594+
}
595+
}
594596
});
595597
return node;
596598
}

0 commit comments

Comments
 (0)