Skip to content

fix: use document.baseURI for sheetHref when handling inline styles #1700

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

Closed
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
5 changes: 5 additions & 0 deletions .changeset/angry-roses-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb-snapshot": patch
---

use ownerNode baseURI for stringifying stylesheet href
5 changes: 5 additions & 0 deletions .changeset/twelve-nails-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb-snapshot": patch
---

Prefer the <base href> for resolving inline <style> URLs, falling back to document location if not present.
4 changes: 2 additions & 2 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 Report Analysis

packages/rrweb-snapshot/src/utils.ts#L83

[tsdoc/syntax] 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 @@ -118,9 +118,9 @@
return null;
}
let sheetHref = s.href;
if (!sheetHref && s.ownerNode && s.ownerNode.ownerDocument) {
if (!sheetHref && s.ownerNode) {
// an inline <style> element
sheetHref = s.ownerNode.ownerDocument.location.href;
sheetHref = s.ownerNode.baseURI;
}
const stringifiedRules = Array.from(rules, (rule: CSSRule) =>
stringifyRule(rule, sheetHref),
Expand Down Expand Up @@ -488,7 +488,7 @@
typeof childNodes[i].textContent === 'string'
) {
const textContentNorm = normalizeCssString(
childNodes[i].textContent!,

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb-snapshot/src/utils.ts#L491

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
_testNoPxNorm,
);
const jLimit = 100; // how many iterations for the first part of searching
Expand Down
46 changes: 46 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 '@rrweb/types';
import type { serializedNode, serializedNodeWithId } from '@rrweb/types';
Expand Down Expand Up @@ -280,4 +281,49 @@ 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.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 mockOwnerNode = {
baseURI: 'https://example.com/fonts/',
} 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