Skip to content

Commit cf89bee

Browse files
mathiasbynensbnoordhuis
authored andcommitted
punycode: Update to v0.2.1
1 parent b204006 commit cf89bee

1 file changed

Lines changed: 48 additions & 98 deletions

File tree

lib/punycode.js

Lines changed: 48 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@
44
* Available under MIT license <http://mths.be/mit>
55
*/
66

7-
;(function(window) {
7+
;(function(root) {
88

99
/**
10-
* The `Punycode` object.
11-
* @name Punycode
10+
* The `punycode` object.
11+
* @name punycode
1212
* @type Object
1313
*/
14-
var Punycode,
14+
var punycode,
1515

16-
/** Detect free variables `define`, `exports`, and `require` */
16+
/** Detect free variables `define`, `exports`, `module` and `require` */
1717
freeDefine = typeof define == 'function' && typeof define.amd == 'object' &&
1818
define.amd && define,
19-
freeExports = typeof exports == 'object' && exports &&
20-
(typeof global == 'object' && global && global == global.global &&
21-
(window = global), exports),
19+
freeExports = typeof exports == 'object' && exports,
20+
freeModule = typeof module == 'object' && module,
2221
freeRequire = typeof require == 'function' && require,
2322

2423
/** Highest positive signed 32-bit float value */
@@ -35,7 +34,7 @@
3534
delimiter = '-', // '\x2D'
3635

3736
/** Regular expressions */
38-
regexASCII = /[^\x20-\x7e]/,
37+
regexNonASCII = /[^ -~]/, // matches unprintable ASCII chars + non-ASCII chars
3938
regexPunycode = /^xn--/,
4039

4140
/** Error messages */
@@ -50,7 +49,10 @@
5049
/** Convenience shortcuts */
5150
baseMinusTMin = base - tMin,
5251
floor = Math.floor,
53-
stringFromCharCode = String.fromCharCode;
52+
stringFromCharCode = String.fromCharCode,
53+
54+
/** Temporary variable */
55+
key;
5456

5557
/*--------------------------------------------------------------------------*/
5658

@@ -97,8 +99,9 @@
9799
/**
98100
* Creates an array containing the decimal code points of each character in
99101
* the string.
100-
* @see `Punycode.utf16.encode`
101-
* @memberOf Punycode.utf16
102+
* @see `punycode.utf16.encode`
103+
* @see <http://tools.ietf.org/html/rfc2781>
104+
* @memberOf punycode.utf16
102105
* @name decode
103106
* @param {String} string The Unicode input string.
104107
* @returns {Array} The new array.
@@ -125,8 +128,9 @@
125128

126129
/**
127130
* Creates a string based on an array of decimal code points.
128-
* @see `Punycode.utf16.decode`
129-
* @memberOf Punycode.utf16
131+
* @see `punycode.utf16.decode`
132+
* @see <http://tools.ietf.org/html/rfc2781>
133+
* @memberOf punycode.utf16
130134
* @name encode
131135
* @param {Array} codePoints The array of decimal code points.
132136
* @returns {String} The new string.
@@ -215,25 +219,13 @@
215219
/**
216220
* Converts a Punycode string of ASCII code points to a string of Unicode
217221
* code points.
218-
* @memberOf Punycode
222+
* @memberOf punycode
219223
* @param {String} input The Punycode string of ASCII code points.
220-
* @param {Boolean} preserveCase A boolean value indicating if character
221-
* case should be preserved or not.
222224
* @returns {String} The resulting string of Unicode code points.
223225
*/
224-
function decode(input, preserveCase) {
226+
function decode(input) {
225227
// Don't use UTF-16
226228
var output = [],
227-
/**
228-
* The `caseFlags` array needs room for at least `output.length` values,
229-
* or it can be `undefined` if the case information is not needed. A
230-
* truthy value suggests that the corresponding Unicode character be
231-
* forced to uppercase (if possible), while falsy values suggest that it
232-
* be forced to lowercase (if possible). ASCII code points are output
233-
* already in the proper case, but their flags will be set appropriately
234-
* so that applying the flags would be harmless.
235-
*/
236-
caseFlags = [],
237229
inputLength = input.length,
238230
out,
239231
i = 0,
@@ -261,18 +253,15 @@
261253
}
262254

263255
for (j = 0; j < basic; ++j) {
264-
if (preserveCase) {
265-
caseFlags[output.length] = input.charCodeAt(j) - 65 < 26;
266-
}
267256
// if it's not a basic code point
268257
if (input.charCodeAt(j) >= 0x80) {
269258
error('not-basic');
270259
}
271260
output.push(input.charCodeAt(j));
272261
}
273262

274-
// Main decoding loop: start just after the last delimiter if any basic
275-
// code points were copied; start at the beginning otherwise.
263+
// Main decoding loop: start just after the last delimiter if any basic code
264+
// points were copied; start at the beginning otherwise.
276265

277266
for (index = basic > 0 ? basic + 1 : 0; index < inputLength; /* no final expression */) {
278267

@@ -322,36 +311,21 @@
322311
i %= out;
323312

324313
// Insert `n` at position `i` of the output
325-
// The case of the last character determines `uppercase` flag
326-
if (preserveCase) {
327-
caseFlags.splice(i, 0, input.charCodeAt(index - 1) - 65 < 26);
328-
}
329-
330-
output.splice(i, 0, n);
331-
i++;
314+
output.splice(i++, 0, n);
332315

333316
}
334317

335-
if (preserveCase) {
336-
for (i = 0, length = output.length; i < length; i++) {
337-
if (caseFlags[i]) {
338-
output[i] = (stringFromCharCode(output[i]).toUpperCase()).charCodeAt(0);
339-
}
340-
}
341-
}
342318
return utf16encode(output);
343319
}
344320

345321
/**
346322
* Converts a string of Unicode code points to a Punycode string of ASCII
347323
* code points.
348-
* @memberOf Punycode
324+
* @memberOf punycode
349325
* @param {String} input The string of Unicode code points.
350-
* @param {Boolean} preserveCase A boolean value indicating if character
351-
* case should be preserved or not.
352326
* @returns {String} The resulting Punycode string of ASCII code points.
353327
*/
354-
function encode(input, preserveCase) {
328+
function encode(input) {
355329
var n,
356330
delta,
357331
handledCPCount,
@@ -363,18 +337,6 @@
363337
k,
364338
t,
365339
currentValue,
366-
/**
367-
* The `caseFlags` array will hold `inputLength` boolean values, where
368-
* `true` suggests that the corresponding Unicode character be forced
369-
* to uppercase after being decoded (if possible), and `false`
370-
* suggests that it be forced to lowercase (if possible). ASCII code
371-
* points are encoded literally, except that ASCII letters are forced
372-
* to uppercase or lowercase according to the corresponding uppercase
373-
* flags. If `caseFlags` remains `undefined` then ASCII letters are
374-
* left as they are, and other code points are treated as if their
375-
* uppercase flags were `true`.
376-
*/
377-
caseFlags,
378340
output = [],
379341
/** `inputLength` will hold the number of code points in `input`. */
380342
inputLength,
@@ -383,24 +345,12 @@
383345
baseMinusT,
384346
qMinusT;
385347

386-
if (preserveCase) {
387-
// Preserve case, step 1 of 2: get a list of the unaltered string
388-
caseFlags = utf16decode(input);
389-
}
390-
391348
// Convert the input in UTF-16 to Unicode
392349
input = utf16decode(input);
393350

394351
// Cache the length
395352
inputLength = input.length;
396353

397-
if (preserveCase) {
398-
// Preserve case, step 2 of 2: modify the list to true/false
399-
for (j = 0; j < inputLength; j++) {
400-
caseFlags[j] = input[j] != caseFlags[j];
401-
}
402-
}
403-
404354
// Initialize the state
405355
n = initialN;
406356
delta = 0;
@@ -410,11 +360,7 @@
410360
for (j = 0; j < inputLength; ++j) {
411361
currentValue = input[j];
412362
if (currentValue < 0x80) {
413-
output.push(
414-
stringFromCharCode(
415-
caseFlags ? encodeBasic(currentValue, caseFlags[j]) : currentValue
416-
)
417-
);
363+
output.push(stringFromCharCode(currentValue));
418364
}
419365
}
420366

@@ -433,7 +379,6 @@
433379

434380
// All non-basic code points < n have been handled already. Find the next
435381
// larger one:
436-
437382
for (m = maxInt, j = 0; j < inputLength; ++j) {
438383
currentValue = input[j];
439384
if (currentValue >= n && currentValue < m) {
@@ -473,7 +418,7 @@
473418
q = floor(qMinusT / baseMinusT);
474419
}
475420

476-
output.push(stringFromCharCode(digitToBasic(q, preserveCase && caseFlags[j] ? 1 : 0)));
421+
output.push(stringFromCharCode(digitToBasic(q, 0)));
477422
bias = adapt(delta, handledCPCountPlusOne, handledCPCount == basicLength);
478423
delta = 0;
479424
++handledCPCount;
@@ -492,7 +437,7 @@
492437
* Punycoded parts of the domain name will be converted, i.e. it doesn't
493438
* matter if you call it on a string that has already been converted to
494439
* Unicode.
495-
* @memberOf Punycode
440+
* @memberOf punycode
496441
* @param {String} domain The Punycode domain name to convert to Unicode.
497442
* @returns {String} The Unicode representation of the given Punycode
498443
* string.
@@ -509,13 +454,13 @@
509454
* Converts a Unicode string representing a domain name to Punycode. Only the
510455
* non-ASCII parts of the domain name will be converted, i.e. it doesn't
511456
* matter if you call it with a domain that's already in ASCII.
512-
* @memberOf Punycode
457+
* @memberOf punycode
513458
* @param {String} domain The domain name to convert, as a Unicode string.
514459
* @returns {String} The Punycode representation of the given domain name.
515460
*/
516461
function toASCII(domain) {
517462
return mapDomain(domain, function(string) {
518-
return regexASCII.test(string)
463+
return regexNonASCII.test(string)
519464
? 'xn--' + encode(string)
520465
: string;
521466
});
@@ -524,12 +469,17 @@
524469
/*--------------------------------------------------------------------------*/
525470

526471
/** Define the public API */
527-
Punycode = {
528-
'version': '0.1.1',
472+
punycode = {
473+
/**
474+
* A string representing the current Punycode.js version number.
475+
* @memberOf punycode
476+
* @type String
477+
*/
478+
'version': '0.2.1',
529479
/**
530480
* An object of methods to convert from JavaScript's internal character
531481
* representation to Unicode and back.
532-
* @memberOf Punycode
482+
* @memberOf punycode
533483
* @type Object
534484
*/
535485
'utf16': {
@@ -542,23 +492,23 @@
542492
'toUnicode': toUnicode
543493
};
544494

545-
/** Expose Punycode */
495+
/** Expose `punycode` */
546496
if (freeExports) {
547-
if (typeof module == 'object' && module && module.exports == freeExports) {
548-
// in Node.js
549-
module.exports = Punycode;
497+
if (freeModule && freeModule.exports == freeExports) {
498+
// in Node.js or Ringo 0.8+
499+
freeModule.exports = punycode;
550500
} else {
551-
// in Narwhal or Ringo
552-
freeExports.Punycode = Punycode;
501+
// in Narwhal or Ringo 0.7-
502+
for (key in punycode) {
503+
punycode.hasOwnProperty(key) && (freeExports[key] = punycode[key]);
504+
}
553505
}
554506
} else if (freeDefine) {
555507
// via curl.js or RequireJS
556-
freeDefine(function() {
557-
return Punycode;
558-
});
508+
define('punycode', punycode);
559509
} else {
560510
// in a browser or Rhino
561-
window.Punycode = Punycode;
511+
root.punycode = punycode;
562512
}
563513

564514
}(this));

0 commit comments

Comments
 (0)