|
1 | | -/*! http://mths.be/punycode v1.2.0 by @mathias */ |
| 1 | +/*! http://mths.be/punycode v1.2.3 by @mathias */ |
2 | 2 | ;(function(root) { |
3 | 3 |
|
| 4 | + /** Detect free variables */ |
| 5 | + var freeExports = typeof exports == 'object' && exports; |
| 6 | + var freeModule = typeof module == 'object' && module && |
| 7 | + module.exports == freeExports && module; |
| 8 | + var freeGlobal = typeof global == 'object' && global; |
| 9 | + if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { |
| 10 | + root = freeGlobal; |
| 11 | + } |
| 12 | + |
4 | 13 | /** |
5 | 14 | * The `punycode` object. |
6 | 15 | * @name punycode |
7 | 16 | * @type Object |
8 | 17 | */ |
9 | 18 | var punycode, |
10 | 19 |
|
11 | | - /** Detect free variables `define`, `exports`, `module` and `require` */ |
12 | | - freeDefine = typeof define == 'function' && typeof define.amd == 'object' && |
13 | | - define.amd && define, |
14 | | - freeExports = typeof exports == 'object' && exports, |
15 | | - freeModule = typeof module == 'object' && module, |
16 | | - freeRequire = typeof require == 'function' && require, |
17 | | - |
18 | 20 | /** Highest positive signed 32-bit float value */ |
19 | 21 | maxInt = 2147483647, // aka. 0x7FFFFFFF or 2^31-1 |
20 | 22 |
|
|
90 | 92 | } |
91 | 93 |
|
92 | 94 | /** |
93 | | - * Creates an array containing the decimal code points of each Unicode |
| 95 | + * Creates an array containing the numeric code points of each Unicode |
94 | 96 | * character in the string. While JavaScript uses UCS-2 internally, |
95 | 97 | * this function will convert a pair of surrogate halves (each of which |
96 | 98 | * UCS-2 exposes as separate characters) into a single code point, |
|
110 | 112 | extra; |
111 | 113 | while (counter < length) { |
112 | 114 | value = string.charCodeAt(counter++); |
113 | | - if ((value & 0xF800) == 0xD800 && counter < length) { |
| 115 | + if (value >= 0xD800 && value <= 0xDBFF && counter < length) { |
114 | 116 | // high surrogate, and there is a next character |
115 | 117 | extra = string.charCodeAt(counter++); |
116 | 118 | if ((extra & 0xFC00) == 0xDC00) { // low surrogate |
117 | 119 | output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); |
118 | 120 | } else { |
119 | | - output.push(value, extra); |
| 121 | + // unmatched surrogate; only append this code unit, in case the next |
| 122 | + // code unit is the high surrogate of a surrogate pair |
| 123 | + output.push(value); |
| 124 | + counter--; |
120 | 125 | } |
121 | 126 | } else { |
122 | 127 | output.push(value); |
|
126 | 131 | } |
127 | 132 |
|
128 | 133 | /** |
129 | | - * Creates a string based on an array of decimal code points. |
| 134 | + * Creates a string based on an array of numeric code points. |
130 | 135 | * @see `punycode.ucs2.decode` |
131 | 136 | * @memberOf punycode.ucs2 |
132 | 137 | * @name encode |
133 | | - * @param {Array} codePoints The array of decimal code points. |
| 138 | + * @param {Array} codePoints The array of numeric code points. |
134 | 139 | * @returns {String} The new Unicode string (UCS-2). |
135 | 140 | */ |
136 | 141 | function ucs2encode(array) { |
|
150 | 155 | * Converts a basic code point into a digit/integer. |
151 | 156 | * @see `digitToBasic()` |
152 | 157 | * @private |
153 | | - * @param {Number} codePoint The basic (decimal) code point. |
| 158 | + * @param {Number} codePoint The basic numeric code point value. |
154 | 159 | * @returns {Number} The numeric value of a basic code point (for use in |
155 | 160 | * representing integers) in the range `0` to `base - 1`, or `base` if |
156 | 161 | * the code point does not represent a value. |
157 | 162 | */ |
158 | 163 | function basicToDigit(codePoint) { |
159 | | - return codePoint - 48 < 10 |
160 | | - ? codePoint - 22 |
161 | | - : codePoint - 65 < 26 |
162 | | - ? codePoint - 65 |
163 | | - : codePoint - 97 < 26 |
164 | | - ? codePoint - 97 |
165 | | - : base; |
| 164 | + if (codePoint - 48 < 10) { |
| 165 | + return codePoint - 22; |
| 166 | + } |
| 167 | + if (codePoint - 65 < 26) { |
| 168 | + return codePoint - 65; |
| 169 | + } |
| 170 | + if (codePoint - 97 < 26) { |
| 171 | + return codePoint - 97; |
| 172 | + } |
| 173 | + return base; |
166 | 174 | } |
167 | 175 |
|
168 | 176 | /** |
|
174 | 182 | * representing integers) is `digit`, which needs to be in the range |
175 | 183 | * `0` to `base - 1`. If `flag` is non-zero, the uppercase form is |
176 | 184 | * used; else, the lowercase form is used. The behavior is undefined |
177 | | - * if flag is non-zero and `digit` has no uppercase form. |
| 185 | + * if `flag` is non-zero and `digit` has no uppercase form. |
178 | 186 | */ |
179 | 187 | function digitToBasic(digit, flag) { |
180 | 188 | // 0..25 map to ASCII a..z or A..Z |
|
198 | 206 | } |
199 | 207 |
|
200 | 208 | /** |
201 | | - * Converts a basic code point to lowercase if `flag` is falsy, or to |
202 | | - * uppercase if `flag` is truthy. The code point is unchanged if it's |
203 | | - * caseless. The behavior is undefined if `codePoint` is not a basic code |
204 | | - * point. |
205 | | - * @private |
206 | | - * @param {Number} codePoint The numeric value of a basic code point. |
207 | | - * @returns {Number} The resulting basic code point. |
208 | | - */ |
209 | | - function encodeBasic(codePoint, flag) { |
210 | | - codePoint -= (codePoint - 97 < 26) << 5; |
211 | | - return codePoint + (!flag && codePoint - 65 < 26) << 5; |
212 | | - } |
213 | | - |
214 | | - /** |
215 | | - * Converts a Punycode string of ASCII code points to a string of Unicode |
216 | | - * code points. |
| 209 | + * Converts a Punycode string of ASCII-only symbols to a string of Unicode |
| 210 | + * symbols. |
217 | 211 | * @memberOf punycode |
218 | | - * @param {String} input The Punycode string of ASCII code points. |
219 | | - * @returns {String} The resulting string of Unicode code points. |
| 212 | + * @param {String} input The Punycode string of ASCII-only symbols. |
| 213 | + * @returns {String} The resulting string of Unicode symbols. |
220 | 214 | */ |
221 | 215 | function decode(input) { |
222 | 216 | // Don't use UCS-2 |
|
314 | 308 | } |
315 | 309 |
|
316 | 310 | /** |
317 | | - * Converts a string of Unicode code points to a Punycode string of ASCII |
318 | | - * code points. |
| 311 | + * Converts a string of Unicode symbols to a Punycode string of ASCII-only |
| 312 | + * symbols. |
319 | 313 | * @memberOf punycode |
320 | | - * @param {String} input The string of Unicode code points. |
321 | | - * @returns {String} The resulting Punycode string of ASCII code points. |
| 314 | + * @param {String} input The string of Unicode symbols. |
| 315 | + * @returns {String} The resulting Punycode string of ASCII-only symbols. |
322 | 316 | */ |
323 | 317 | function encode(input) { |
324 | 318 | var n, |
|
470 | 464 | * @memberOf punycode |
471 | 465 | * @type String |
472 | 466 | */ |
473 | | - 'version': '1.2.0', |
| 467 | + 'version': '1.2.3', |
474 | 468 | /** |
475 | 469 | * An object of methods to convert from JavaScript's internal character |
476 | | - * representation (UCS-2) to decimal Unicode code points, and back. |
| 470 | + * representation (UCS-2) to Unicode code points, and back. |
477 | 471 | * @see <http://mathiasbynens.be/notes/javascript-encoding> |
478 | 472 | * @memberOf punycode |
479 | 473 | * @type Object |
|
489 | 483 | }; |
490 | 484 |
|
491 | 485 | /** Expose `punycode` */ |
492 | | - if (freeExports) { |
493 | | - if (freeModule && freeModule.exports == freeExports) { |
494 | | - // in Node.js or Ringo 0.8+ |
| 486 | + // Some AMD build optimizers, like r.js, check for specific condition patterns |
| 487 | + // like the following: |
| 488 | + if ( |
| 489 | + typeof define == 'function' && |
| 490 | + typeof define.amd == 'object' && |
| 491 | + define.amd |
| 492 | + ) { |
| 493 | + define(function() { |
| 494 | + return punycode; |
| 495 | + }); |
| 496 | + } else if (freeExports && !freeExports.nodeType) { |
| 497 | + if (freeModule) { // in Node.js or RingoJS v0.8.0+ |
495 | 498 | freeModule.exports = punycode; |
496 | | - } else { |
497 | | - // in Narwhal or Ringo 0.7- |
| 499 | + } else { // in Narwhal or RingoJS v0.7.0- |
498 | 500 | for (key in punycode) { |
499 | 501 | punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]); |
500 | 502 | } |
501 | 503 | } |
502 | | - } else if (freeDefine) { |
503 | | - // via curl.js or RequireJS |
504 | | - define('punycode', punycode); |
505 | | - } else { |
506 | | - // in a browser or Rhino |
| 504 | + } else { // in Rhino or a web browser |
507 | 505 | root.punycode = punycode; |
508 | 506 | } |
509 | 507 |
|
|
0 commit comments