Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81169 views
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
it("should encode data URIs (issue 16)", function(){
45
var data = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAALAAABAAEAAAIBRAA7";
46
assert.equal(entities.decode(entities.encode(data)), data);
47
});
48
});
49
50
describe("Decode test", function(){
51
var testcases = [
52
{ input: "&", output: "&" },
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
];
64
testcases.forEach(function(tc) {
65
it("should XML decode " + tc.input, function(){
66
assert.equal(entities.decodeXML(tc.input), tc.output);
67
});
68
it("should HTML4 decode " + tc.input, function(){
69
assert.equal(entities.decodeHTML(tc.input), tc.output);
70
});
71
it("should HTML5 decode " + tc.input, function(){
72
assert.equal(entities.decodeHTML(tc.input), tc.output);
73
});
74
});
75
});
76
77
var levels = ["xml", "entities"];
78
79
describe("Documents", function(){
80
levels
81
.map(function(n){ return path.join("..", "maps", n); })
82
.map(require)
83
.forEach(function(doc, i){
84
describe("Decode", function(){
85
it(levels[i], function(){
86
Object.keys(doc).forEach(function(e){
87
for(var l = i; l < levels.length; l++){
88
assert.equal(entities.decode("&" + e + ";", l), doc[e]);
89
}
90
});
91
});
92
});
93
94
describe("Decode strict", function(){
95
it(levels[i], function(){
96
Object.keys(doc).forEach(function(e){
97
for(var l = i; l < levels.length; l++){
98
assert.equal(entities.decodeStrict("&" + e + ";", l), doc[e]);
99
}
100
});
101
});
102
});
103
104
describe("Encode", function(){
105
it(levels[i], function(){
106
Object.keys(doc).forEach(function(e){
107
for(var l = i; l < levels.length; l++){
108
assert.equal(entities.decode(entities.encode(doc[e], l), l), doc[e]);
109
}
110
});
111
});
112
});
113
});
114
115
var legacy = require("../maps/legacy.json");
116
117
describe("Legacy", function(){
118
it("should decode", runLegacy);
119
});
120
121
function runLegacy(){
122
Object.keys(legacy).forEach(function(e){
123
assert.equal(entities.decodeHTML("&" + e), legacy[e]);
124
});
125
}
126
});
127
128
var astral = {
129
"1D306": "\uD834\uDF06",
130
"1D11E": "\uD834\uDD1E"
131
};
132
133
var astralSpecial = {
134
"80": "\u20AC",
135
"110000": "\uFFFD"
136
};
137
138
139
describe("Astral entities", function(){
140
Object.keys(astral).forEach(function(c){
141
it("should decode " + astral[c], function(){
142
assert.equal(entities.decode("&#x" + c + ";"), astral[c]);
143
});
144
145
it("should encode " + astral[c], function(){
146
assert.equal(entities.encode(astral[c]), "&#x" + c + ";");
147
});
148
149
it("should escape " + astral[c], function(){
150
assert.equal(entities.escape(astral[c]), "&#x" + c + ";");
151
});
152
});
153
154
Object.keys(astralSpecial).forEach(function(c){
155
it("special should decode \\u" + c, function(){
156
assert.equal(entities.decode("&#x" + c + ";"), astralSpecial[c]);
157
});
158
});
159
});
160
161
describe("Escape", function(){
162
it("should always decode ASCII chars", function(){
163
for(var i = 0; i < 0x7F; i++){
164
var c = String.fromCharCode(i);
165
assert.equal(entities.decodeXML(entities.escape(c)), c);
166
}
167
});
168
});
169
170