Skip to content

Commit

Permalink
fix: align btoa to spec (#9053)
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb authored Feb 16, 2021
1 parent 7e9028b commit 3f5265b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cli/tests/unit/text_encoding_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ unitTest(function btoaFailed(): void {
const text = "你好";
assertThrows(() => {
btoa(text);
}, TypeError);
}, DOMException);
});

unitTest(function textDecoder2(): void {
Expand Down
11 changes: 8 additions & 3 deletions op_crates/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"use strict";

((window) => {
const webidl = window.__bootstrap.webidl;
const core = Deno.core;

const CONTINUE = null;
Expand Down Expand Up @@ -124,13 +125,17 @@
}

function btoa(s) {
s = webidl.converters.DOMString(s, {
prefix: "Failed to execute 'bota'",
context: "Argument 1",
});
const byteArray = [];
for (let i = 0; i < s.length; i++) {
const charCode = s[i].charCodeAt(0);
if (charCode > 0xff) {
throw new TypeError(
"The string to be encoded contains characters " +
"outside of the Latin1 range.",
throw new DOMException(
"The string to be encoded contains characters outside of the Latin1 range.",
"InvalidCharacterError",
);
}
byteArray.push(charCode);
Expand Down
3 changes: 3 additions & 0 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,9 @@
},
"html": {
"webappapis": {
"atob": {
"base64.any.js": true
},
"timers": {
"cleartimeout-clearinterval.any.js": true,
"missing-timeout-setinterval.any.js": true,
Expand Down

0 comments on commit 3f5265b

Please sign in to comment.