diff options
Diffstat (limited to 'node_modules/entities/src/encode.spec.ts')
| -rw-r--r-- | node_modules/entities/src/encode.spec.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/node_modules/entities/src/encode.spec.ts b/node_modules/entities/src/encode.spec.ts new file mode 100644 index 0000000..10ff5de --- /dev/null +++ b/node_modules/entities/src/encode.spec.ts @@ -0,0 +1,78 @@ +import { describe, it, expect } from "vitest"; +import * as entities from "./index.js"; + +describe("Encode->decode test", () => { + const testcases = [ + { + input: "asdf & รฟ รผ '", + xml: "asdf & ÿ ü '", + html: "asdf & ÿ ü '", + }, + { + input: "&", + xml: "&#38;", + html: "&#38;", + }, + ]; + + for (const { input, xml, html } of testcases) { + const encodedXML = entities.encodeXML(input); + it(`should XML encode ${input}`, () => expect(encodedXML).toBe(xml)); + it(`should default to XML encode ${input}`, () => + expect(entities.encode(input)).toBe(xml)); + it(`should XML decode ${encodedXML}`, () => + expect(entities.decodeXML(encodedXML)).toBe(input)); + it(`should default to XML encode ${encodedXML}`, () => + expect(entities.decode(encodedXML)).toBe(input)); + it(`should default strict to XML encode ${encodedXML}`, () => + expect(entities.decodeStrict(encodedXML)).toBe(input)); + + const encodedHTML5 = entities.encodeHTML5(input); + it(`should HTML5 encode ${input}`, () => + expect(encodedHTML5).toBe(html)); + it(`should HTML5 decode ${encodedHTML5}`, () => + expect(entities.decodeHTML(encodedHTML5)).toBe(input)); + it("should encode emojis", () => + expect(entities.encodeHTML5("๐๐พ๐ฅณ๐ฅ๐")).toBe( + "😄🍾🥳💥😇", + )); + } + + it("should encode data URIs (issue #16)", () => { + const data = + "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7"; + expect(entities.decode(entities.encode(data))).toBe(data); + }); + + it("should HTML encode all ASCII characters", () => { + for (let index = 0; index < 128; index++) { + const char = String.fromCharCode(index); + const encoded = entities.encodeHTML(char); + const decoded = entities.decodeHTML(encoded); + expect(decoded).toBe(char); + } + }); + + it("should encode trailing parts of entities", () => + expect(entities.encodeHTML("\uD835")).toBe("�")); + + it("should encode surrogate pair with first surrogate equivalent of entity, without corresponding entity", () => + expect(entities.encodeHTML("\u{1D4A4}")).toBe("𝒤")); +}); + +describe("encodeNonAsciiHTML", () => { + it("should encode all non-ASCII characters", () => + expect(entities.encodeNonAsciiHTML("<test> #123! รผbermaรen")).toBe( + "<test> #123! übermaßen", + )); + + it("should encode emojis", () => + expect(entities.encodeNonAsciiHTML("๐๐พ๐ฅณ๐ฅ๐")).toBe( + "😄🍾🥳💥😇", + )); + + it("should encode chars above surrogates", () => + expect(entities.encodeNonAsciiHTML("โ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธโ๏ธ")).toBe( + "♒️♓️♈️♉️♊️♋️♌️♍️♎️♏️♐️♑️", + )); +}); |
