aboutsummaryrefslogtreecommitdiff
path: root/integration
diff options
context:
space:
mode:
Diffstat (limited to 'integration')
-rw-r--r--integration/bunjs-only-snippets/escapeHTML.test.js68
1 files changed, 59 insertions, 9 deletions
diff --git a/integration/bunjs-only-snippets/escapeHTML.test.js b/integration/bunjs-only-snippets/escapeHTML.test.js
index 13ff138c9..6c709bf76 100644
--- a/integration/bunjs-only-snippets/escapeHTML.test.js
+++ b/integration/bunjs-only-snippets/escapeHTML.test.js
@@ -2,7 +2,19 @@ import { describe, it, expect } from "bun:test";
import { gcTick } from "./gc";
describe("escapeHTML", () => {
+ // The matrix of cases we need to test for:
+ // 1. Works with short strings
+ // 2. Works with long strings
+ // 3. Works with latin1 strings
+ // 4. Works with utf16 strings
+ // 5. Works when the text to escape is somewhere in the middle
+ // 6. Works when the text to escape is in the beginning
+ // 7. Works when the text to escape is in the end
+ // 8. Returns the same string when there's no need to escape
it("works", () => {
+ expect(escapeHTML("absolutely nothing to do here")).toBe(
+ "absolutely nothing to do here"
+ );
expect(escapeHTML("<script>alert(1)</script>")).toBe(
"&lt;script&gt;alert(1)&lt;/script&gt;"
);
@@ -18,16 +30,10 @@ describe("escapeHTML", () => {
expect(escapeHTML("\v")).toBe("\v");
expect(escapeHTML("\b")).toBe("\b");
expect(escapeHTML("\u00A0")).toBe("\u00A0");
+ expect(escapeHTML("<script>ab")).toBe("&lt;script&gt;ab");
+ expect(escapeHTML("<script>")).toBe("&lt;script&gt;");
+ expect(escapeHTML("<script><script>")).toBe("&lt;script&gt;&lt;script&gt;");
- // The matrix of cases we need to test for:
- // 1. Works with short strings
- // 2. Works with long strings
- // 3. Works with latin1 strings
- // 4. Works with utf16 strings
- // 5. Works when the text to escape is somewhere in the middle
- // 6. Works when the text to escape is in the beginning
- // 7. Works when the text to escape is in the end
- // 8. Returns the same string when there's no need to escape
expect(escapeHTML("lalala" + "<script>alert(1)</script>" + "lalala")).toBe(
"lalala&lt;script&gt;alert(1)&lt;/script&gt;lalala"
);
@@ -39,6 +45,13 @@ describe("escapeHTML", () => {
"lalala" + "&lt;script&gt;alert(1)&lt;/script&gt;"
);
+ expect(escapeHTML("What does ๐Ÿ˜Š mean?")).toBe("What does ๐Ÿ˜Š mean?");
+ const output = escapeHTML("<What does ๐Ÿ˜Š");
+ expect(output).toBe("&lt;What does ๐Ÿ˜Š");
+ expect(escapeHTML("<div>What does ๐Ÿ˜Š mean in text?")).toBe(
+ "&lt;div&gt;What does ๐Ÿ˜Š mean in text?"
+ );
+
expect(
escapeHTML(
("lalala" + "<script>alert(1)</script>" + "lalala").repeat(900)
@@ -50,5 +63,42 @@ describe("escapeHTML", () => {
expect(
escapeHTML(("lalala" + "<script>alert(1)</script>").repeat(900))
).toBe(("lalala" + "&lt;script&gt;alert(1)&lt;/script&gt;").repeat(900));
+
+ // the positions of the unicode codepoint are important
+ // our simd code for U16 is at 8 bytes, so we need to especially check the boundaries
+ expect(
+ escapeHTML("๐Ÿ˜Šlalala" + "<script>alert(1)</script>" + "lalala")
+ ).toBe("๐Ÿ˜Šlalala&lt;script&gt;alert(1)&lt;/script&gt;lalala");
+ expect(escapeHTML("<script>๐Ÿ˜Šalert(1)</script>" + "lalala")).toBe(
+ "&lt;script&gt;๐Ÿ˜Šalert(1)&lt;/script&gt;lalala"
+ );
+ expect(escapeHTML("<script>alert(1)๐Ÿ˜Š</script>" + "lalala")).toBe(
+ "&lt;script&gt;alert(1)๐Ÿ˜Š&lt;/script&gt;lalala"
+ );
+ expect(escapeHTML("<script>alert(1)</script>" + "๐Ÿ˜Šlalala")).toBe(
+ "&lt;script&gt;alert(1)&lt;/script&gt;๐Ÿ˜Šlalala"
+ );
+ expect(escapeHTML("<script>alert(1)</script>" + "lal๐Ÿ˜Šala")).toBe(
+ "&lt;script&gt;alert(1)&lt;/script&gt;lal๐Ÿ˜Šala"
+ );
+ expect(
+ escapeHTML("<script>alert(1)</script>" + "lal๐Ÿ˜Šala".repeat(10))
+ ).toBe("&lt;script&gt;alert(1)&lt;/script&gt;" + "lal๐Ÿ˜Šala".repeat(10));
+
+ for (let i = 1; i < 10; i++)
+ expect(escapeHTML("<script>alert(1)</script>" + "la๐Ÿ˜Š".repeat(i))).toBe(
+ "&lt;script&gt;alert(1)&lt;/script&gt;" + "la๐Ÿ˜Š".repeat(i)
+ );
+
+ expect(escapeHTML("la๐Ÿ˜Š" + "<script>alert(1)</script>")).toBe(
+ "la๐Ÿ˜Š" + "&lt;script&gt;alert(1)&lt;/script&gt;"
+ );
+ expect(
+ escapeHTML(("lalala" + "<script>alert(1)</script>๐Ÿ˜Š").repeat(1))
+ ).toBe(("lalala" + "&lt;script&gt;alert(1)&lt;/script&gt;๐Ÿ˜Š").repeat(1));
+
+ expect(escapeHTML("๐Ÿ˜Š".repeat(100))).toBe("๐Ÿ˜Š".repeat(100));
+ expect(escapeHTML("๐Ÿ˜Š<".repeat(100))).toBe("๐Ÿ˜Š&lt;".repeat(100));
+ expect(escapeHTML("<๐Ÿ˜Š>".repeat(100))).toBe("&lt;๐Ÿ˜Š&gt;".repeat(100));
});
});