Skip to content

Commit f26eacf

Browse files
committed
Rename absoluteToStylesheet to absolutifyURLs and call it once after stringifying imported stylesheet
1 parent f69e8f3 commit f26eacf

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

packages/rrweb-snapshot/src/snapshot.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
getInputType,
2626
toLowerCase,
2727
extractFileExtension,
28-
absoluteToStylesheet,
28+
absolutifyURLs,
2929
} from './utils';
3030

3131
let _id = 1;
@@ -193,7 +193,7 @@ export function transformAttribute(
193193
} else if (name === 'srcset') {
194194
return getAbsoluteSrcsetString(doc, value);
195195
} else if (name === 'style') {
196-
return absoluteToStylesheet(value, getHref(doc));
196+
return absolutifyURLs(value, getHref(doc));
197197
} else if (tagName === 'object' && name === 'data') {
198198
return absoluteToDoc(doc, value);
199199
}
@@ -523,7 +523,7 @@ function serializeTextNode(
523523
n,
524524
);
525525
}
526-
textContent = absoluteToStylesheet(textContent, getHref(options.doc));
526+
textContent = absolutifyURLs(textContent, getHref(options.doc));
527527
}
528528
if (isScript) {
529529
textContent = 'SCRIPT_PLACEHOLDER';

packages/rrweb-snapshot/src/utils.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ export function escapeImportStatement(rule: CSSImportRule): string {
9696
export function stringifyStylesheet(s: CSSStyleSheet): string | null {
9797
try {
9898
const rules = s.rules || s.cssRules;
99-
const stringifiedRules = Array.from(rules, stringifyRule)
100-
.map((rule) => {
101-
return s.href ? absoluteToStylesheet(rule, s.href) : rule;
102-
})
99+
// const stringifiedRules = [];
100+
// for (let i = 0; i < rules.length; i++) {
101+
// stringifiedRules.push(stringifyRule(rules[i], s.href));
102+
// }
103+
const stringifiedRules = [...rules]
104+
.map((rule: CSSRule) => stringifyRule(rule, s.href))
103105
.join('');
104106

105107
return rules ? fixBrowserCompatibilityIssuesInCSS(stringifiedRules) : null;
@@ -108,7 +110,7 @@ export function stringifyStylesheet(s: CSSStyleSheet): string | null {
108110
}
109111
}
110112

111-
export function stringifyRule(rule: CSSRule): string {
113+
export function stringifyRule(rule: CSSRule, sheetHref: string | null): string {
112114
let importStringified;
113115
if (isCSSImportRule(rule)) {
114116
try {
@@ -118,6 +120,10 @@ export function stringifyRule(rule: CSSRule): string {
118120
stringifyStylesheet(rule.styleSheet) ||
119121
// work around browser issues with the raw string `@import url(...)` statement
120122
escapeImportStatement(rule);
123+
124+
if (sheetHref) {
125+
importStringified = absolutifyURLs(importStringified, sheetHref);
126+
}
121127
} catch (error) {
122128
// ignore
123129
}
@@ -369,10 +375,7 @@ const URL_IN_CSS_REF = /url\((?:(')([^']*)'|(")(.*?)"|([^)]*))\)/gm;
369375
const URL_PROTOCOL_MATCH = /^(?:[a-z+]+:)?\/\//i;
370376
const URL_WWW_MATCH = /^www\..*/i;
371377
const DATA_URI = /^(data:)([^,]*),(.*)/i;
372-
export function absoluteToStylesheet(
373-
cssText: string | null,
374-
href: string,
375-
): string {
378+
export function absolutifyURLs(cssText: string | null, href: string): string {
376379
return (cssText || '').replace(
377380
URL_IN_CSS_REF,
378381
(

packages/rrweb-snapshot/test/snapshot.test.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,56 @@ import { describe, it, expect } from 'vitest';
66
import { serializeNodeWithId, _isBlockedElement } from '../src/snapshot';
77
import snapshot from '../src/snapshot';
88
import { serializedNodeWithId, elementNode } from '../src/types';
9-
import { Mirror, absoluteToStylesheet } from '../src/utils';
9+
import { Mirror, absolutifyURLs } from '../src/utils';
1010

1111
describe('absolute url to stylesheet', () => {
1212
const href = 'http://localhost/css/style.css';
1313

1414
it('can handle relative path', () => {
15-
expect(absoluteToStylesheet('url(a.jpg)', href)).toEqual(
15+
expect(absolutifyURLs('url(a.jpg)', href)).toEqual(
1616
`url(http://localhost/css/a.jpg)`,
1717
);
1818
});
1919

2020
it('can handle same level path', () => {
21-
expect(absoluteToStylesheet('url("./a.jpg")', href)).toEqual(
21+
expect(absolutifyURLs('url("./a.jpg")', href)).toEqual(
2222
`url("http://localhost/css/a.jpg")`,
2323
);
2424
});
2525

2626
it('can handle parent level path', () => {
27-
expect(absoluteToStylesheet('url("../a.jpg")', href)).toEqual(
27+
expect(absolutifyURLs('url("../a.jpg")', href)).toEqual(
2828
`url("http://localhost/a.jpg")`,
2929
);
3030
});
3131

3232
it('can handle absolute path', () => {
33-
expect(absoluteToStylesheet('url("/a.jpg")', href)).toEqual(
33+
expect(absolutifyURLs('url("/a.jpg")', href)).toEqual(
3434
`url("http://localhost/a.jpg")`,
3535
);
3636
});
3737

3838
it('can handle external path', () => {
39-
expect(absoluteToStylesheet('url("http://localhost/a.jpg")', href)).toEqual(
39+
expect(absolutifyURLs('url("http://localhost/a.jpg")', href)).toEqual(
4040
`url("http://localhost/a.jpg")`,
4141
);
4242
});
4343

4444
it('can handle single quote path', () => {
45-
expect(absoluteToStylesheet(`url('./a.jpg')`, href)).toEqual(
45+
expect(absolutifyURLs(`url('./a.jpg')`, href)).toEqual(
4646
`url('http://localhost/css/a.jpg')`,
4747
);
4848
});
4949

5050
it('can handle no quote path', () => {
51-
expect(absoluteToStylesheet('url(./a.jpg)', href)).toEqual(
51+
expect(absolutifyURLs('url(./a.jpg)', href)).toEqual(
5252
`url(http://localhost/css/a.jpg)`,
5353
);
5454
});
5555

5656
it('can handle multiple no quote paths', () => {
5757
expect(
58-
absoluteToStylesheet(
58+
absolutifyURLs(
5959
'background-image: url(images/b.jpg);background: #aabbcc url(images/a.jpg) 50% 50% repeat;',
6060
href,
6161
),
@@ -66,11 +66,11 @@ describe('absolute url to stylesheet', () => {
6666
});
6767

6868
it('can handle data url image', () => {
69+
expect(absolutifyURLs('url(data:image/gif;base64,ABC)', href)).toEqual(
70+
'url(data:image/gif;base64,ABC)',
71+
);
6972
expect(
70-
absoluteToStylesheet('url(data:image/gif;base64,ABC)', href),
71-
).toEqual('url(data:image/gif;base64,ABC)');
72-
expect(
73-
absoluteToStylesheet(
73+
absolutifyURLs(
7474
'url(data:application/font-woff;base64,d09GMgABAAAAAAm)',
7575
href,
7676
),
@@ -79,23 +79,23 @@ describe('absolute url to stylesheet', () => {
7979

8080
it('preserves quotes around inline svgs with spaces', () => {
8181
expect(
82-
absoluteToStylesheet(
82+
absolutifyURLs(
8383
"url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")",
8484
href,
8585
),
8686
).toEqual(
8787
"url(\"data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Cpath fill='%2328a745' d='M3'/%3E%3C/svg%3E\")",
8888
);
8989
expect(
90-
absoluteToStylesheet(
90+
absolutifyURLs(
9191
'url(\'data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>\')',
9292
href,
9393
),
9494
).toEqual(
9595
'url(\'data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>\')',
9696
);
9797
expect(
98-
absoluteToStylesheet(
98+
absolutifyURLs(
9999
'url("data:image/svg+xml;utf8,<svg width="28" height="32" viewBox="0 0 28 32" xmlns="http://www.w3.org/2000/svg"><path d="M27 14C28" fill="white"/></svg>")',
100100
href,
101101
),
@@ -104,7 +104,7 @@ describe('absolute url to stylesheet', () => {
104104
);
105105
});
106106
it('can handle empty path', () => {
107-
expect(absoluteToStylesheet(`url('')`, href)).toEqual(`url('')`);
107+
expect(absolutifyURLs(`url('')`, href)).toEqual(`url('')`);
108108
});
109109
});
110110

tsconfig.base.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"moduleResolution": "Node",
1111
"rootDir": "src",
1212
"outDir": "dist",
13-
"lib": ["es6", "dom"],
13+
"lib": ["es6", "dom", "dom.iterable"],
1414
"sourceMap": true,
1515
"skipLibCheck": true,
1616
"declaration": true,

0 commit comments

Comments
 (0)