diff options
author | Luca Casonato <lucacasonato@yahoo.com> | 2021-04-08 15:05:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-08 15:05:08 +0200 |
commit | c867c1aa476b3f00933be08cbc5f528973e37857 (patch) | |
tree | 4966a8a2e853db1c552ec39821a65bc13f927d5a /op_crates/web/08_text_encoding.js | |
parent | d2e500e1cf7d27132fee92cc09238e1bc98897c6 (diff) |
fix: enable FileReader wpt and align to spec (#10063)
This adds some algorithms from the whatwg mimesniff, whatwg infra, and
whatwg encoding specs that FileReader needs to use internally.
Diffstat (limited to 'op_crates/web/08_text_encoding.js')
-rw-r--r-- | op_crates/web/08_text_encoding.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/op_crates/web/08_text_encoding.js b/op_crates/web/08_text_encoding.js index 73cb38311..1fda1a816 100644 --- a/op_crates/web/08_text_encoding.js +++ b/op_crates/web/08_text_encoding.js @@ -4545,10 +4545,38 @@ fromByteArray, }; + /** + * @param {Uint8Array} bytes + */ + function decode(bytes, encoding) { + const BOMEncoding = BOMSniff(bytes); + let start = 0; + if (BOMEncoding !== null) { + encoding = BOMEncoding; + if (BOMEncoding === "UTF-8") start = 3; + else start = 2; + } + return new TextDecoder(encoding).decode(bytes.slice(start)); + } + + /** + * @param {Uint8Array} bytes + */ + function BOMSniff(bytes) { + const BOM = bytes.subarray(0, 3); + if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) { + return "UTF-8"; + } + if (BOM[0] === 0xFE && BOM[1] === 0xFF) return "UTF-16BE"; + if (BOM[0] === 0xFF && BOM[1] === 0xFE) return "UTF-16LE"; + return null; + } + window.TextEncoder = TextEncoder; window.TextDecoder = TextDecoder; window.atob = atob; window.btoa = btoa; window.__bootstrap = window.__bootstrap || {}; + window.__bootstrap.encoding = { decode }; window.__bootstrap.base64 = base64; })(this); |