Skip to content

Commit

Permalink
Merge pull request #36 from wrapperup/fix-escape
Browse files Browse the repository at this point in the history
Fix: Convert any value to string when escaping
  • Loading branch information
oscarotero authored Feb 26, 2024
2 parents 627b42f + beb7698 commit a921536
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
4 changes: 3 additions & 1 deletion plugins/escape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) : "";
};
}
4 changes: 3 additions & 1 deletion plugins/unescape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) : "";
};
}
30 changes: 30 additions & 0 deletions test/escape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
});
});
32 changes: 31 additions & 1 deletion test/unescape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,3 +35,33 @@ Deno.test("Escape by default", async () => {
expected: "<h1>Hello world</h1>",
});
});

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: "",
});
});

0 comments on commit a921536

Please sign in to comment.