Skip to content

Commit 1b71bc3

Browse files
committed
Realize we don't actually need to look at the .sheet during a mutation, even if there is only one text element on the <style>.
Recent conversation with Justin has firmed up my thinking on why we look at .sheet in the first place (it's not because we want to get rid of vendor prefixes, which may actually be undesirable where they are useful for accurate replay in the replayer - but rather it's to ensure we don't miss any programmatic style mutations that have happened - have added comments to reflect this)
1 parent afbe14c commit 1b71bc3

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

packages/rrweb-snapshot/src/snapshot.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -570,26 +570,10 @@ function serializeTextNode(
570570
} else if (!blankTextNodes) {
571571
textContent = n.textContent;
572572
if (isStyle && textContent) {
573-
// This branch is solely for the use of mutation
574-
if (n.nextSibling || n.previousSibling) {
575-
// This is not the only child of the stylesheet.
576-
// We can't read all of the sheet's .cssRules and expect them
577-
// to _only_ include the current rule(s) added by the text node.
578-
// So we'll be conservative and keep textContent as-is.
579-
} else if ((n.parentNode as HTMLStyleElement).sheet?.cssRules) {
580-
try {
581-
textContent = stringifyStylesheet(
582-
(n.parentNode as HTMLStyleElement).sheet!,
583-
);
584-
} catch (err) {
585-
console.warn(
586-
`Cannot get CSS styles from text's parentNode. Error: ${
587-
err as string
588-
}`,
589-
n,
590-
);
591-
}
592-
}
573+
// mutation only: we don't need to use stringifyStylesheet
574+
// as a <style> text node mutation obliterates any previous
575+
// programmatic rule manipulation (.insertRule etc.)
576+
// so the current textContent represents the most up to date state
593577
textContent = absoluteToStylesheet(textContent, getHref(options.doc));
594578
}
595579
}

packages/rrweb-snapshot/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ export function escapeImportStatement(rule: CSSImportRule): string {
9393
return statement.join(' ') + ';';
9494
}
9595

96+
/*
97+
* serialize the css rules from the .sheet property
98+
* for <link rel="stylesheet"> elements, this is the only way of getting the rules without a FETCH
99+
* for <style> elements, this is less preferable to looking at childNodes[0].textContent
100+
* (which will include vendor prefixed rules which may not be used or visible to the recorded browser,
101+
* but which might be needed by the replayer browser)
102+
* however, at snapshot time, we don't know whether the style element has suffered
103+
* any programmatic manipulation prior to the snapshot, in which case the .sheet would be more up to date
104+
*/
96105
export function stringifyStylesheet(s: CSSStyleSheet): string | null {
97106
try {
98107
const rules = s.rules || s.cssRules;

0 commit comments

Comments
 (0)