Repository navigation
Expand file tree
/
Copy pathEncodingDetector-test.js
More file actions
303 lines (261 loc) · 15 KB
/
Copy pathEncodingDetector-test.js
File metadata and controls
303 lines (261 loc) · 15 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, awaitsForDone */
/*unittests: EncodingDetector*/
define(function (require, exports, module) {
const EncodingDetector = require("document/EncodingDetector");
// Builds a Uint8Array where each char code (0-255) becomes exactly one byte. Lets tests
// express arbitrary single-byte-encoded content (eg windows-1252) as a plain JS string
// without needing a real encoder: any char code above 0x7F is used as the raw byte value,
// which is exactly what windows-1252/latin1 do for the accented letters used below.
function bytesFromLatin1(str) {
const arr = new Uint8Array(str.length);
for (let i = 0; i < str.length; i++) {
// eslint-disable-next-line no-bitwise
arr[i] = str.charCodeAt(i) & 0xFF;
}
return arr;
}
function bytesFromUTF8(str) {
return new TextEncoder().encode(str);
}
// 0xE9 is "é" in both windows-1252 and latin1, but is not a valid standalone UTF-8 byte.
const WIN1252_E_ACUTE = String.fromCharCode(0xE9);
// matches the normalized (non-alphanumeric-stripped) form Phoenix's fs.SUPPORTED_ENCODINGS uses
const SUPPORTED = ["utf8", "windows1252", "iso88591", "utf16le", "utf16be", "utf32le", "utf32be"];
describe("EncodingDetector", function () {
describe("detectEncodingFromBytes", function () {
it("should return null for empty/missing bytes", function () {
expect(EncodingDetector.detectEncodingFromBytes("html", new Uint8Array(0), SUPPORTED)).toBeNull();
expect(EncodingDetector.detectEncodingFromBytes("html", null, SUPPORTED)).toBeNull();
});
it("should ignore non-sniffable extensions even with a declared charset", function () {
const bytes = bytesFromLatin1(
'<meta charset="windows-1252"> caf' + WIN1252_E_ACUTE
);
expect(EncodingDetector.detectEncodingFromBytes("js", bytes, SUPPORTED)).toBeNull();
expect(EncodingDetector.detectEncodingFromBytes("txt", bytes, SUPPORTED)).toBeNull();
});
it("should detect windows-1252 from a short-form <meta charset> tag", function () {
const bytes = bytesFromLatin1(
'<html><head><meta charset="windows-1252"></head><body>caf' +
WIN1252_E_ACUTE + '</body></html>'
);
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBe("windows1252");
});
it("should detect windows-1252 from a Content-Type http-equiv meta tag", function () {
// this is exactly the shape FrontPage/legacy authoring tools emit, and what the
// original bug report's file used.
const bytes = bytesFromLatin1(
'<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">' +
'supermarch' + WIN1252_E_ACUTE
);
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBe("windows1252");
});
it("should alias iso-8859-1 and latin1 declarations to windows-1252", function () {
const isoBytes = bytesFromLatin1(
'<meta charset="iso-8859-1">caf' + WIN1252_E_ACUTE
);
const latin1Bytes = bytesFromLatin1(
'<meta charset="latin1">caf' + WIN1252_E_ACUTE
);
expect(EncodingDetector.detectEncodingFromBytes("html", isoBytes, SUPPORTED)).toBe("windows1252");
expect(EncodingDetector.detectEncodingFromBytes("html", latin1Bytes, SUPPORTED)).toBe("windows1252");
});
it("should trust valid UTF-8 content over a stale/incorrect meta declaration", function () {
// declares windows-1252, but the bytes are actually well-formed utf-8 - keep utf-8.
const bytes = bytesFromUTF8('<meta charset="windows-1252">café supermarché');
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBeNull();
});
it("should return null when the declared charset is utf-8 but the bytes aren't valid utf-8", function () {
const bytes = bytesFromLatin1(
'<meta charset="utf-8">caf' + WIN1252_E_ACUTE
);
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBeNull();
});
it("should return null when no charset is declared at all", function () {
const bytes = bytesFromLatin1('<html><body>caf' + WIN1252_E_ACUTE + '</body></html>');
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBeNull();
});
it("should return null when the declared charset isn't in the supported encodings list", function () {
const bytes = bytesFromLatin1(
'<meta charset="made-up-charset-xyz">caf' + WIN1252_E_ACUTE
);
expect(EncodingDetector.detectEncodingFromBytes("html", bytes, SUPPORTED)).toBeNull();
});
it("should fall back to the global fs.SUPPORTED_ENCODINGS when none is passed in", function () {
const bytes = bytesFromLatin1(
'<meta charset="windows-1252">caf' + WIN1252_E_ACUTE
);
// running inside the real Phoenix shell, `fs.SUPPORTED_ENCODINGS` genuinely lists
// windows-1252 (backed by the bundled iconv-lite codec table).
expect(EncodingDetector.detectEncodingFromBytes("html", bytes)).toBe("windows1252");
});
it("should honor a BOM even on a non-sniffable extension, and take precedence over any meta tag", function () {
const utf16leBOM = new Uint8Array([0xFF, 0xFE, 0x61, 0x00]); // BOM + 'a'
expect(EncodingDetector.detectEncodingFromBytes("txt", utf16leBOM, SUPPORTED)).toBe("utf16le");
const utf16beBOM = new Uint8Array([0xFE, 0xFF, 0x00, 0x61]);
expect(EncodingDetector.detectEncodingFromBytes("html", utf16beBOM, SUPPORTED)).toBe("utf16be");
const utf32leBOM = new Uint8Array([0xFF, 0xFE, 0x00, 0x00, 0x61, 0x00, 0x00, 0x00]);
expect(EncodingDetector.detectEncodingFromBytes("txt", utf32leBOM, SUPPORTED)).toBe("utf32le");
const utf32beBOM = new Uint8Array([0x00, 0x00, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x61]);
expect(EncodingDetector.detectEncodingFromBytes("txt", utf32beBOM, SUPPORTED)).toBe("utf32be");
});
it("should return null for a utf-8 BOM since that's already the default", function () {
const utf8BOM = new Uint8Array([0xEF, 0xBB, 0xBF, 0x61]);
expect(EncodingDetector.detectEncodingFromBytes("html", utf8BOM, SUPPORTED)).toBeNull();
});
});
describe("detectFileEncoding", function () {
function makeMockFile(fullPath, readResult) {
let readCalled = false;
return {
fullPath: fullPath,
_readCalled: function () {
return readCalled;
},
read: function (options, callback) {
readCalled = true;
if (readResult.err) {
callback(readResult.err);
} else {
callback(null, readResult.content);
}
}
};
}
it("should resolve the detected encoding for a sniffable file with a declared charset", async function () {
const bytes = bytesFromLatin1(
'<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">caf' +
WIN1252_E_ACUTE
);
const file = makeMockFile("/proj/eu_format_test.html", {content: bytes.buffer});
let detected;
await awaitsForDone(
EncodingDetector.detectFileEncoding(file).done(function (result) {
detected = result;
})
);
expect(detected).toBe("windows1252");
});
it("should resolve null without reading the file for a binary extension", async function () {
const file = makeMockFile("/proj/photo.png", {content: new ArrayBuffer(0)});
let detected = "unset";
await awaitsForDone(
EncodingDetector.detectFileEncoding(file).done(function (result) {
detected = result;
})
);
expect(detected).toBeNull();
expect(file._readCalled()).toBe(false);
});
it("should still detect a BOM on a non-markup (but non-binary) extension like .txt/.js", async function () {
// Regression test: a BOM is unambiguous and extension-independent, unlike the
// <meta charset> scan which only makes sense for markup files - a plain .txt or .js
// file with a real UTF-16 BOM must still be auto-detected correctly.
const utf16beBOM = new Uint8Array([0xFE, 0xFF, 0x00, 0x61]);
const txtFile = makeMockFile("/proj/notes.txt", {content: utf16beBOM.buffer});
const jsFile = makeMockFile("/proj/script.js", {content: utf16beBOM.buffer});
let txtDetected, jsDetected;
await awaitsForDone(
EncodingDetector.detectFileEncoding(txtFile).done(function (result) {
txtDetected = result;
})
);
await awaitsForDone(
EncodingDetector.detectFileEncoding(jsFile).done(function (result) {
jsDetected = result;
})
);
expect(txtDetected).toBe("utf16be");
expect(jsDetected).toBe("utf16be");
expect(txtFile._readCalled()).toBe(true);
expect(jsFile._readCalled()).toBe(true);
});
it("should NOT scan for a <meta charset> declaration on a non-markup extension", async function () {
// a declared charset only makes sense for markup files - a .js/.txt file that
// happens to contain a "charset=windows-1252"-looking string (eg inside a comment
// or string literal) must not be reinterpreted based on it.
const bytes = bytesFromLatin1(
'// <meta charset="windows-1252"> not real markup, just a comment: caf' +
WIN1252_E_ACUTE
);
const file = makeMockFile("/proj/script.js", {content: bytes.buffer});
let detected;
await awaitsForDone(
EncodingDetector.detectFileEncoding(file).done(function (result) {
detected = result;
})
);
expect(detected).toBeNull();
expect(file._readCalled()).toBe(true);
});
it("should resolve null (not reject) when the file read fails", async function () {
const file = makeMockFile("/proj/broken.html", {err: "NotFound"});
let detected = "unset", rejected = false;
await awaitsForDone(
EncodingDetector.detectFileEncoding(file)
.done(function (result) {
detected = result;
})
.fail(function () {
rejected = true;
})
);
expect(detected).toBeNull();
expect(rejected).toBe(false);
});
it("should resolve null for well-formed utf-8 html regardless of a stale meta charset", async function () {
const bytes = bytesFromUTF8('<meta charset="windows-1252">café');
const file = makeMockFile("/proj/modern.html", {content: bytes.buffer});
let detected;
await awaitsForDone(
EncodingDetector.detectFileEncoding(file).done(function (result) {
detected = result;
})
);
expect(detected).toBeNull();
});
});
describe("isKnownTextEncoding", function () {
it("should reject falsy values", function () {
expect(EncodingDetector.isKnownTextEncoding(null)).toBe(false);
expect(EncodingDetector.isKnownTextEncoding(undefined)).toBe(false);
expect(EncodingDetector.isKnownTextEncoding("")).toBe(false);
});
it("should reject the byte-array sentinel used for non-text reads", function () {
// Regression test: other code paths (download-file, attach-image-to-chat, etc)
// read a File instance with {encoding: fs.BYTE_ARRAY_ENCODING} and no
// doNotCache, which - per File.read()'s own caching - leaves that sentinel value
// cached in file._encoding even though the file was never opened as text. Note
// fs.BYTE_ARRAY_ENCODING is deliberately included in fs.SUPPORTED_ENCODINGS (so
// reads can request it), which is exactly why this needs its own explicit check
// rather than relying on SUPPORTED_ENCODINGS membership.
expect(EncodingDetector.isKnownTextEncoding(window.fs.BYTE_ARRAY_ENCODING)).toBe(false);
expect(window.fs.SUPPORTED_ENCODINGS.indexOf(window.fs.BYTE_ARRAY_ENCODING)).not.toBe(-1);
});
it("should accept real text codec names", function () {
expect(EncodingDetector.isKnownTextEncoding("utf8")).toBe(true);
expect(EncodingDetector.isKnownTextEncoding("windows1252")).toBe(true);
expect(EncodingDetector.isKnownTextEncoding("utf16le")).toBe(true);
});
});
});
});