| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent bf2d9f2 commit 5c60087
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ Last update: | |||
| 15 | 15 | - url: https://github.com/web-platform-tests/wpt/tree/418f7fabeb/url | |
| 16 | 16 | - resources: https://github.com/web-platform-tests/wpt/tree/e1fddfbf80/resources | |
| 17 | 17 | - interfaces: https://github.com/web-platform-tests/wpt/tree/8ada332aea/interfaces | |
| 18 | + - html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob | ||
| 18 | 19 | - html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/0c3bed38df/html/webappapis/microtask-queuing | |
| 19 | 20 | - html/webappapis/timers: https://github.com/web-platform-tests/wpt/tree/ddfe9c089b/html/webappapis/timers | |
| 20 | 21 | - hr-time: https://github.com/web-platform-tests/wpt/tree/a5d1774ecf/hr-time | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,163 @@ | |||
| 1 | + /** | ||
| 2 | + * btoa() as defined by the HTML5 spec, which mostly just references RFC4648. | ||
| 3 | + */ | ||
| 4 | + function mybtoa(s) { | ||
| 5 | + // String conversion as required by WebIDL. | ||
| 6 | + s = String(s); | ||
| 7 | + | ||
| 8 | + // "The btoa() method must throw an INVALID_CHARACTER_ERR exception if the | ||
| 9 | + // method's first argument contains any character whose code point is | ||
| 10 | + // greater than U+00FF." | ||
| 11 | + for (var i = 0; i < s.length; i++) { | ||
| 12 | + if (s.charCodeAt(i) > 255) { | ||
| 13 | + return "INVALID_CHARACTER_ERR"; | ||
| 14 | + } | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + var out = ""; | ||
| 18 | + for (var i = 0; i < s.length; i += 3) { | ||
| 19 | + var groupsOfSix = [undefined, undefined, undefined, undefined]; | ||
| 20 | + groupsOfSix[0] = s.charCodeAt(i) >> 2; | ||
| 21 | + groupsOfSix[1] = (s.charCodeAt(i) & 0x03) << 4; | ||
| 22 | + if (s.length > i + 1) { | ||
| 23 | + groupsOfSix[1] |= s.charCodeAt(i + 1) >> 4; | ||
| 24 | + groupsOfSix[2] = (s.charCodeAt(i + 1) & 0x0f) << 2; | ||
| 25 | + } | ||
| 26 | + if (s.length > i + 2) { | ||
| 27 | + groupsOfSix[2] |= s.charCodeAt(i + 2) >> 6; | ||
| 28 | + groupsOfSix[3] = s.charCodeAt(i + 2) & 0x3f; | ||
| 29 | + } | ||
| 30 | + for (var j = 0; j < groupsOfSix.length; j++) { | ||
| 31 | + if (typeof groupsOfSix[j] == "undefined") { | ||
| 32 | + out += "="; | ||
| 33 | + } else { | ||
| 34 | + out += btoaLookup(groupsOfSix[j]); | ||
| 35 | + } | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + return out; | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * Lookup table for mybtoa(), which converts a six-bit number into the | ||
| 43 | + * corresponding ASCII character. | ||
| 44 | + */ | ||
| 45 | + function btoaLookup(idx) { | ||
| 46 | + if (idx < 26) { | ||
| 47 | + return String.fromCharCode(idx + 'A'.charCodeAt(0)); | ||
| 48 | + } | ||
| 49 | + if (idx < 52) { | ||
| 50 | + return String.fromCharCode(idx - 26 + 'a'.charCodeAt(0)); | ||
| 51 | + } | ||
| 52 | + if (idx < 62) { | ||
| 53 | + return String.fromCharCode(idx - 52 + '0'.charCodeAt(0)); | ||
| 54 | + } | ||
| 55 | + if (idx == 62) { | ||
| 56 | + return '+'; | ||
| 57 | + } | ||
| 58 | + if (idx == 63) { | ||
| 59 | + return '/'; | ||
| 60 | + } | ||
| 61 | + // Throw INVALID_CHARACTER_ERR exception here -- won't be hit in the tests. | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + function btoaException(input) { | ||
| 65 | + input = String(input); | ||
| 66 | + for (var i = 0; i < input.length; i++) { | ||
| 67 | + if (input.charCodeAt(i) > 255) { | ||
| 68 | + return true; | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + return false; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + function testBtoa(input) { | ||
| 75 | + // "The btoa() method must throw an INVALID_CHARACTER_ERR exception if the | ||
| 76 | + // method's first argument contains any character whose code point is | ||
| 77 | + // greater than U+00FF." | ||
| 78 | + var normalizedInput = String(input); | ||
| 79 | + for (var i = 0; i < normalizedInput.length; i++) { | ||
| 80 | + if (normalizedInput.charCodeAt(i) > 255) { | ||
| 81 | + assert_throws_dom("InvalidCharacterError", function() { btoa(input); }, | ||
| 82 | + "Code unit " + i + " has value " + normalizedInput.charCodeAt(i) + ", which is greater than 255"); | ||
| 83 | + return; | ||
| 84 | + } | ||
| 85 | + } | ||
| 86 | + assert_equals(btoa(input), mybtoa(input)); | ||
| 87 | + assert_equals(atob(btoa(input)), String(input), "atob(btoa(input)) must be the same as String(input)"); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + var tests = ["עברית", "", "ab", "abc", "abcd", "abcde", | ||
| 91 | + // This one is thrown in because IE9 seems to fail atob(btoa()) on it. Or | ||
| 92 | + // possibly to fail btoa(). I actually can't tell what's happening here, | ||
| 93 | + // but it doesn't hurt. | ||
| 94 | + "\xff\xff\xc0", | ||
| 95 | + // Is your DOM implementation binary-safe? | ||
| 96 | + "\0a", "a\0b", | ||
| 97 | + // WebIDL tests. | ||
| 98 | + undefined, null, 7, 12, 1.5, true, false, NaN, +Infinity, -Infinity, 0, -0, | ||
| 99 | + {toString: function() { return "foo" }}, | ||
| 100 | + ]; | ||
| 101 | + for (var i = 0; i < 258; i++) { | ||
| 102 | + tests.push(String.fromCharCode(i)); | ||
| 103 | + } | ||
| 104 | + tests.push(String.fromCharCode(10000)); | ||
| 105 | + tests.push(String.fromCharCode(65534)); | ||
| 106 | + tests.push(String.fromCharCode(65535)); | ||
| 107 | + | ||
| 108 | + // This is supposed to be U+10000. | ||
| 109 | + tests.push(String.fromCharCode(0xd800, 0xdc00)); | ||
| 110 | + tests = tests.map( | ||
| 111 | + function(elem) { | ||
| 112 | + var expected = mybtoa(elem); | ||
| 113 | + if (expected === "INVALID_CHARACTER_ERR") { | ||
| 114 | + return ["btoa(" + format_value(elem) + ") must raise INVALID_CHARACTER_ERR", elem]; | ||
| 115 | + } | ||
| 116 | + return ["btoa(" + format_value(elem) + ") == " + format_value(mybtoa(elem)), elem]; | ||
| 117 | + } | ||
| 118 | + ); | ||
| 119 | + | ||
| 120 | + var everything = ""; | ||
| 121 | + for (var i = 0; i < 256; i++) { | ||
| 122 | + everything += String.fromCharCode(i); | ||
| 123 | + } | ||
| 124 | + tests.push(["btoa(first 256 code points concatenated)", everything]); | ||
| 125 | + | ||
| 126 | + generate_tests(testBtoa, tests); | ||
| 127 | + | ||
| 128 | + promise_test(() => fetch("../../../fetch/data-urls/resources/base64.json").then(res => res.json()).then(runAtobTests), "atob() setup."); | ||
| 129 | + | ||
| 130 | + const idlTests = [ | ||
| 131 | + [undefined, null], | ||
| 132 | + [null, [158, 233, 101]], | ||
| 133 | + [7, null], | ||
| 134 | + [12, [215]], | ||
| 135 | + [1.5, null], | ||
| 136 | + [true, [182, 187]], | ||
| 137 | + [false, null], | ||
| 138 | + [NaN, [53, 163]], | ||
| 139 | + [+Infinity, [34, 119, 226, 158, 43, 114]], | ||
| 140 | + [-Infinity, null], | ||
| 141 | + [0, null], | ||
| 142 | + [-0, null], | ||
| 143 | + [{toString: function() { return "foo" }}, [126, 138]], | ||
| 144 | + [{toString: function() { return "abcd" }}, [105, 183, 29]] | ||
| 145 | + ]; | ||
| 146 | + | ||
| 147 | + function runAtobTests(tests) { | ||
| 148 | + const allTests = tests.concat(idlTests); | ||
| 149 | + for(let i = 0; i < allTests.length; i++) { | ||
| 150 | + const input = allTests[i][0], | ||
| 151 | + output = allTests[i][1]; | ||
| 152 | + test(() => { | ||
| 153 | + if(output === null) { | ||
| 154 | + assert_throws_dom("InvalidCharacterError", () => globalThis.atob(input)); | ||
| 155 | + } else { | ||
| 156 | + const result = globalThis.atob(input); | ||
| 157 | + for(let ii = 0; ii < output.length; ii++) { | ||
| 158 | + assert_equals(result.charCodeAt(ii), output[ii]); | ||
| 159 | + } | ||
| 160 | + } | ||
| 161 | + }, "atob(" + format_value(input) + ")"); | ||
| 162 | + } | ||
| 163 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,10 @@ | |||
| 19 | 19 | "commit": "8ada332aeac03764f73ec7ff66f682c5c0b454b4", | |
| 20 | 20 | "path": "interfaces" | |
| 21 | 21 | }, | |
| 22 | + "html/webappapis/atob": { | ||
| 23 | + "commit": "f267e1dca6f57a9f4d69f32a6920adfdb3268656", | ||
| 24 | + "path": "html/webappapis/atob" | ||
| 25 | + }, | ||
| 22 | 26 | "html/webappapis/microtask-queuing": { | |
| 23 | 27 | "commit": "0c3bed38df6d9dcd1441873728fb5c1bb59c92df", | |
| 24 | 28 | "path": "html/webappapis/microtask-queuing" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,5 @@ | |||
| 1 | + { | ||
| 2 | + "base64.any.js": { | ||
| 3 | + "fail": "promise_test: Unhandled rejection with value: object \"Error: ENOENT: no such file or directory, open '/root/node/node/fetch/data-urls/resources/base64.json'\"" | ||
| 4 | + } | ||
| 5 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + require('../common'); | ||
| 4 | + const { WPTRunner } = require('../common/wpt'); | ||
| 5 | + | ||
| 6 | + const runner = new WPTRunner('html/webappapis/atob'); | ||
| 7 | + | ||
| 8 | + // Needed to access to DOMException. | ||
| 9 | + runner.setFlags(['--expose-internals']); | ||
| 10 | + | ||
| 11 | + // Set a script that will be executed in the worker before running the tests. | ||
| 12 | + runner.setInitScript(` | ||
| 13 | + const { internalBinding } = require('internal/test/binding'); | ||
| 14 | + const { atob, btoa } = require('buffer'); | ||
| 15 | + const { DOMException } = internalBinding('messaging'); | ||
| 16 | + global.DOMException = DOMException; | ||
| 17 | + `); | ||
| 18 | + | ||
| 19 | + runner.runJsTests(); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments