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