|
| 1 | +(function(root) { |
| 2 | + |
| 3 | + // NOTES: |
| 4 | + // Provides utf8 only implementation of TextEncoder and TextDecoder for IE10+ |
| 5 | + // https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder |
| 6 | + // https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder |
| 7 | + |
| 8 | + // It is a modified version of https://gist.github.com/Yaffle/5458286 for IE10+ and Uint8Array compatibility |
| 9 | + // https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#Methods |
| 10 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array |
| 11 | + |
| 12 | + if (typeof root.TextEncoder === 'undefined') { |
| 13 | + |
| 14 | + // Normalise String.prototype.codePointAt for IE10+ |
| 15 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt |
| 16 | + function StringToCodePointAt(string, position) { |
| 17 | + if (String.prototype.codePointAt) return string.codePointAt(position); |
| 18 | + string = String(string); |
| 19 | + var size = string.length; |
| 20 | + // `ToInteger` |
| 21 | + var index = position ? Number(position) : 0; |
| 22 | + if (index != index) { // better `isNaN` |
| 23 | + index = 0; |
| 24 | + } |
| 25 | + // Account for out-of-bounds indices: |
| 26 | + if (index < 0 || index >= size) { |
| 27 | + return undefined; |
| 28 | + } |
| 29 | + // Get the first code unit |
| 30 | + var first = string.charCodeAt(index); |
| 31 | + var second; |
| 32 | + if ( // check if it’s the start of a surrogate pair |
| 33 | + first >= 0xD800 && first <= 0xDBFF && // high surrogate |
| 34 | + size > index + 1 // there is a next code unit |
| 35 | + ) { |
| 36 | + second = string.charCodeAt(index + 1); |
| 37 | + if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate |
| 38 | + // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae |
| 39 | + return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; |
| 40 | + } |
| 41 | + } |
| 42 | + return first; |
| 43 | + }; |
| 44 | + |
| 45 | + function TextEncoder(encoding) { |
| 46 | + if (!encoding) encoding = 'utf8'; |
| 47 | + switch (encoding) { |
| 48 | + case 'utf-8': |
| 49 | + case 'utf8': |
| 50 | + break; |
| 51 | + default: |
| 52 | + throw 'TextEncoder only supports utf8'; |
| 53 | + } |
| 54 | + } |
| 55 | + TextEncoder.prototype.encode = function (string) { |
| 56 | + var octets = new Uint8Array(string.length * 3); |
| 57 | + var pos = -1; |
| 58 | + var length = string.length; |
| 59 | + var i = 0; |
| 60 | + while (i < length) { |
| 61 | + var codePoint = StringToCodePointAt(string, i); |
| 62 | + var c = 0; |
| 63 | + var bits = 0; |
| 64 | + if (codePoint <= 0x0000007F) { |
| 65 | + c = 0; |
| 66 | + bits = 0x00; |
| 67 | + } else if (codePoint <= 0x000007FF) { |
| 68 | + c = 6; |
| 69 | + bits = 0xC0; |
| 70 | + } else if (codePoint <= 0x0000FFFF) { |
| 71 | + c = 12; |
| 72 | + bits = 0xE0; |
| 73 | + } else if (codePoint <= 0x001FFFFF) { |
| 74 | + c = 18; |
| 75 | + bits = 0xF0; |
| 76 | + } |
| 77 | + octets[pos += 1] = (bits | (codePoint >> c)); |
| 78 | + c -= 6; |
| 79 | + while (c >= 0) { |
| 80 | + octets[pos += 1] = (0x80 | ((codePoint >> c) & 0x3F)); |
| 81 | + c -= 6; |
| 82 | + } |
| 83 | + i += codePoint >= 0x10000 ? 2 : 1; |
| 84 | + } |
| 85 | + return octets.subarray(0, pos+1); |
| 86 | + }; |
| 87 | + |
| 88 | + root.TextEncoder = TextEncoder; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + if (typeof root.TextDecoder === 'undefined') { |
| 93 | + |
| 94 | + // Normalise String.fromCodePointAt for IE10+ |
| 95 | + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint |
| 96 | + function StringFromCodePoint(_) { |
| 97 | + if (String.fromCodePoint) return String.fromCodePoint(_); |
| 98 | + var codeUnits = [], codeLen = 0, result = ''; |
| 99 | + for (var index=0, len = arguments.length; index !== len; ++index) { |
| 100 | + var codePoint = +arguments[index]; |
| 101 | + // correctly handles all cases including `NaN`, `-Infinity`, `+Infinity` |
| 102 | + // The surrounding `!(...)` is required to correctly handle `NaN` cases |
| 103 | + // The (codePoint>>>0) === codePoint clause handles decimals and negatives |
| 104 | + if (!(codePoint < 0x10FFFF && (codePoint>>>0) === codePoint)) |
| 105 | + throw RangeError('Invalid code point: ' + codePoint); |
| 106 | + if (codePoint <= 0xFFFF) { // BMP code point |
| 107 | + codeLen = codeUnits.push(codePoint); |
| 108 | + } else { // Astral code point; split in surrogate halves |
| 109 | + // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae |
| 110 | + codePoint -= 0x10000; |
| 111 | + codeLen = codeUnits.push( |
| 112 | + (codePoint >> 10) + 0xD800, // highSurrogate |
| 113 | + (codePoint % 0x400) + 0xDC00 // lowSurrogate |
| 114 | + ); |
| 115 | + } |
| 116 | + if (codeLen >= 0x3fff) { |
| 117 | + result += String.fromCharCode.apply(null, codeUnits); |
| 118 | + codeUnits.length = 0; |
| 119 | + } |
| 120 | + } |
| 121 | + return result + String.fromCharCode.apply(null, codeUnits); |
| 122 | + }; |
| 123 | + |
| 124 | + function TextDecoder(encoding) { |
| 125 | + if (!encoding) encoding = 'utf8'; |
| 126 | + switch (encoding) { |
| 127 | + case 'utf-8': |
| 128 | + case 'utf8': |
| 129 | + break; |
| 130 | + default: |
| 131 | + throw 'TextDecoder only supports utf8'; |
| 132 | + } |
| 133 | + } |
| 134 | + TextDecoder.prototype.decode = function (octets) { |
| 135 | + var string = ''; |
| 136 | + var i = 0; |
| 137 | + while (i < octets.length) { |
| 138 | + var octet = octets[i]; |
| 139 | + var bytesNeeded = 0; |
| 140 | + var codePoint = 0; |
| 141 | + if (octet <= 0x7F) { |
| 142 | + bytesNeeded = 0; |
| 143 | + codePoint = octet & 0xFF; |
| 144 | + } else if (octet <= 0xDF) { |
| 145 | + bytesNeeded = 1; |
| 146 | + codePoint = octet & 0x1F; |
| 147 | + } else if (octet <= 0xEF) { |
| 148 | + bytesNeeded = 2; |
| 149 | + codePoint = octet & 0x0F; |
| 150 | + } else if (octet <= 0xF4) { |
| 151 | + bytesNeeded = 3; |
| 152 | + codePoint = octet & 0x07; |
| 153 | + } |
| 154 | + if (octets.length - i - bytesNeeded > 0) { |
| 155 | + var k = 0; |
| 156 | + while (k < bytesNeeded) { |
| 157 | + octet = octets[i + k + 1]; |
| 158 | + codePoint = (codePoint << 6) | (octet & 0x3F); |
| 159 | + k += 1; |
| 160 | + } |
| 161 | + } else { |
| 162 | + codePoint = 0xFFFD; |
| 163 | + bytesNeeded = octets.length - i; |
| 164 | + } |
| 165 | + string += StringFromCodePoint(codePoint); |
| 166 | + i += bytesNeeded + 1; |
| 167 | + } |
| 168 | + return string |
| 169 | + }; |
| 170 | + |
| 171 | + root.TextDecoder = TextDecoder; |
| 172 | + |
| 173 | + } |
| 174 | + |
| 175 | + })(this); |
0 commit comments