Skip to content

adopt a couple of rrweb proposed PRs #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
* Browsers sometimes incorrectly escape `@import` on `.cssText` statements.
* This function tries to correct the escaping.
* more info: https://bugs.chromium.org/p/chromium/issues/detail?id=1472259
* @param cssImportRule

Check warning on line 83 in packages/rrweb-snapshot/src/utils.ts

View workflow job for this annotation

GitHub Actions / ESLint Check and Report Upload

tsdoc-param-tag-missing-hyphen: The @param block should be followed by a parameter name and then a hyphen
* @returns `cssText` with browser inconsistencies fixed, or null if not applicable.
*/
export function escapeImportStatement(rule: CSSImportRule): string {
Expand Down Expand Up @@ -108,8 +108,13 @@
if (!rules) {
return null;
}
let sheetHref = s.href;
if (!sheetHref && s.ownerNode && s.ownerNode.ownerDocument) {
// an inline <style> element
sheetHref = s.ownerNode.ownerDocument.baseURI;
}
const stringifiedRules = Array.from(rules, (rule: CSSRule) =>
stringifyRule(rule, s.href),
stringifyRule(rule, sheetHref),
).join('');
return fixBrowserCompatibilityIssuesInCSS(stringifiedRules);
} catch (error) {
Expand All @@ -130,9 +135,17 @@
} catch (error) {
importStringified = rule.cssText;
}
if (rule.styleSheet.href) {
// url()s within the imported stylesheet are relative to _that_ sheet's href
return absolutifyURLs(importStringified, rule.styleSheet.href);
// if importStringified is not null,
// there should be a stylesheet and a rule here,
// but we avoid errors in this method by checking for null
// see https://github.com/rrweb-io/rrweb/pull/1686
try {
if (importStringified && rule.styleSheet?.href) {
// url()s within the imported stylesheet are relative to _that_ sheet's href
return absolutifyURLs(importStringified, rule.styleSheet.href);
}
} catch {
// swallow this, we'll return null
}
return importStringified;
} else {
Expand Down
50 changes: 50 additions & 0 deletions packages/rrweb-snapshot/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
extractFileExtension,
fixSafariColons,
isNodeMetaEqual,
stringifyStylesheet,
} from '../src/utils';
import { NodeType } from '@posthog/rrweb-types';
import type {
Expand Down Expand Up @@ -283,4 +284,53 @@ describe('utils', () => {
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
});
});

describe('stringifyStylesheet', () => {
it('returns null if rules are missing', () => {
const mockSheet = {
rules: null,
cssRules: null,
} as unknown as CSSStyleSheet;
expect(stringifyStylesheet(mockSheet)).toBeNull();
});

it('stringifies rules using .cssRules if .rules is missing', () => {
const mockRule1 = { cssText: 'div { margin: 0; }' } as CSSRule;
const mockSheet = {
cssRules: [mockRule1],
href: 'https://example.com/main.css',
} as unknown as CSSStyleSheet;
expect(stringifyStylesheet(mockSheet)).toBe('div { margin: 0; }');
});

it('uses ownerNode.ownerDocument.baseURI for inline styles', () => {
const mockFontFaceRule = {
cssText: `
@font-face {
font-family: 'MockFont';
src: url('../fonts/mockfont.woff2') format('woff2');
font-weight: normal;
font-style: normal;
}
`,
} as CSSRule;
const mockOwnerDocument = {
location: { href: 'https://example.com/page.html' },
baseURI: 'https://example.com/fonts/',
} as unknown as Document;
const mockOwnerNode = {
ownerDocument: mockOwnerDocument,
} as unknown as Node;
const mockSheet = {
cssRules: [mockFontFaceRule],
href: null,
ownerNode: mockOwnerNode,
} as unknown as CSSStyleSheet;
expect(
stringifyStylesheet(mockSheet)?.replace(/\s+/g, ' ').trim(),
).toEqual(
"@font-face { font-family: 'MockFont'; src: url('https://example.com/fonts/mockfont.woff2') format('woff2'); font-weight: normal; font-style: normal; }",
);
});
});
});
Loading