-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
encoding.js
239 lines (205 loc) · 7.91 KB
/
encoding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"use strict";
const fs = require("pn/fs");
const path = require("path");
const { assert } = require("chai");
const { describe, it, before, after } = require("mocha-sugar-free");
const { createServer } = require("../util.js");
const { JSDOM } = require("../..");
function fixturePath(fixture) {
return path.resolve(__dirname, "fixtures/encoding", fixture);
}
function readFixture(fixture) {
return fs.readFile(fixturePath(fixture));
}
const factories = {
Buffer: fixture => readFixture(fixture),
Uint8Array: fixture => readFixture(fixture).then(buffer => Uint8Array.from(buffer)),
ArrayBuffer: fixture => readFixture(fixture).then(buffer => buffer.buffer),
DataView: fixture => readFixture(fixture).then(buffer => new DataView(buffer.buffer)),
Int8Array: fixture => readFixture(fixture).then(buffer => {
// Test a view that is indexing into a larger ArrayBuffer
const target = new Int8Array(buffer.buffer);
const bigger = new Int8Array([0, 0, 0, ...target, 0, 0, 0]);
const subView = new Int8Array(bigger.buffer, 3, buffer.buffer.byteLength);
return subView;
})
};
const encodingFixtures = {
"no-bom-charset-http-equiv-no-quotes.html": {
name: "ISO-8859-5",
nameWhenOverridden: "ISO-8859-8",
body: "Ђ",
bodyWhenOverridden: "¢"
},
"no-bom-charset-http-equiv-tis-620.html": {
name: "windows-874",
nameWhenOverridden: "ISO-8859-8",
body: "ข",
bodyWhenOverridden: "¢"
},
"no-bom-charset-koi8.html": {
name: "KOI8-R",
nameWhenOverridden: "ISO-8859-8",
body: "╒",
bodyWhenOverridden: "¢"
},
"no-bom-charset-utf-16.html": {
name: "UTF-8",
nameWhenOverridden: "ISO-8859-8",
body: "Є"
},
"no-bom-charset-utf-16be.html": {
name: "UTF-8",
nameWhenOverridden: "ISO-8859-8",
body: "Є"
},
"no-bom-charset-utf-16le.html": {
name: "UTF-8",
nameWhenOverridden: "ISO-8859-8",
body: "Є"
},
"no-bom-no-charset.html": {
name: "windows-1252",
nameWhenOverridden: "ISO-8859-8",
body: "®"
},
"utf-8-bom.html": {
name: "UTF-8",
nameWhenOverridden: "UTF-8",
body: "Є",
bodyWhenOverridden: "Є"
},
"utf-16be-bom.html": {
name: "UTF-16BE",
nameWhenOverridden: "UTF-16BE",
body: "Є",
bodyWhenOverridden: "Є"
},
"utf-16le-bom.html": {
name: "UTF-16LE",
nameWhenOverridden: "UTF-16LE",
body: "Є",
bodyWhenOverridden: "Є"
}
};
describe("API: encoding detection", () => {
describe("constructor, given a string", () => {
it("should default to UTF-8 when passing a string", () => {
const dom = new JSDOM("©");
assert.strictEqual(dom.window.document.characterSet, "UTF-8");
assert.strictEqual(dom.window.document.body.textContent, "©");
});
it("should default to UTF-8 when passing nothing", () => {
const dom = new JSDOM();
assert.strictEqual(dom.window.document.characterSet, "UTF-8");
assert.strictEqual(dom.window.document.body.textContent, "");
});
it("should default to UTF-8 when passing null", () => {
const dom = new JSDOM(null);
assert.strictEqual(dom.window.document.characterSet, "UTF-8");
assert.strictEqual(dom.window.document.body.textContent, "null");
});
});
describe("constructor, given binary data", { skipIfBrowser: true }, () => {
describe("with no contentType option given", () => {
for (const binaryDataType of Object.keys(factories)) {
const factory = factories[binaryDataType];
describe(binaryDataType, () => {
for (const encodingFixture of Object.keys(encodingFixtures)) {
const { name, body } = encodingFixtures[encodingFixture];
it(`should sniff ${encodingFixture} as ${name}`, () => {
return factory(encodingFixture).then(binaryData => {
assert.strictEqual(
binaryData.constructor.name, binaryDataType,
"Sanity check: input binary data must be of the right type"
);
const dom = new JSDOM(binaryData);
assert.strictEqual(dom.window.document.characterSet, name);
assert.strictEqual(dom.window.document.body.textContent, body);
});
});
}
});
}
});
describe("with a contentType option specifying csiso88598e", () => {
for (const binaryDataType of Object.keys(factories)) {
const factory = factories[binaryDataType];
describe(binaryDataType, () => {
for (const encodingFixture of Object.keys(encodingFixtures)) {
const { nameWhenOverridden, bodyWhenOverridden } = encodingFixtures[encodingFixture];
it(`should sniff ${encodingFixture} as ${nameWhenOverridden}`, () => {
return factory(encodingFixture).then(binaryData => {
assert.strictEqual(
binaryData.constructor.name, binaryDataType,
"Sanity check: input binary data must be of the right type"
);
const dom = new JSDOM(binaryData, { contentType: "text/html;charset=csiso88598e" });
assert.strictEqual(dom.window.document.characterSet, nameWhenOverridden);
assert.strictEqual(dom.window.document.contentType, "text/html"); // encoding should be stripped
if (bodyWhenOverridden) {
assert.strictEqual(dom.window.document.body.textContent, bodyWhenOverridden);
}
});
});
}
});
}
});
});
describe("fromFile", { skipIfBrowser: true }, () => {
for (const encodingFixture of Object.keys(encodingFixtures)) {
const { name, body } = encodingFixtures[encodingFixture];
it(`should sniff ${encodingFixture} as ${name}`, () => {
return JSDOM.fromFile(fixturePath(encodingFixture)).then(dom => {
assert.strictEqual(dom.window.document.characterSet, name);
assert.strictEqual(dom.window.document.body.textContent, body);
});
});
}
});
describe("fromURL", { skipIfBrowser: true }, () => {
let server;
let host;
before(() => {
return createServer((req, res) => {
const [, fixture, query] = /^\/([^?]+)(\?.*)?$/.exec(req.url);
const headers = { "Content-Type": "text/html" };
if (query === "?charset=csiso88598e") {
headers["Content-Type"] = "text/html;charset=csiso88598e";
}
res.writeHead(200, headers);
fs.createReadStream(fixturePath(fixture)).pipe(res);
}).then(s => {
server = s;
host = `http://127.0.0.1:${s.address().port}`;
});
});
after(() => server.destroy());
describe("with no Content-Type header given", () => {
for (const encodingFixture of Object.keys(encodingFixtures)) {
const { name, body } = encodingFixtures[encodingFixture];
it(`should sniff ${encodingFixture} as ${name}`, () => {
return JSDOM.fromURL(`${host}/${encodingFixture}`).then(dom => {
assert.strictEqual(dom.window.document.characterSet, name);
assert.strictEqual(dom.window.document.body.textContent, body);
});
});
}
});
describe("with a Content-Type header specifying csiso88598e", () => {
for (const encodingFixture of Object.keys(encodingFixtures)) {
const { nameWhenOverridden, bodyWhenOverridden } = encodingFixtures[encodingFixture];
it(`should sniff ${encodingFixture} as ${nameWhenOverridden}`, () => {
return JSDOM.fromURL(`${host}/${encodingFixture}?charset=csiso88598e`).then(dom => {
assert.strictEqual(dom.window.document.characterSet, nameWhenOverridden);
assert.strictEqual(dom.window.document.contentType, "text/html"); // encoding should be stripped
if (bodyWhenOverridden) {
assert.strictEqual(dom.window.document.body.textContent, bodyWhenOverridden);
}
});
});
}
});
});
});