about summary refs log tree commit diff stats
path: root/test/js/encode_decode.html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-04-21 21:26:27 +0200
committerbptato <nincsnevem662@gmail.com>2024-04-21 21:28:45 +0200
commite3d9ee2c6224cd30bf56e24f7988899e0e3985c1 (patch)
tree31116d7d751d058f32bf43313c12673423e8d8be /test/js/encode_decode.html
parentba88f015ea500bcc574b621392c721ef2c9dbcd0 (diff)
downloadchawan-e3d9ee2c6224cd30bf56e24f7988899e0e3985c1.tar.gz
test: add js & layout tests
(Sadly some layout tests still fail.)
Diffstat (limited to 'test/js/encode_decode.html')
-rw-r--r--test/js/encode_decode.html39
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>