Skip to content

Commit be1a665

Browse files
authored
fix: css bug identified in user site (#83)
see https://posthoghelp.zendesk.com/agent/tickets/29917 in which a style node has multiple text node children _together_ those nodes make up a valid style rule but calling `adaptCssForReplay` on each individually causes it to throw let's make it not throw and then if needed can gather examples of what valid and invalid input look like to figure out the next step
1 parent abc8426 commit be1a665

File tree

6 files changed

+187
-8
lines changed

6 files changed

+187
-8
lines changed

packages/rrdom/test/diff.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,7 @@ describe('diff algorithm for rrdom', () => {
14131413
* If the selector match is case insensitive, it will cause some CSS style problems in the replayer.
14141414
* This test result executed in JSDom is different from that in real browser so we use puppeteer as test environment.
14151415
*/
1416-
const browser = await puppeteer.launch();
1416+
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
14171417
const page = await browser.newPage();
14181418
await page.goto('about:blank');
14191419

packages/rrdom/test/virtual-dom.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ describe('RRDocument for browser environment', () => {
242242
let page: puppeteer.Page;
243243

244244
beforeAll(async () => {
245-
browser = await puppeteer.launch();
245+
browser = await puppeteer.launch({ args: ['--no-sandbox'] });
246246
code = fs.readFileSync(
247247
path.resolve(__dirname, '../dist/rrdom.umd.cjs'),
248248
'utf8',

packages/rrweb-snapshot/src/rebuild.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type elementNode,
77
type legacyAttributes,
88
} from '@posthog/rrweb-types';
9-
import { type tagMap, type BuildCache } from './types';
9+
import { type tagMap, type BuildCache, type textNode } from './types';
1010
import {
1111
isElement,
1212
Mirror,
@@ -89,6 +89,29 @@ export function createCache(): BuildCache {
8989
};
9090
}
9191

92+
function safeDocNode(
93+
n: textNode,
94+
options: {
95+
doc: Document;
96+
hackCss: boolean;
97+
cache: BuildCache;
98+
},
99+
) {
100+
let stringContent = n.textContent;
101+
if (n.isStyle && options.hackCss) {
102+
try {
103+
stringContent = adaptCssForReplay(n.textContent, options.cache);
104+
} catch {
105+
// if, for example, a style node has multiple text nodes
106+
// which can only be adopted to CSS all together,
107+
// then this will fail...
108+
// ignore
109+
}
110+
}
111+
112+
return options.doc.createTextNode(stringContent);
113+
}
114+
92115
function buildNode(
93116
n: serializedNodeWithId,
94117
options: {
@@ -327,11 +350,12 @@ function buildNode(
327350
return node;
328351
}
329352
case NodeType.Text:
330-
return doc.createTextNode(
331-
n.isStyle && hackCss
332-
? adaptCssForReplay(n.textContent, cache)
333-
: n.textContent,
334-
);
353+
return safeDocNode(n, {
354+
cache,
355+
doc,
356+
hackCss,
357+
});
358+
335359
case NodeType.CDATA:
336360
if (n.textContent.trim() === '') {
337361
return null;

packages/rrweb-snapshot/test/__snapshots__/rebuild.test.ts.snap

Lines changed: 3 additions & 0 deletions
Large diffs are not rendered by default.

packages/rrweb-snapshot/test/integration.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ describe('integration tests', function (this: ISuite) {
103103
serverURL = getServerURL(server);
104104
browser = await puppeteer.launch({
105105
// headless: false,
106+
args: ['--no-sandbox'],
106107
});
107108

108109
code = fs.readFileSync(
@@ -435,6 +436,7 @@ describe('iframe integration tests', function (this: ISuite) {
435436
serverURL = getServerURL(server);
436437
browser = await puppeteer.launch({
437438
// headless: false,
439+
args: ['--no-sandbox'],
438440
});
439441

440442
code = fs.readFileSync(
@@ -479,6 +481,7 @@ describe('dialog integration tests', function (this: ISuite) {
479481
serverURL = getServerURL(server);
480482
browser = await puppeteer.launch({
481483
// headless: false,
484+
args: ['--no-sandbox'],
482485
});
483486

484487
code = fs.readFileSync(
@@ -525,6 +528,7 @@ describe('shadow DOM integration tests', function (this: ISuite) {
525528
serverURL = getServerURL(server);
526529
browser = await puppeteer.launch({
527530
// headless: false,
531+
args: ['--no-sandbox'],
528532
});
529533

530534
code = fs.readFileSync(

packages/rrweb-snapshot/test/rebuild.test.ts

Lines changed: 148 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)