2024-01-01 21:11:32 +00:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-05-30 23:49:16 +00:00
|
|
|
|
|
|
|
|
|
import { escape, unescape } from "./entities.ts";
|
2023-07-13 07:04:30 +00:00
|
|
|
|
import { assertEquals } from "../assert/mod.ts";
|
2023-05-30 23:49:16 +00:00
|
|
|
|
import entityList from "./named_entity_list.json" assert { type: "json" };
|
|
|
|
|
|
2024-01-02 23:46:12 +00:00
|
|
|
|
Deno.test("escape()", async (t) => {
|
2023-05-30 23:49:16 +00:00
|
|
|
|
await t.step('escapes &<>"', () => {
|
|
|
|
|
assertEquals(escape("&<>'\""), "&<>'"");
|
|
|
|
|
});
|
|
|
|
|
await t.step("escapes ' to ' (not ')", () => {
|
|
|
|
|
assertEquals(escape("'"), "'");
|
|
|
|
|
});
|
|
|
|
|
await t.step("doesn't escape non-breaking space", () => {
|
|
|
|
|
assertEquals(escape("\xa0"), "\xa0");
|
|
|
|
|
});
|
|
|
|
|
await t.step(
|
|
|
|
|
"doesn't escape other characters, even if they have named entities",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(escape("þð"), "þð");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-02 23:46:12 +00:00
|
|
|
|
Deno.test("unescape()", async (t) => {
|
2023-05-30 23:49:16 +00:00
|
|
|
|
await t.step("round-trips with escape", () => {
|
|
|
|
|
const chars = "&<>'\"";
|
|
|
|
|
assertEquals(unescape(escape(chars)), chars);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await t.step("named entities", async (t) => {
|
|
|
|
|
await t.step("default options", async (t) => {
|
|
|
|
|
await t.step("unescapes ' as alias for ' '", () => {
|
|
|
|
|
assertEquals(unescape("'"), "'");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes ", () => {
|
|
|
|
|
assertEquals(unescape(" "), "\xa0");
|
|
|
|
|
});
|
|
|
|
|
await t.step("doesn't unescape other named entities", () => {
|
|
|
|
|
assertEquals(unescape("þð"), "þð");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await t.step("full entity list", async (t) => {
|
|
|
|
|
await t.step("unescapes arbitrary named entities", () => {
|
|
|
|
|
assertEquals(unescape("þð", { entityList }), "þð");
|
|
|
|
|
});
|
|
|
|
|
await t.step(
|
|
|
|
|
"unescapes truncated named entity (no trailing semicolon) if it is listed",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(unescape("&", { entityList }), "&");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
await t.step(
|
|
|
|
|
"consumes full named entity even when a truncated version is specified",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(unescape("&", { entityList }), "&");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
await t.step(
|
|
|
|
|
"doesn't unescape truncated named entity if it isn't listed",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(
|
|
|
|
|
unescape("∴ &therefore", { entityList }),
|
|
|
|
|
"∴ &therefore",
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-02 23:46:12 +00:00
|
|
|
|
await t.step("unescape() handles decimal", async (t) => {
|
2023-05-30 23:49:16 +00:00
|
|
|
|
await t.step("unescapes decimal", () => {
|
|
|
|
|
assertEquals(unescape("."), ".");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes max decimal codepoint", () => {
|
|
|
|
|
assertEquals(unescape(""), "\u{10ffff}");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes decimal with leading zero", () => {
|
|
|
|
|
assertEquals(unescape("."), ".");
|
|
|
|
|
});
|
|
|
|
|
await t.step(
|
|
|
|
|
"unescapes invalid decimal codepoint to replacement character",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(unescape("�"), "<22>");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-02 23:46:12 +00:00
|
|
|
|
await t.step("unescape() handles hex", async (t) => {
|
2023-05-30 23:49:16 +00:00
|
|
|
|
await t.step("unescapes lower-case hex", () => {
|
|
|
|
|
assertEquals(unescape("."), ".");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes upper-case hex", () => {
|
|
|
|
|
assertEquals(unescape("."), ".");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes hex with leading zero", () => {
|
|
|
|
|
assertEquals(unescape("."), ".");
|
|
|
|
|
});
|
|
|
|
|
await t.step("unescapes max hex codepoint", () => {
|
|
|
|
|
assertEquals(unescape(""), "\u{10ffff}");
|
|
|
|
|
});
|
|
|
|
|
await t.step(
|
|
|
|
|
"unescapes invalid hex codepoint to replacement character",
|
|
|
|
|
() => {
|
|
|
|
|
assertEquals(unescape("�"), "<22>");
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|