|
1 | 1 | ;(function () {
|
2 | 2 |
|
3 |
| - var |
4 |
| - object = typeof exports != 'undefined' ? exports : this, // #8: web workers |
5 |
| - chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', |
6 |
| - INVALID_CHARACTER_ERR = (function () { |
7 |
| - // fabricate a suitable error object |
8 |
| - try { document.createElement('$'); } |
9 |
| - catch (error) { return error; }}()); |
| 3 | + var object = typeof exports != 'undefined' ? exports : this; // #8: web workers |
| 4 | + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
| 5 | + |
| 6 | + function InvalidCharacterError(message) { |
| 7 | + this.message = message; |
| 8 | + } |
| 9 | + InvalidCharacterError.prototype = new Error; |
| 10 | + InvalidCharacterError.prototype.name = 'InvalidCharacterError'; |
10 | 11 |
|
11 | 12 | // encoder
|
12 | 13 | // [https://gist.github.com/999166] by [https://github.com/nignag]
|
|
23 | 24 | output += map.charAt(63 & block >> 8 - idx % 1 * 8)
|
24 | 25 | ) {
|
25 | 26 | charCode = input.charCodeAt(idx += 3/4);
|
26 |
| - if (charCode > 0xFF) throw INVALID_CHARACTER_ERR; |
| 27 | + if (charCode > 0xFF) { |
| 28 | + throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."); |
| 29 | + } |
27 | 30 | block = block << 8 | charCode;
|
28 | 31 | }
|
29 | 32 | return output;
|
|
34 | 37 | object.atob || (
|
35 | 38 | object.atob = function (input) {
|
36 | 39 | input = input.replace(/=+$/, '')
|
37 |
| - if (input.length % 4 == 1) throw INVALID_CHARACTER_ERR; |
| 40 | + if (input.length % 4 == 1) { |
| 41 | + throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded."); |
| 42 | + } |
38 | 43 | for (
|
39 | 44 | // initialize result and counters
|
40 | 45 | var bc = 0, bs, buffer, idx = 0, output = '';
|
|
0 commit comments