From 81fc9d57bdaa16249c0491d675981af2ddc02429 Mon Sep 17 00:00:00 2001 From: uchar Date: Wed, 18 Sep 2024 18:37:17 +0330 Subject: [PATCH] remove async from sanitize --- package.json | 3 +- src/tools/kcSanitize/KcSanitizer.ts | 19 ++++++------ test/kcSanitize/KcSanitizer.spec.ts | 45 ++++++++++++++--------------- yarn.lock | 5 ---- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 23f7c335..45311bdd 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,6 @@ "vite": "^5.2.11", "vitest": "^1.6.0", "yauzl": "^2.10.0", - "zod": "^3.17.10", - "html-entities": "2.5.2" + "zod": "^3.17.10" } } diff --git a/src/tools/kcSanitize/KcSanitizer.ts b/src/tools/kcSanitize/KcSanitizer.ts index 1be68d42..69c73e4e 100644 --- a/src/tools/kcSanitize/KcSanitizer.ts +++ b/src/tools/kcSanitize/KcSanitizer.ts @@ -6,30 +6,31 @@ export class KcSanitizer { private static HREF_PATTERN = /\s+href="([^"]*)"/g; private static textarea: HTMLTextAreaElement | null = null; - public static async sanitize(html: string | null): Promise { + public static sanitize(html: string | null): string { if (html == null) { throw new Error("Cannot escape null value."); } if (html === "") return ""; - html = await this.decodeHtmlFull(html); + html = this.decodeHtmlFull(html); const sanitized = KcSanitizerPolicy.sanitize(html); return this.fixURLs(sanitized); } - private static async decodeHtmlFull(html: string): Promise { + private static decodeHtmlFull(html: string): string { if (typeof window !== "undefined" && typeof document !== "undefined") { return KcSanitizer.decodeHtmlOnClient(html); } else { - return await KcSanitizer.decodeHtmlOnServer(html); + throw new Error("not implemented"); + // return await KcSanitizer.decodeHtmlOnServer(html); } } - private static async decodeHtmlOnServer(html: string): Promise { - // Dynamically import html-entities only on the server side - const { decode } = await import("html-entities"); - return decode(html); - } + // private static async decodeHtmlOnServer(html: string): Promise { + // // Dynamically import html-entities only on the server side + // const { decode } = await import("html-entities"); + // return decode(html); + // } private static decodeHtmlOnClient(html: string): string { if (!KcSanitizer.textarea) { diff --git a/test/kcSanitize/KcSanitizer.spec.ts b/test/kcSanitize/KcSanitizer.spec.ts index 09a72a70..2740a691 100644 --- a/test/kcSanitize/KcSanitizer.spec.ts +++ b/test/kcSanitize/KcSanitizer.spec.ts @@ -150,35 +150,29 @@ const testCases = [ ] } ]; -const assertResult = async ( - expectedResult: string | null, - html: string | null -): Promise => { +const assertResult = (expectedResult: string | null, html: string | null): void => { if (html === null) { - await expect(KcSanitizer.sanitize(html)).rejects.toThrow( - "Cannot escape null value." - ); + expect(KcSanitizer.sanitize(html)).toThrow("Cannot escape null value."); } else { - const result = await KcSanitizer.sanitize(html); + const result = KcSanitizer.sanitize(html); expect(result).toBe(expectedResult); } }; // Server-side tests -describe("KcSanitizer - Server Side", () => { - for (const group of testCases) { - describe(group.description, () => { - for (const test of group.cases) { - it(`should handle ${test.html}`, async () => { - await assertResult(test.expectedResult, test.html); - }); - } - }); - } -}); +// describe("KcSanitizer - Server Side", () => { +// for (const group of testCases) { +// describe(group.description, () => { +// for (const test of group.cases) { +// it(`should handle ${test.html}`, async () => { +// await assertResult(test.expectedResult, test.html); +// }); +// } +// }); +// } +// }); -// Client-side tests -describe("KcSanitizer - Client Side (jsdom)", () => { +describe("KcSanitizer - Client Side", () => { const decodeHtmlEntities = (html: string): string => { const entitiesMap: { [key: string]: string } = { "&": "&", @@ -195,6 +189,7 @@ describe("KcSanitizer - Client Side (jsdom)", () => { }; beforeAll(() => { + vi.stubGlobal("window", {}); // Mocking the `document.createElement` to simulate textarea behavior vi.stubGlobal("document", { createElement: (tagName: string) => { @@ -219,8 +214,12 @@ describe("KcSanitizer - Client Side (jsdom)", () => { for (const group of testCases) { describe(group.description, () => { for (const test of group.cases) { - it(`should handle ${test.html}`, async () => { - await assertResult(test.expectedResult, test.html); + it(`should handle ${test.html}`, () => { + if (test.html == null) + expect(() => + assertResult(test.expectedResult, test.html) + ).toThrow("Cannot escape null value."); + else assertResult(test.expectedResult, test.html); }); } }); diff --git a/yarn.lock b/yarn.lock index 364940a8..b451fb8c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6633,11 +6633,6 @@ html-encoding-sniffer@^4.0.0: dependencies: whatwg-encoding "^3.1.1" -html-entities@2.5.2: - version "2.5.2" - resolved "https://registry.yarnpkg.com/html-entities/-/html-entities-2.5.2.tgz#201a3cf95d3a15be7099521620d19dfb4f65359f" - integrity sha512-K//PSRMQk4FZ78Kyau+mZurHn3FH0Vwr+H36eE0rPbeYkRRi9YxceYPhuN60UwWorxyKHhqoAJl2OFKa4BVtaA== - html-entities@^2.1.0: version "2.3.3" resolved "https://registry.npmjs.org/html-entities/-/html-entities-2.3.3.tgz"