Skip to content

Commit c28ef5f

Browse files
Replay fix for Firefox — add <head> and <body> separately (#1133)
* Firefox fix: Allow the <head> and <body> to be added in two stages so that (presumably) stylesheet rules are ready to be applied when the body appears The css which triggered the bug was simply { margin-left: 220px; transition: margin-left .448s; } * Add a test case which can only be appreciated if you record against this file://, save the events to a html file, and then open the file in Firefox (without this PR applied) * Apply formatting changes * Ensure we don't apply this branch when using rrdom, where it is not necessary * Apply formatting changes * Rewrite insertion in order to be compatible with rrdom Also easier to understand * Delete transition.html * Create grumpy-ways-own.md --------- Co-authored-by: Yun Feng <[email protected]>
1 parent be54981 commit c28ef5f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

.changeset/grumpy-ways-own.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'rrweb-snapshot': patch
3+
---
4+
5+
Fix: CSS transitions are incorrectly being applied upon rebuild in Firefox. Presumably FF doesn't finished parsing the styles in time, and applies e.g. a default margin:0 to elements which have a non-zero margin set in CSS, along with a transition on them.
6+
7+
Related bug report to Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1816672​

packages/rrweb-snapshot/src/rebuild.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,28 @@ export function buildNodeWithSN(
438438

439439
if (childN.isShadow && isElement(node) && node.shadowRoot) {
440440
node.shadowRoot.appendChild(childNode);
441+
} else if (
442+
n.type === NodeType.Document &&
443+
childN.type == NodeType.Element
444+
) {
445+
const htmlElement = childNode as HTMLElement;
446+
let body: HTMLBodyElement | null = null;
447+
htmlElement.childNodes.forEach((child) => {
448+
if (child.nodeName === 'BODY') body = child as HTMLBodyElement;
449+
});
450+
if (body) {
451+
// this branch solves a problem in Firefox where css transitions are incorrectly
452+
// being applied upon rebuild. Presumably FF doesn't finished parsing the styles
453+
// in time, and applies e.g. a default margin:0 to elements which have a non-zero
454+
// margin set in CSS, along with a transition on them
455+
htmlElement.removeChild(body);
456+
// append <head> and <style>s
457+
node.appendChild(childNode);
458+
// now append <body>
459+
htmlElement.appendChild(body);
460+
} else {
461+
node.appendChild(childNode);
462+
}
441463
} else {
442464
node.appendChild(childNode);
443465
}

0 commit comments

Comments
 (0)