Use node.baseURI for stringifying stylesheet hrefs (#1705)
This commit is contained in:
5
.changeset/lucky-trainers-joke.md
Normal file
5
.changeset/lucky-trainers-joke.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"rrweb-snapshot": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
use ownerNode.baseURI for stringifying sheet hrefs
|
||||||
@@ -118,9 +118,9 @@ export function stringifyStylesheet(s: CSSStyleSheet): string | null {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
let sheetHref = s.href;
|
let sheetHref = s.href;
|
||||||
if (!sheetHref && s.ownerNode && s.ownerNode.ownerDocument) {
|
if (!sheetHref && s.ownerNode) {
|
||||||
// an inline <style> element
|
// an inline <style> element
|
||||||
sheetHref = s.ownerNode.ownerDocument.location.href;
|
sheetHref = s.ownerNode.baseURI;
|
||||||
}
|
}
|
||||||
const stringifiedRules = Array.from(rules, (rule: CSSRule) =>
|
const stringifiedRules = Array.from(rules, (rule: CSSRule) =>
|
||||||
stringifyRule(rule, sheetHref),
|
stringifyRule(rule, sheetHref),
|
||||||
|
|||||||
@@ -280,4 +280,49 @@ describe('utils', () => {
|
|||||||
expect(out3).toEqual('[data-aa\\:other] { color: red; }');
|
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; }",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user