Skip to content

Commit 47a7c3f

Browse files
authored
Warn instead of fail on exceptions thrown from postcss (#1580)
* postcss was introduced in #1458 for use within adaptCssForReplay * #1600 fixes the main case where invalid css could be introduced when if valid css from the output of `sheet.cssRules` was split according to how it was split across text nodes of the <style> * the guard introduced here is still useful as we likely in future will switch to capturing the raw stylesheet contents (both <style> and <link>), at which point we will be much less confident of getting valid css
1 parent a6893f7 commit 47a7c3f

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

.changeset/angry-turtles-provide.md

+5
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

+11-5
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

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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,
@@ -270,4 +270,17 @@ ul li.specified c.\\:hover img {
270270
should_not_modify,
271271
);
272272
});
273+
274+
it('handles exceptions from postcss when calling adaptCssForReplay', () => {
275+
const consoleWarnSpy = vi
276+
.spyOn(console, 'warn')
277+
.mockImplementation(() => {});
278+
// trigger CssSyntaxError by passing invalid css
279+
const cssText = 'a{';
280+
adaptCssForReplay(cssText, cache);
281+
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
282+
'Failed to adapt css for replay',
283+
expect.any(Error),
284+
);
285+
});
273286
});

0 commit comments

Comments
 (0)