diff --git a/components/ui/theme-script.ts b/components/ui/theme-script.ts index 6d0d49d..0146615 100644 --- a/components/ui/theme-script.ts +++ b/components/ui/theme-script.ts @@ -5,11 +5,29 @@ export const THEME_STORAGE_KEY = "rt.theme"; +const UNSAFE_SCRIPT_CHARS: Record = { + "<": "\\u003C", + ">": "\\u003E", + "/": "\\u002F", + "\u2028": "\\u2028", + "\u2029": "\\u2029", +}; + +/** + * Serialize a value as a JavaScript literal that is safe to embed inside an + * inline `\u2028\u2029"; + const literal = toInlineScriptLiteral(value); + expect(literal).not.toMatch(/[<>/\u2028\u2029]/); + expect(new Function(`return ${literal};`)()).toBe(value); + }); +}); + describe("ThemeToggle", () => { it("cycles the theme and updates its label", () => { render();