diff --git a/plugins/escape.ts b/plugins/escape.ts index ccca019..e712967 100644 --- a/plugins/escape.ts +++ b/plugins/escape.ts @@ -3,6 +3,8 @@ import type { Environment } from "../src/environment.ts"; export default function () { return (env: Environment) => { - env.filters.escape = html.escape; + // deno-lint-ignore no-explicit-any + env.filters.escape = (value: any) => + value ? html.escape(value.toString()) : ""; }; } diff --git a/plugins/unescape.ts b/plugins/unescape.ts index 222b0ae..130ab52 100644 --- a/plugins/unescape.ts +++ b/plugins/unescape.ts @@ -3,6 +3,8 @@ import type { Environment } from "../src/environment.ts"; export default function () { return (env: Environment) => { - env.filters.unescape = html.unescape; + // deno-lint-ignore no-explicit-any + env.filters.unescape = (value: any) => + value ? html.unescape(value.toString()) : ""; }; } diff --git a/test/escape.test.ts b/test/escape.test.ts index e8e561b..a20a443 100644 --- a/test/escape.test.ts +++ b/test/escape.test.ts @@ -35,3 +35,33 @@ Deno.test("Escape by default", async () => { expected: "<h1>Hello world</h1>", }); }); + +Deno.test("Escape non-string", async () => { + await test({ + template: ` + {{ 100 |> escape }} + `, + expected: "100", + }); + testSync({ + template: ` + {{ 100 |> escape }} + `, + expected: "100", + }); +}); + +Deno.test("Escape undefined", async () => { + await test({ + template: ` + {{ undefined |> escape }} + `, + expected: "", + }); + testSync({ + template: ` + {{ undefined |> escape }} + `, + expected: "", + }); +}); diff --git a/test/unescape.test.ts b/test/unescape.test.ts index d9440bb..6a4c0d5 100644 --- a/test/unescape.test.ts +++ b/test/unescape.test.ts @@ -15,7 +15,7 @@ Deno.test("Unescape filter", async () => { }); }); -Deno.test("Escape by default", async () => { +Deno.test("Unescape with escape by default", async () => { await test({ options: { autoescape: true, @@ -35,3 +35,33 @@ Deno.test("Escape by default", async () => { expected: "

Hello world

", }); }); + +Deno.test("Unescape non-string", async () => { + await test({ + template: ` + {{ 100 |> unescape }} + `, + expected: "100", + }); + testSync({ + template: ` + {{ 100 |> unescape }} + `, + expected: "100", + }); +}); + +Deno.test("Unescape undefined", async () => { + await test({ + template: ` + {{ undefined |> unescape }} + `, + expected: "", + }); + testSync({ + template: ` + {{ undefined |> unescape }} + `, + expected: "", + }); +});