|
7 | 7 | extractFileExtension,
|
8 | 8 | fixSafariColons,
|
9 | 9 | isNodeMetaEqual,
|
| 10 | + stringifyStylesheet, |
10 | 11 | } from '../src/utils';
|
11 | 12 | import { NodeType } from '@posthog/rrweb-types';
|
12 | 13 | import type {
|
@@ -283,4 +284,53 @@ describe('utils', () => {
|
283 | 284 | expect(out3).toEqual('[data-aa\\:other] { color: red; }');
|
284 | 285 | });
|
285 | 286 | });
|
| 287 | + |
| 288 | + describe('stringifyStylesheet', () => { |
| 289 | + it('returns null if rules are missing', () => { |
| 290 | + const mockSheet = { |
| 291 | + rules: null, |
| 292 | + cssRules: null, |
| 293 | + } as unknown as CSSStyleSheet; |
| 294 | + expect(stringifyStylesheet(mockSheet)).toBeNull(); |
| 295 | + }); |
| 296 | + |
| 297 | + it('stringifies rules using .cssRules if .rules is missing', () => { |
| 298 | + const mockRule1 = { cssText: 'div { margin: 0; }' } as CSSRule; |
| 299 | + const mockSheet = { |
| 300 | + cssRules: [mockRule1], |
| 301 | + href: 'https://example.com/main.css', |
| 302 | + } as unknown as CSSStyleSheet; |
| 303 | + expect(stringifyStylesheet(mockSheet)).toBe('div { margin: 0; }'); |
| 304 | + }); |
| 305 | + |
| 306 | + it('uses ownerNode.ownerDocument.baseURI for inline styles', () => { |
| 307 | + const mockFontFaceRule = { |
| 308 | + cssText: ` |
| 309 | + @font-face { |
| 310 | + font-family: 'MockFont'; |
| 311 | + src: url('../fonts/mockfont.woff2') format('woff2'); |
| 312 | + font-weight: normal; |
| 313 | + font-style: normal; |
| 314 | + } |
| 315 | + `, |
| 316 | + } as CSSRule; |
| 317 | + const mockOwnerDocument = { |
| 318 | + location: { href: 'https://example.com/page.html' }, |
| 319 | + baseURI: 'https://example.com/fonts/', |
| 320 | + } as unknown as Document; |
| 321 | + const mockOwnerNode = { |
| 322 | + ownerDocument: mockOwnerDocument, |
| 323 | + } as unknown as Node; |
| 324 | + const mockSheet = { |
| 325 | + cssRules: [mockFontFaceRule], |
| 326 | + href: null, |
| 327 | + ownerNode: mockOwnerNode, |
| 328 | + } as unknown as CSSStyleSheet; |
| 329 | + expect( |
| 330 | + stringifyStylesheet(mockSheet)?.replace(/\s+/g, ' ').trim(), |
| 331 | + ).toEqual( |
| 332 | + "@font-face { font-family: 'MockFont'; src: url('https://example.com/fonts/mockfont.woff2') format('woff2'); font-weight: normal; font-style: normal; }", |
| 333 | + ); |
| 334 | + }); |
| 335 | + }); |
286 | 336 | });
|
0 commit comments