Skip to content

Commit

Permalink
Remove TextEncoding
Browse files Browse the repository at this point in the history
1. Remove "new TextEncoding.TextEncoder()" and "new TextEncoding.TextDecoder()" from browser tests,
and leave just old "new TextEncoder()" and "new TextDecoder()".

2. Define "new TextEncoder()" and "new TextDecoder()" if this is undefined.

3. To override "TextEncoder/TextDecoder", use:
<script>
  window.TextEncoder = undefined;
  window.TextDecoder = undefined;
//or
//window.TextEncoder = window.TextDecoder = null;
</script>

4.  "new TextEncoding.TextEncoder()" and "new TextEncoding.TextDecoder()" working too,
when "TextEncoder/TextDecoder" is already defined.
  • Loading branch information
username1565 authored May 30, 2020
1 parent 5eb0906 commit 031827f
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 92 deletions.
11 changes: 9 additions & 2 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ declare const window;

// Polyfills browser
if (typeof window !== 'undefined') {
if (!('TextDecoder' in window)) window['TextDecoder'] = TextDecoder;
if (!('TextEncoder' in window)) window['TextEncoder'] = TextEncoder;
const checkUndefined = key => !(key in window)
|| typeof window[key] === 'undefined'
|| window[key] === null;

if (checkUndefined('TextDecoder'))
window['TextDecoder'] = TextDecoder;

if (checkUndefined('TextEncoder'))
window['TextEncoder'] = TextEncoder;
}

export { TextDecoder, TextEncoder };
2 changes: 1 addition & 1 deletion test/browser/test-big5.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/browser/test-euc-jp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/browser/test-euc-kr.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/browser/test-gb18030.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test(function() {
];

cases.forEach(function(c) {
assert_equals(new TextEncoding.TextDecoder('gb18030').decode(new Uint8Array(c.bytes)),
assert_equals(new TextDecoder('gb18030').decode(new Uint8Array(c.bytes)),
c.string);
});
}, 'gb18030 ranges');
2 changes: 1 addition & 1 deletion test/browser/test-gbk.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/browser/test-iso-2022-jp.js

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions test/browser/test-iso-8859-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ test(
var allBytes = new Uint8Array(256); for(var i = 0; i<256; i++){allBytes[i] = i;} //generate Uint8Array with all possible 256 consecutive byte-values.
var s = ''; for(var i = 0; i<allBytes.length; i++){s += String.fromCharCode(allBytes[i]);} //generate iso-8859-1 string with char-codes of this bytes.

var decoded_latin1_string = new TextEncoding.TextDecoder("iso-8859-1").decode(allBytes); //decode allBytes "latin1"-string
var decoded_latin1_string = new TextDecoder("iso-8859-1").decode(allBytes); //decode allBytes "latin1"-string
assert_equals(decoded_latin1_string, s); //compare decoded string and generated string.

var encoded_allBytes = new TextEncoding.
TextEncoder( //encode into new Uint8Array
var encoded_allBytes = new TextEncoder( //encode into new Uint8Array
"iso-8859-1", //"iso-8859-1"-encoded string
{NONSTANDARD_allowLegacyEncoding: true} //as legacy encoding
)
.encode(decoded_latin1_string) //from this value
;

var decoded_latin1_string_again = new TextEncoding.TextDecoder("iso-8859-1").decode(encoded_allBytes); //and decode this string back, by using TextDecoder.
var decoded_latin1_string_again = new TextDecoder("iso-8859-1").decode(encoded_allBytes); //and decode this string back, by using TextDecoder.

assert_equals(decoded_latin1_string_again, s); //compare the second decoded string and generated string.

Expand Down
142 changes: 71 additions & 71 deletions test/browser/test-misc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion test/browser/test-shift_jis.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions test/browser/test-utf.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ function test_utf_roundtrip () {
var BLOCK_SIZE = 0x1000;
var SKIP_SIZE = 31;

var TD_U16LE = new TextEncoding.TextDecoder("UTF-16LE");
var TD_U16BE = new TextEncoding.TextDecoder("UTF-16BE");
var TD_U16LE = new TextDecoder("UTF-16LE");
var TD_U16BE = new TextDecoder("UTF-16BE");

var TE_U8 = new TextEncoding.TextEncoder();
var TD_U8 = new TextEncoding.TextDecoder("UTF-8");
var TE_U8 = new TextEncoder();
var TD_U8 = new TextDecoder("UTF-8");

for (var i = MIN_CODEPOINT; i < MAX_CODEPOINT; i += BLOCK_SIZE) {
var block_tag = cpname(i) + " - " + cpname(i + BLOCK_SIZE - 1);
Expand All @@ -110,7 +110,7 @@ function test_utf_roundtrip () {
decoded = TD_U8.decode(encoded);
assert_string_equals(block, decoded, "UTF-8 round trip " + block_tag);

// testTextEncoding.TextEncoder(UTF-8) against the older idiom
// test TextEncoder(UTF-8) against the older idiom
var exp_encoded = encode_utf8(block);
assert_array_equals(encoded, exp_encoded,
"UTF-8 reference encoding " + block_tag);
Expand All @@ -137,7 +137,7 @@ function test_utf_samples () {

cases.forEach(
function(t) {
var decoded = new TextEncoding.TextDecoder(t.encoding)
var decoded = new TextDecoder(t.encoding)
.decode(new Uint8Array(t.expected));
assert_equals(decoded, sample,
"expected equal decodings - " + t.encoding);
Expand Down
4 changes: 2 additions & 2 deletions test/browser/test-x-user-defined.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

test(
function() {
assert_equals(new TextEncoding.TextEncoder('x-user-defined').encoding, 'utf-8');
assert_equals(new TextEncoder('x-user-defined').encoding, 'utf-8');

var decoder = new TextEncoding.TextDecoder('x-user-defined');
var decoder = new TextDecoder('x-user-defined');
for (var i = 0; i < 0x80; ++i) {
assert_equals(decoder.decode(new Uint8Array([i])), String.fromCharCode(i));
assert_equals(decoder.decode(new Uint8Array([i + 0x80])), String.fromCharCode(i + 0xF780));
Expand Down
4 changes: 4 additions & 0 deletions test/node/test-misc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ describe('Miscellaneous tests', () => {
it('TextDecoder Polyfill (will fail if natively supported)', () => {
assert_false(/\[native code\]/.test(String(TextDecoder)),
'Native implementation present - polyfill not tested.');
assert_true(Boolean(TextDecoder),
'TextDecoder is not defined at all.');
});

it('TextEncoder Polyfill (will fail if natively supported)', () => {
assert_false(/\[native code\]/.test(String(TextEncoder)),
'Native implementation present - polyfill not tested.');
assert_true(Boolean(TextEncoder),
'TextEncoder is not defined at all.');
});

it('Attributes', () => {
Expand Down

1 comment on commit 031827f

@username1565
Copy link
Owner Author

@username1565 username1565 commented on 031827f May 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was been fixed, as progress of this issue: zxing-js#5

Some files have CRLF, as the end of line, because after running the command:

git clone --recurse-submodules -j8 git://github.com/username1565/text-encoding.git

Git automatically change LF to CRLF, so, to disable this, need to use:

#git config --global core.autocrlf false   # do not replace LF to CRLF
git config --global core.autocrlf input    # do CRLF to LF when upload, but do not replace LF to CRLF when download.
git clone --recurse-submodules -j8 git://github.com/username1565/text-encoding.git

Maybe, this can be hardcoded in .gitattributes

Please sign in to comment.