import { describe, it, expect } from "vitest"; import { KcSanitizer } from "keycloakify/lib/kcSanitize/KcSanitizer"; import { decode } from "html-entities"; import DOMPurify from "isomorphic-dompurify"; // Implementation of Keycloak Java method KeycloakSanitizerTest with bunch of more test for p tag styling // https://github.com/keycloak/keycloak/blob/8ce8a4ba089eef25a0e01f58e09890399477b9ef/services/src/test/java/org/keycloak/theme/KeycloakSanitizerTest.java#L32 describe("KeycloakSanitizerMethod", () => { it("should handle escapes correctly", () => { let html: string = ""; let expectedResult: string; html = "
Keycloak
"; expectedResult = '
Keycloak
'; assertResult(expectedResult, html); html = "

Foo

"; expectedResult = "

Foo

"; assertResult(expectedResult, html); html = '
Keycloak
'; expectedResult = '
Keycloak
'; assertResult(expectedResult, html); html = ""; expectedResult = ""; assertResult(expectedResult, html); }); it("should handle URLs correctly", () => { let html: string = ""; html = "

link

"; assertResult('

link

', html); html = '

link

'; assertResult("

link

", html); html = "

link

"; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult( '

link

', html ); html = "

link1link2

"; assertResult( '

link1link2

', html ); }); it("should handle ordinary texts correctly", () => { let html: string = ""; html = "Some text"; assertResult("Some text", html); html = `text with "double quotation"`; assertResult(`text with "double quotation"`, html); html = `text with 'single quotation'`; assertResult(`text with 'single quotation'`, html); }); it("should handle text styles correctly", () => { let html: string = ""; html = "

text

"; assertResult("

text

", html); html = "

text

"; assertResult("

text

", html); html = `

red text

`; assertResult(`

red text

`, html); html = `

red text

`; assertResult(`

red text

`, html); html = `

Case-insensitive

`; assertResult(`

Case-insensitive

`, html); html = `

wrong value for align

`; assertResult(`

wrong value for align

`, html); html = `

wrong value for align

`; assertResult(`

wrong value for align

`, html); html = `

This is a paragraph with larger text.

`; assertResult( `

This is a paragraph with larger text.

`, html ); html = `

או נושא שתבחר

`; assertResult(`

או נושא שתבחר

`, html); }); it("should handle styles correctly", () => { let html = ""; html = `
`; assertResult(`
`, html); html = `
`; assertResult(`
`, html); html = ` Content `; assertResult(` Content `, html); }); function assertResult(expectedResult: string, html: string): void { const result = KcSanitizer.sanitize(html, { DOMPurify: DOMPurify as any, htmlEntitiesDecode: decode }); expect(result).toBe(expectedResult); } });