1 var assert = require("assert"),
2 path = require("path"),
3 entities = require("../");
5 describe("Encode->decode test", function() {
9 xml: "asdf & ÿ ü '",
10 html: "asdf & ÿ ü '"
15 html: "&"
18 testcases.forEach(function(tc) {
19 var encodedXML = entities.encodeXML(tc.input);
20 it("should XML encode " + tc.input, function() {
21 assert.equal(encodedXML, tc.xml);
23 it("should default to XML encode " + tc.input, function() {
24 assert.equal(entities.encode(tc.input), tc.xml);
26 it("should XML decode " + encodedXML, function() {
27 assert.equal(entities.decodeXML(encodedXML), tc.input);
29 it("should default to XML encode " + encodedXML, function() {
30 assert.equal(entities.decode(encodedXML), tc.input);
32 it("should default strict to XML encode " + encodedXML, function() {
33 assert.equal(entities.decodeStrict(encodedXML), tc.input);
36 var encodedHTML5 = entities.encodeHTML5(tc.input);
37 it("should HTML5 encode " + tc.input, function() {
38 assert.equal(encodedHTML5, tc.html);
40 it("should HTML5 decode " + encodedHTML5, function() {
41 assert.equal(entities.decodeHTML(encodedHTML5), tc.input);
45 it("should encode data URIs (issue 16)", function() {
46 var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";
47 assert.equal(entities.decode(entities.encode(data)), data);
51 describe("Decode test", function() {
53 { input: "&", output: "&" },
54 { input: "&", output: "&" },
55 { input: "&", output: "&" },
56 { input: "&", output: "&" },
57 { input: "&", output: "&" },
58 { input: "&", output: "&" },
59 { input: "&", output: "&" },
60 { input: ":", output: ":" },
61 { input: ":", output: ":" },
62 { input: ":", output: ":" },
63 { input: ":", output: ":" }
65 testcases.forEach(function(tc) {
66 it("should XML decode " + tc.input, function() {
67 assert.equal(entities.decodeXML(tc.input), tc.output);
69 it("should HTML4 decode " + tc.input, function() {
70 assert.equal(entities.decodeHTML(tc.input), tc.output);
72 it("should HTML5 decode " + tc.input, function() {
73 assert.equal(entities.decodeHTML(tc.input), tc.output);
78 var levels = ["xml", "entities"];
80 describe("Documents", function() {
83 return path.join("..", "maps", n);
86 .forEach(function(doc, i) {
87 describe("Decode", function() {
88 it(levels[i], function() {
89 Object.keys(doc).forEach(function(e) {
90 for (var l = i; l < levels.length; l++) {
91 assert.equal(entities.decode("&" + e + ";", l), doc[e]);
97 describe("Decode strict", function() {
98 it(levels[i], function() {
99 Object.keys(doc).forEach(function(e) {
100 for (var l = i; l < levels.length; l++) {
101 assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
107 describe("Encode", function() {
108 it(levels[i], function() {
109 Object.keys(doc).forEach(function(e) {
110 for (var l = i; l < levels.length; l++) {
111 assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
118 var legacy = require("../maps/legacy.json");
120 describe("Legacy", function() {
121 it("should decode", runLegacy);
124 function runLegacy() {
125 Object.keys(legacy).forEach(function(e) {
126 assert.equal(entities.decodeHTML("&" + e), legacy[e]);
132 "1D306": "\uD834\uDF06",
133 "1D11E": "\uD834\uDD1E"
136 var astralSpecial = {
141 describe("Astral entities", function() {
142 Object.keys(astral).forEach(function(c) {
143 it("should decode " + astral[c], function() {
144 assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
147 it("should encode " + astral[c], function() {
148 assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
151 it("should escape " + astral[c], function() {
152 assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
156 Object.keys(astralSpecial).forEach(function(c) {
157 it("special should decode \\u" + c, function() {
158 assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
163 describe("Escape", function() {
164 it("should always decode ASCII chars", function() {
165 for (var i = 0; i < 0x7f; i++) {
166 var c = String.fromCharCode(i);
167 assert.equal(entities.decodeXML(entities.escape(c)), c);