1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-07-30 02:21:17 +03:00

Use text-encoding-utf-8 as a TextEncoder polyfill

Somebody else seems to have done a good job of polyfilling TextEncoder, so
let's use that.
This commit is contained in:
Richard van der Hoff
2017-01-18 11:39:44 +00:00
parent 09ce74cc76
commit 31df78f946
7 changed files with 89 additions and 336 deletions

View File

@ -1,85 +0,0 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
import TextDecoderPolyfill from 'utils/TextDecoderPolyfill';
import * as testUtils from '../test-utils';
import expect from 'expect';
describe('textDecoderPolyfill', function() {
beforeEach(function() {
testUtils.beforeEach(this);
});
it('should correctly decode a range of strings', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(65, 66, 67))).toEqual('ABC');
expect(decoder.decode(Uint8Array.of(0xC3, 0xA6))).toEqual('æ');
expect(decoder.decode(Uint8Array.of(0xE2, 0x82, 0xAC))).toEqual('€');
expect(decoder.decode(Uint8Array.of(0xF0, 0x9F, 0x92, 0xA9))).toEqual('\uD83D\uDCA9');
});
it('should ignore byte-order marks', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(0xEF, 0xBB, 0xBF, 65)))
.toEqual('A');
});
it('should not ignore byte-order marks in the middle of the array', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(65, 0xEF, 0xBB, 0xBF, 66)))
.toEqual('A\uFEFFB');
});
it('should reject overlong encodings', function() {
const decoder = new TextDecoderPolyfill();
// euro, as 4 bytes
expect(decoder.decode(Uint8Array.of(65, 0xF0, 0x82, 0x82, 0xAC, 67)))
.toEqual('A\uFFFD\uFFFD\uFFFD\uFFFDC');
});
it('should reject 5 and 6-byte encodings', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(65, 0xF8, 0x82, 0x82, 0x82, 0x82, 67)))
.toEqual('A\uFFFD\uFFFD\uFFFD\uFFFD\uFFFDC');
});
it('should reject code points beyond 0x10000', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(0xF4, 0xA0, 0x80, 0x80)))
.toEqual('\uFFFD\uFFFD\uFFFD\uFFFD');
});
it('should cope with end-of-string', function() {
const decoder = new TextDecoderPolyfill();
expect(decoder.decode(Uint8Array.of(65, 0xC3)))
.toEqual('A\uFFFD');
expect(decoder.decode(Uint8Array.of(65, 0xE2, 0x82)))
.toEqual('A\uFFFD\uFFFD');
expect(decoder.decode(Uint8Array.of(65, 0xF0, 0x9F, 0x92)))
.toEqual('A\uFFFD\uFFFD\uFFFD');
});
});

View File

@ -1,39 +0,0 @@
/*
Copyright 2017 Vector Creations Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
import TextEncoderPolyfill from 'utils/TextEncoderPolyfill';
import * as testUtils from '../test-utils';
import expect from 'expect';
describe('textEncoderPolyfill', function() {
beforeEach(function() {
testUtils.beforeEach(this);
});
it('should correctly encode a range of strings', function() {
const encoder = new TextEncoderPolyfill();
expect(encoder.encode('ABC')).toEqual(Uint8Array.of(65, 66, 67));
expect(encoder.encode('æ')).toEqual(Uint8Array.of(0xC3, 0xA6));
expect(encoder.encode('€')).toEqual(Uint8Array.of(0xE2, 0x82, 0xAC));
// PILE OF POO (💩)
expect(encoder.encode('\uD83D\uDCA9')).toEqual(Uint8Array.of(0xF0, 0x9F, 0x92, 0xA9));
});
});