Open History to see the last commit
1import { expect, it } from "vite-plus/test";
2
3import { applyThemeCss } from "./themeCss";
4
5it("applies theme CSS and drops every rule that would load a remote file", () => {
6 const probe = document.createElement("div");
7 probe.dataset.themeCssProbe = "";
8 document.body.append(probe);
9 using _probe = { [Symbol.dispose]: () => probe.remove() };
10 using _css = { [Symbol.dispose]: () => applyThemeCss(undefined) };
11
12 applyThemeCss(`
13 @import url(https://example.com/sheet.css);
14 @namespace svg url(http://www.w3.org/2000/svg);
15 @property --registered { syntax: "<url>"; inherits: false; initial-value: url(https://example.com/d); }
16 @counter-style leak { system: cyclic; symbols: url(https://example.com/f); }
17 @keyframes leak { to { background-image: url(https://example.com/g); } }
18 :root { --leak: u\\72l(https://example.com/a); }
19 [data-theme-css-probe] {
20 color: rgb(1, 2, 3);
21 background-image: u\\72l(https://example.com/b);
22 list-style-image: var(--leak);
23 border-image-source: var(--registered);
24 mask-image: v\\61r(--none, u\\72l(https://example.com/e));
25 @media (min-width: 0px) { cursor: url(https://example.com/c), auto; }
26 }
27 [data-theme-css-probe] { background: var(--none, url(https://example.com/h)); }
28 @keyframes shorthand { to { border-image: var(--none, url(https://example.com/i)); } }
29 [data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; }
30 `);
31
32 const style = getComputedStyle(probe);
33 expect(style.color).toBe("rgb(1, 2, 3)");
34 expect(style.backgroundImage).toBe("none");
35 expect(style.listStyleImage).toBe("none");
36 expect(style.borderImageSource).toBe("none");
37 expect(style.maskImage).toBe("none");
38 const themeCssText = () =>
39 document.adoptedStyleSheets
40 .flatMap((sheet) => Array.from(sheet.cssRules, (rule) => rule.cssText))
41 .join("\n");
42 expect(themeCssText()).not.toContain("example.com");
43 expect(themeCssText()).toContain(
44 "background: var(--theme-background-image) center / cover no-repeat;",
45 );
46 expect(style.cursor).toBe("auto");
47
48 // A later longhand hides the shorthand's text from CSSOM, and the browser still loads its url().
49 applyThemeCss(`
50 [data-theme-css-probe] { mask: v\\61r(--none, u\\72l(https://example.com/k)); mask-size: auto; }
51 [data-theme-css-probe] { list-style: env(t3-none, url(https://example.com/l)); list-style-type: disc; }
52 @keyframes hidden { to { background: var(--none, url(https://example.com/m)); background-color: red; } }
53 [data-theme-css-probe] { border-image: var(--none, url(https://example.com/n)); border-image-slice: 1; }
54 [data-theme-css-probe] { animation: hidden 1s forwards paused; animation-delay: -1s; }
55 `);
56 expect(style.maskImage).toBe("none");
57 expect(style.listStyleImage).toBe("none");
58 expect(style.borderImageSource).toBe("none");
59 expect(style.backgroundImage).toBe("none");
60 expect(style.backgroundColor).toBe("rgb(255, 0, 0)");
61
62 // Every function that names a file is refused, in a font face and behind `all` too.
63 applyThemeCss(`
64 @font-face { font-family: remote; src: url(https://example.com/o.woff2); }
65 [data-theme-css-probe] { background: var(--none, image-set("https://example.com/p" 1x)); background-color: red; }
66 [data-theme-css-probe] { mask: var(--none, -webkit-cross-fade(url(https://example.com/q), url(https://example.com/r), 50%)); mask-size: auto; }
67 [data-theme-css-probe] { all: var(--none, url(https://example.com/s)); font-size: 12px; }
68 body { [data-theme-css-probe] { list-style: if(style(--none: 1): none; else: url(https://example.com/t)); list-style-type: disc; } }
69 `);
70 expect(themeCssText()).not.toContain("example.com");
71 expect(style.backgroundImage).toBe("none");
72 expect(style.maskImage).toBe("none");
73 expect(style.listStyleImage).toBe("none");
74
75 // The same shape without a fetch stays, whatever the rest of the sheet holds.
76 applyThemeCss(`
77 [data-theme-css-probe] {
78 --edge: 4px solid red; border: var(--edge); border-left-width: 0;
79 background: var(--none, linear-gradient(red, blue)); background-color: red;
80 }
81 .unused { c\\6flor: red; b\\61 ckground: var(--none); cursor: url(https://example.com/x), auto; }
82 .unused { background: var(--none, url(https://example.com/y)); background-size: auto; }
83 `);
84 expect(themeCssText()).not.toContain("example.com");
85 expect(style.borderTopWidth).toBe("4px");
86 expect(style.borderLeftWidth).toBe("0px");
87 expect(style.backgroundImage).toContain("linear-gradient");
88
89 applyThemeCss(undefined);
90 expect(getComputedStyle(probe).color).not.toBe("rgb(1, 2, 3)");
91});