diff options
Diffstat (limited to 'test/js/encode_decode.html')
-rw-r--r-- | test/js/encode_decode.html | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/test/js/encode_decode.html b/test/js/encode_decode.html new file mode 100644 index 00000000..069ddc72 --- /dev/null +++ b/test/js/encode_decode.html @@ -0,0 +1,39 @@ +<!doctype html> +<title>TextEncoder/TextDecoder test</title> +<div id="success">Fail</div> +<script> +(function() { + /* Adapted from: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem */ + function base64ToBytes(base64) { + const binString = atob(base64); + const result = []; + for (const c of binString) + result.push(Uint8Array.from(c, (m) => m.codePointAt(0))); + return result; + } + + function bytesToBase64(bytes) { + const binString = String.fromCodePoint(...bytes); + return btoa(binString); + } + + const utf8 = new TextEncoder().encode("a Ā 𐀀 文 🦄") + const b64utf8 = bytesToBase64(utf8); + if (b64utf8 !== "YSDEgCDwkICAIOaWhyDwn6aE") { + console.log(b64utf8); + return; + } + const dec = new TextDecoder(); + const bytes = base64ToBytes(b64utf8); + const a = []; + let res = ""; + for (const c of bytes) + res += dec.decode(c, {stream: true}); + res += dec.decode(); + if (res !== "a Ā 𐀀 文 🦄") { + console.log(res); + return; + } + document.getElementById("success").textContent = "Success"; +})(); +</script> |