Skip to content

Commit

Permalink
Handle exceptions thrown from postcss when calling adaptCssForReplay
Browse files Browse the repository at this point in the history
  • Loading branch information
guntherjh authored and Vadman97 committed Oct 29, 2024
1 parent 96e180d commit 2f831c5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/angry-turtles-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb-snapshot": patch
---

Handle exceptions thrown from postcss when calling adaptCssForReplay
16 changes: 11 additions & 5 deletions packages/rrweb-snapshot/src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,17 @@ export function adaptCssForReplay(cssText: string, cache: BuildCache): string {
const cachedStyle = cache?.stylesWithHoverClass.get(cssText);
if (cachedStyle) return cachedStyle;

const ast: { css: string } = postcss([
mediaSelectorPlugin,
pseudoClassPlugin,
]).process(cssText);
const result = ast.css;
let result = cssText;
try {
const ast: { css: string } = postcss([
mediaSelectorPlugin,
pseudoClassPlugin,
]).process(cssText);
result = ast.css;
} catch (error) {
console.warn('Failed to adapt css for replay', error);
}

cache?.stylesWithHoverClass.set(cssText, result);
return result;
}
Expand Down
16 changes: 15 additions & 1 deletion packages/rrweb-snapshot/test/rebuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
import * as fs from 'fs';
import * as path from 'path';
import { beforeEach, describe, expect as _expect, it } from 'vitest';
import { beforeEach, describe, expect as _expect, it, vi } from 'vitest';
import {
adaptCssForReplay,
buildNodeWithSN,
createCache,
} from '../src/rebuild';
import { NodeType } from '../src/types';
import { createMirror, Mirror } from '../src/utils';
import postcss from 'postcss';

const expect = _expect as unknown as {
<T = unknown>(actual: T): {
Expand Down Expand Up @@ -248,4 +249,17 @@ ul li.specified c.\\:hover img {
should_not_modify,
);
});

it('handles exceptions from postcss when calling adaptCssForReplay', () => {
const consoleWarnSpy = vi
.spyOn(console, 'warn')
.mockImplementation(() => {});
// trigger CssSyntaxError by passing invalid css
const cssText = 'a{';
adaptCssForReplay(cssText, cache);
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
'Failed to adapt css for replay',
expect.any(Error),
);
});
});

0 comments on commit 2f831c5

Please sign in to comment.