1 | var assert = require("assert"),
|
2 | path = require("path"),
|
3 | entities = require("../");
|
4 |
|
5 | describe("Encode->decode test", function(){
|
6 | var testcases = [
|
7 | {
|
8 | input: "asdf & ÿ ü '",
|
9 | xml: "asdf & ÿ ü '",
|
10 | html: "asdf & ÿ ü '"
|
11 | }, {
|
12 | input: "&",
|
13 | xml: "&",
|
14 | html: "&"
|
15 | },
|
16 | ];
|
17 | testcases.forEach(function(tc) {
|
18 | var encodedXML = entities.encodeXML(tc.input);
|
19 | it("should XML encode " + tc.input, function(){
|
20 | assert.equal(encodedXML, tc.xml);
|
21 | });
|
22 | it("should default to XML encode " + tc.input, function(){
|
23 | assert.equal(entities.encode(tc.input), tc.xml);
|
24 | });
|
25 | it("should XML decode " + encodedXML, function(){
|
26 | assert.equal(entities.decodeXML(encodedXML), tc.input);
|
27 | });
|
28 | it("should default to XML encode " + encodedXML, function(){
|
29 | assert.equal(entities.decode(encodedXML), tc.input);
|
30 | });
|
31 | it("should default strict to XML encode " + encodedXML, function(){
|
32 | assert.equal(entities.decodeStrict(encodedXML), tc.input);
|
33 | });
|
34 |
|
35 | var encodedHTML5 = entities.encodeHTML5(tc.input);
|
36 | it("should HTML5 encode " + tc.input, function(){
|
37 | assert.equal(encodedHTML5, tc.html);
|
38 | });
|
39 | it("should HTML5 decode " + encodedHTML5, function(){
|
40 | assert.equal(entities.decodeHTML(encodedHTML5), tc.input);
|
41 | });
|
42 | });
|
43 | });
|
44 |
|
45 | describe("Decode test", function(){
|
46 | var testcases = [
|
47 | { input: "&", output: "&" },
|
48 | { input: "&", output: "&" },
|
49 | { input: "&", output: "&" },
|
50 | { input: "&", output: "&" },
|
51 | { input: "&", output: "&" },
|
52 | { input: "&", output: "&" },
|
53 | { input: "&", output: "&" },
|
54 | { input: ":", output: ":" },
|
55 | { input: ":", output: ":" },
|
56 | { input: ":", output: ":" },
|
57 | { input: ":", output: ":" }
|
58 | ];
|
59 | testcases.forEach(function(tc) {
|
60 | it("should XML decode " + tc.input, function(){
|
61 | assert.equal(entities.decodeXML(tc.input), tc.output);
|
62 | });
|
63 | it("should HTML4 decode " + tc.input, function(){
|
64 | assert.equal(entities.decodeHTML(tc.input), tc.output);
|
65 | });
|
66 | it("should HTML5 decode " + tc.input, function(){
|
67 | assert.equal(entities.decodeHTML(tc.input), tc.output);
|
68 | });
|
69 | });
|
70 | });
|
71 |
|
72 | var levels = ["xml", "entities"];
|
73 |
|
74 | describe("Documents", function(){
|
75 | levels
|
76 | .map(function(n){ return path.join("..", "maps", n); })
|
77 | .map(require)
|
78 | .forEach(function(doc, i){
|
79 | describe("Decode", function(){
|
80 | it(levels[i], function(){
|
81 | Object.keys(doc).forEach(function(e){
|
82 | for(var l = i; l < levels.length; l++){
|
83 | assert.equal(entities.decode("&" + e + ";", l), doc[e]);
|
84 | }
|
85 | });
|
86 | });
|
87 | });
|
88 |
|
89 | describe("Decode strict", function(){
|
90 | it(levels[i], function(){
|
91 | Object.keys(doc).forEach(function(e){
|
92 | for(var l = i; l < levels.length; l++){
|
93 | assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
|
94 | }
|
95 | });
|
96 | });
|
97 | });
|
98 |
|
99 | describe("Encode", function(){
|
100 | it(levels[i], function(){
|
101 | Object.keys(doc).forEach(function(e){
|
102 | for(var l = i; l < levels.length; l++){
|
103 | assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
|
104 | }
|
105 | });
|
106 | });
|
107 | });
|
108 | });
|
109 |
|
110 | var legacy = require("../maps/legacy.json");
|
111 |
|
112 | describe("Legacy", function(){
|
113 | it("should decode", runLegacy);
|
114 | });
|
115 |
|
116 | function runLegacy(){
|
117 | Object.keys(legacy).forEach(function(e){
|
118 | assert.equal(entities.decodeHTML("&" + e), legacy[e]);
|
119 | });
|
120 | }
|
121 | });
|
122 |
|
123 | var astral = {
|
124 | "1D306": "\uD834\uDF06",
|
125 | "1D11E": "\uD834\uDD1E"
|
126 | };
|
127 |
|
128 | var astralSpecial = {
|
129 | "80": "\u20AC",
|
130 | "110000": "\uFFFD"
|
131 | };
|
132 |
|
133 |
|
134 | describe("Astral entities", function(){
|
135 | Object.keys(astral).forEach(function(c){
|
136 | it("should decode " + astral[c], function(){
|
137 | assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
|
138 | });
|
139 |
|
140 | it("should encode " + astral[c], function(){
|
141 | assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
|
142 | });
|
143 |
|
144 | it("should escape " + astral[c], function(){
|
145 | assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
|
146 | });
|
147 | });
|
148 |
|
149 | Object.keys(astralSpecial).forEach(function(c){
|
150 | it("special should decode \\u" + c, function(){
|
151 | assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
|
152 | });
|
153 | });
|
154 | });
|
155 |
|
156 | describe("Escape", function(){
|
157 | it("should always decode ASCII chars", function(){
|
158 | for(var i = 0; i < 0x7F; i++){
|
159 | var c = String.fromCharCode(i);
|
160 | assert.equal(entities.decodeXML(entities.escape(c)), c);
|
161 | }
|
162 | });
|
163 | });
|