forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

rich-text.test.tsx27 lines, 1.4 KiBHistoryRawDownload
1import { isRichTextEmpty, richTextExcerpt, sanitizeRichText, toRichTextHtml } from "@/lib/rich-text";
2
3describe("rich-text", () => {
4 it("strips scripts, handlers and unknown tags but keeps formatting", () => {
5 const html = sanitizeRichText('<h2>Plan</h2><p onclick="x()">Hi <strong>there</strong><img src=x onerror=alert(1)><script>alert(1)</script></p>');
6 expect(html).toBe("<h2>Plan</h2><p>Hi <strong>there</strong></p>");
7 });
8
9 it("drops javascript links and opens real ones in a new tab", () => {
10 expect(sanitizeRichText('<a href="javascript:alert(1)">x</a>')).not.toContain("javascript");
11 expect(sanitizeRichText('<a href="https://example.com">x</a>')).toBe('<a href="https://example.com" target="_blank" rel="noopener noreferrer">x</a>');
12 });
13
14 it("turns imported markdown-ish text into blocks", () => {
15 expect(toRichTextHtml("## Unit 1\n\nFirst line\nsecond line\n\n- a\n- b\n1. one")).toBe(
16 "<h2>Unit 1</h2><p>First line<br>second line</p><ul><li>a</li><li>b</li></ul><ol><li>one</li></ol>",
17 );
18 expect(toRichTextHtml("a < b")).toBe("<p>a &lt; b</p>");
19 });
20
21 it("reads excerpts and emptiness from the text, not the markup", () => {
22 expect(richTextExcerpt("<h2>Title</h2><p>Body text</p>")).toBe("Title Body text");
23 expect(richTextExcerpt("<p>abcdef</p>", 3)).toBe("abc…");
24 expect(isRichTextEmpty("<p><br></p>")).toBe(true);
25 expect(isRichTextEmpty("<p>x</p>")).toBe(false);
26 });
27});