Skip to content

Commit 2fddc81

Browse files
guntherjhVadman97
authored andcommitted
Handle exceptions thrown from postcss when calling adaptCssForReplay
1 parent 55bd199 commit 2fddc81

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

.changeset/angry-turtles-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rrweb-snapshot": patch
3+
---
4+
5+
Handle exceptions thrown from postcss when calling adaptCssForReplay

packages/rrweb-snapshot/src/rebuild.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string {
6262
const cachedStyle = cache?.stylesWithHoverClass.get(cssText);
6363
if (cachedStyle) return cachedStyle;
6464

65-
const ast: { css: string } = postcss([
66-
mediaSelectorPlugin,
67-
pseudoClassPlugin,
68-
]).process(cssText);
69-
const result = ast.css;
65+
let result = cssText;
66+
try {
67+
const ast: { css: string } = postcss([
68+
mediaSelectorPlugin,
69+
pseudoClassPlugin,
70+
]).process(cssText);
71+
result = ast.css;
72+
} catch (error) {
73+
console.warn('Failed to adapt css for replay', error);
74+
}
75+
7076
cache?.stylesWithHoverClass.set(cssText, result);
7177
return result;
7278
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
*/
44
import * as fs from 'fs';
55
import * as path from 'path';
6-
import { beforeEach, describe, expect as _expect, it } from 'vitest';
6+
import { beforeEach, describe, expect as _expect, it, vi } from 'vitest';
77
import {
88
adaptCssForReplay,
99
buildNodeWithSN,
1010
createCache,
1111
} from '../src/rebuild';
1212
import { NodeType } from '../src/types';
1313
import { createMirror, Mirror } from '../src/utils';
14+
import postcss from 'postcss';
1415

1516
const expect = _expect as unknown as {
1617
<T = unknown>(actual: T): {
@@ -248,4 +249,17 @@ ul li.specified c.\\:hover img {
248249
should_not_modify,
249250
);
250251
});
252+
253+
it('handles exceptions from postcss when calling adaptCssForReplay', () => {
254+
const consoleWarnSpy = vi
255+
.spyOn(console, 'warn')
256+
.mockImplementation(() => {});
257+
// trigger CssSyntaxError by passing invalid css
258+
const cssText = 'a{';
259+
adaptCssForReplay(cssText, cache);
260+
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
261+
'Failed to adapt css for replay',
262+
expect.any(Error),
263+
);
264+
});
251265
});

0 commit comments

Comments
 (0)