Skip to content

Commit 2d536ac

Browse files
author
Friedrich
committed
Merge pull request #874 from kossebau/fixBOMHandling
No longer fail due to possible Byte Order Marks in ODF-internal XML files with Chromium
2 parents 0434b5b + 5a2db9c commit 2d536ac

3 files changed

Lines changed: 93 additions & 21 deletions

File tree

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## WebODF
44

5+
### Fixes
6+
7+
* No longer fail due to possible Byte Order Marks in ODF-internal XML files with Chromium ([#872](https://github.com/kogmbh/WebODF/issues/872)))
8+
9+
510
## Wodo.TextEditor
611
See also section about WebODF
712

webodf/lib/runtime.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,18 @@ Runtime.byteArrayToString = function (bytearray, encoding) {
210210
* @return {!string}
211211
*/
212212
function utf8ByteArrayToString(bytearray) {
213-
var s = "", i, l = bytearray.length,
213+
var s = "", startPos, i, l = bytearray.length,
214214
chars = [],
215215
c0, c1, c2, c3, codepoint;
216216

217-
for (i = 0; i < l; i += 1) {
217+
// skip a possible UTF-8 BOM
218+
if (l >= 3 && bytearray[0] === 0xef && bytearray[1] === 0xbb && bytearray[2] === 0xbf) {
219+
startPos = 3;
220+
} else {
221+
startPos = 0;
222+
}
223+
224+
for (i = startPos; i < l; i += 1) {
218225
c0 = /**@type{!number}*/(bytearray[i]);
219226
if (c0 < 0x80) {
220227
chars.push(c0);

webodf/tests/core/RuntimeTests.js

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* @source: https://github.com/kogmbh/WebODF/
2323
*/
2424

25-
/*global core, runtime, Runtime*/
25+
/*global core, runtime, Runtime, ArrayBuffer, Uint8Array*/
2626

2727
/*jslint bitwise: true*/
2828

@@ -102,21 +102,78 @@ core.RuntimeTests = function RuntimeTests(runner) {
102102
});
103103
}
104104

105-
function testUtf8ByteArrayToString(callback) {
106-
var pre = r.resourcePrefix();
107-
runtime.read(pre + "utf8.txt", 14, 4, function (err, data) {
108-
t.err = err;
109-
r.shouldBeNull(t, "t.err");
110-
t.data = data;
111-
r.shouldBe(t, "t.data.length", "4");
112-
if (data) {
113-
// we want to test the actual Runtime implementation rather than the nodejs runtime
114-
t.data = Runtime.byteArrayToString(data, "utf8");
115-
}
116-
r.shouldBe(t, "t.data.charCodeAt(0)", "55378");
117-
r.shouldBe(t, "t.data.charCodeAt(1)", "57186");
118-
callback();
119-
});
105+
function testUtf8ByteArrayToString_hello_withBOM() {
106+
var bytearray = new Uint8Array(new ArrayBuffer(8));
107+
108+
bytearray[0] = 0xef;
109+
bytearray[1] = 0xbb;
110+
bytearray[2] = 0xbf;
111+
bytearray[3] = 0x68;
112+
bytearray[4] = 0x65;
113+
bytearray[5] = 0x6C;
114+
bytearray[6] = 0x6C;
115+
bytearray[7] = 0x6F;
116+
117+
// we want to test the actual Runtime implementation rather than the nodejs runtime
118+
t.data = Runtime.byteArrayToString(bytearray, "utf8");
119+
120+
r.shouldBe(t, "t.data.charCodeAt(0)", "0x68");
121+
r.shouldBe(t, "t.data.charCodeAt(1)", "0x65");
122+
r.shouldBe(t, "t.data.charCodeAt(2)", "0x6C");
123+
r.shouldBe(t, "t.data.charCodeAt(3)", "0x6C");
124+
r.shouldBe(t, "t.data.charCodeAt(4)", "0x6F");
125+
}
126+
127+
function testUtf8ByteArrayToString_hello_noBOM() {
128+
var bytearray = new Uint8Array(new ArrayBuffer(5));
129+
130+
bytearray[0] = 0x68;
131+
bytearray[1] = 0x65;
132+
bytearray[2] = 0x6C;
133+
bytearray[3] = 0x6C;
134+
bytearray[4] = 0x6F;
135+
136+
// we want to test the actual Runtime implementation rather than the nodejs runtime
137+
t.data = Runtime.byteArrayToString(bytearray, "utf8");
138+
139+
r.shouldBe(t, "t.data.charCodeAt(0)", "0x68");
140+
r.shouldBe(t, "t.data.charCodeAt(1)", "0x65");
141+
r.shouldBe(t, "t.data.charCodeAt(2)", "0x6C");
142+
r.shouldBe(t, "t.data.charCodeAt(3)", "0x6C");
143+
r.shouldBe(t, "t.data.charCodeAt(4)", "0x6F");
144+
}
145+
146+
function testUtf8ByteArrayToString_surrogates_withBOM() {
147+
var bytearray = new Uint8Array(new ArrayBuffer(7));
148+
149+
bytearray[0] = 0xef;
150+
bytearray[1] = 0xbb;
151+
bytearray[2] = 0xbf;
152+
bytearray[3] = 0xf0;
153+
bytearray[4] = 0xa4;
154+
bytearray[5] = 0xad;
155+
bytearray[6] = 0xa2;
156+
157+
// we want to test the actual Runtime implementation rather than the nodejs runtime
158+
t.data = Runtime.byteArrayToString(bytearray, "utf8");
159+
160+
r.shouldBe(t, "t.data.charCodeAt(0)", "55378");
161+
r.shouldBe(t, "t.data.charCodeAt(1)", "57186");
162+
}
163+
164+
function testUtf8ByteArrayToString_surrogates_noBOM() {
165+
var bytearray = new Uint8Array(new ArrayBuffer(4));
166+
167+
bytearray[0] = 0xf0;
168+
bytearray[1] = 0xa4;
169+
bytearray[2] = 0xad;
170+
bytearray[3] = 0xa2;
171+
172+
// we want to test the actual Runtime implementation rather than the nodejs runtime
173+
t.data = Runtime.byteArrayToString(bytearray, "utf8");
174+
175+
r.shouldBe(t, "t.data.charCodeAt(0)", "55378");
176+
r.shouldBe(t, "t.data.charCodeAt(1)", "57186");
120177
}
121178

122179
function testLoadXML(callback) {
@@ -137,14 +194,17 @@ core.RuntimeTests = function RuntimeTests(runner) {
137194
t = {};
138195
};
139196
this.tests = function () {
140-
return [
141-
];
197+
return r.name([
198+
testUtf8ByteArrayToString_hello_withBOM,
199+
testUtf8ByteArrayToString_hello_noBOM,
200+
testUtf8ByteArrayToString_surrogates_withBOM,
201+
testUtf8ByteArrayToString_surrogates_noBOM
202+
]);
142203
};
143204
this.asyncTests = function () {
144205
return r.name([
145206
testRead,
146207
testWrite,
147-
testUtf8ByteArrayToString,
148208
testLoadXML
149209
]);
150210
};

0 commit comments

Comments
 (0)