|
8 | 8 | replaceChromeGridTemplateAreas, |
9 | 9 | fixSafariColons, |
10 | 10 | isNodeMetaEqual, |
| 11 | + stringifyStylesheet |
11 | 12 | } from '../src/utils'; |
12 | 13 | import { NodeType } from '@rrweb/types'; |
13 | 14 | import type { serializedNode, serializedNodeWithId } from '@rrweb/types'; |
@@ -374,4 +375,53 @@ describe('utils', () => { |
374 | 375 | expect(out3).toEqual('[data-aa\\:other] { color: red; }'); |
375 | 376 | }); |
376 | 377 | }); |
| 378 | + |
| 379 | + describe('stringifyStylesheet', () => { |
| 380 | + it('returns null if rules are missing', () => { |
| 381 | + const mockSheet = { |
| 382 | + rules: null, |
| 383 | + cssRules: null, |
| 384 | + } as unknown as CSSStyleSheet; |
| 385 | + expect(stringifyStylesheet(mockSheet)).toBeNull(); |
| 386 | + }); |
| 387 | + |
| 388 | + it('stringifies rules using .cssRules if .rules is missing', () => { |
| 389 | + const mockRule1 = { cssText: 'div { margin: 0; }' } as CSSRule; |
| 390 | + const mockSheet = { |
| 391 | + cssRules: [mockRule1], |
| 392 | + href: 'https://example.com/main.css', |
| 393 | + } as unknown as CSSStyleSheet; |
| 394 | + expect(stringifyStylesheet(mockSheet)).toBe('div { margin: 0; }'); |
| 395 | + }); |
| 396 | + |
| 397 | + it('uses ownerNode.ownerDocument.baseURI for inline styles', () => { |
| 398 | + const mockFontFaceRule = { |
| 399 | + cssText: ` |
| 400 | + @font-face { |
| 401 | + font-family: 'MockFont'; |
| 402 | + src: url('../fonts/mockfont.woff2') format('woff2'); |
| 403 | + font-weight: normal; |
| 404 | + font-style: normal; |
| 405 | + } |
| 406 | + ` |
| 407 | + } as CSSRule; |
| 408 | + const mockOwnerDocument = { |
| 409 | + location: { href: 'https://example.com/page.html' }, |
| 410 | + baseURI: 'https://example.com/fonts/', |
| 411 | + } as unknown as Document; |
| 412 | + const mockOwnerNode = { |
| 413 | + ownerDocument: mockOwnerDocument, |
| 414 | + } as unknown as Node; |
| 415 | + const mockSheet = { |
| 416 | + cssRules: [mockFontFaceRule], |
| 417 | + href: null, |
| 418 | + ownerNode: mockOwnerNode, |
| 419 | + } as unknown as CSSStyleSheet; |
| 420 | + expect( |
| 421 | + stringifyStylesheet(mockSheet)?.replace(/\s+/g, ' ').trim() |
| 422 | + ).toEqual( |
| 423 | + "@font-face { font-family: 'MockFont'; src: url('https://example.com/fonts/mockfont.woff2') format('woff2'); font-weight: normal; font-style: normal; }" |
| 424 | + ); |
| 425 | + }); |
| 426 | + }); |
377 | 427 | }); |
0 commit comments