fix: regression caused by security hardening causing clickable icons to not work.

This commit is contained in:
Alphons Joseph committed 2026-09-01 15:39:19 +08:00
1 parent 9f80ba11f1
commit 8df7edcb75
3 files changed
+16 -2

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testEnvironment: 'jsdom',
roots: ['<rootDir>/src'],
testMatch: [
'**/__tests__/**/*.ts',
+13
View File
@@ -0,0 +1,13 @@
import stringToHTML from "./stringToHTML";
describe("stringToHTML", () => {
it("preserves inline onclick handlers used by lesson action buttons", () => {
const body = stringToHTML(
`<div class="day-button clickable" onclick="location.href='../#?page=/courses/123:456'">Go</div>`,
);
const el = body.firstElementChild as HTMLElement | null;
expect(el).not.toBeNull();
expect(el?.getAttribute("onclick")).toContain("location.href");
});
});
+2 -1
View File
@@ -16,8 +16,9 @@ export default function stringToHTML(str: string, styles = false) {
const parser = new DOMParser();
str = DOMPurify.sanitize(str, {
ADD_ATTR: ["onclick"],
ALLOWED_URI_REGEXP:
/^(?:(?:https?|mailto|tel):|\/|#|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp|chrome-extension):|\/|#|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i,
});
const doc = parser.parseFromString(str, "text/html");