Skip to content

Commit 232c4c2

Browse files
mathiasbynensindutny
authored andcommitted
punycode: update to v1.3.2
Changes since v1.2.3: * Email address support in `toASCII` and `toUnicode` * `punycode.ucs2.encode` now no longer mutates the `codePoints` argument * Ensure trailing `.` in domain names are preserved * Some minor code cleanup + bug fixes Reviewed-By: Fedor Indutny <[email protected]> PR-URL: #6
1 parent 574407a commit 232c4c2

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

lib/punycode.js

+57-34
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
/*! http://mths.be/punycode v1.2.3 by @mathias */
1+
/*! https://mths.be/punycode v1.3.2 by @mathias */
22
;(function(root) {
33

44
/** Detect free variables */
5-
var freeExports = typeof exports == 'object' && exports;
5+
var freeExports = typeof exports == 'object' && exports &&
6+
!exports.nodeType && exports;
67
var freeModule = typeof module == 'object' && module &&
7-
module.exports == freeExports && module;
8+
!module.nodeType && module;
89
var freeGlobal = typeof global == 'object' && global;
9-
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
10+
if (
11+
freeGlobal.global === freeGlobal ||
12+
freeGlobal.window === freeGlobal ||
13+
freeGlobal.self === freeGlobal
14+
) {
1015
root = freeGlobal;
1116
}
1217

@@ -32,8 +37,8 @@
3237

3338
/** Regular expressions */
3439
regexPunycode = /^xn--/,
35-
regexNonASCII = /[^ -~]/, // unprintable ASCII chars + non-ASCII chars
36-
regexSeparators = /\x2E|\u3002|\uFF0E|\uFF61/g, // RFC 3490 separators
40+
regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars
41+
regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators
3742

3843
/** Error messages */
3944
errors = {
@@ -72,23 +77,37 @@
7277
*/
7378
function map(array, fn) {
7479
var length = array.length;
80+
var result = [];
7581
while (length--) {
76-
array[length] = fn(array[length]);
82+
result[length] = fn(array[length]);
7783
}
78-
return array;
84+
return result;
7985
}
8086

8187
/**
82-
* A simple `Array#map`-like wrapper to work with domain name strings.
88+
* A simple `Array#map`-like wrapper to work with domain name strings or email
89+
* addresses.
8390
* @private
84-
* @param {String} domain The domain name.
91+
* @param {String} domain The domain name or email address.
8592
* @param {Function} callback The function that gets called for every
8693
* character.
8794
* @returns {Array} A new string of characters returned by the callback
8895
* function.
8996
*/
9097
function mapDomain(string, fn) {
91-
return map(string.split(regexSeparators), fn).join('.');
98+
var parts = string.split('@');
99+
var result = '';
100+
if (parts.length > 1) {
101+
// In email addresses, only the domain name should be punycoded. Leave
102+
// the local part (i.e. everything up to `@`) intact.
103+
result = parts[0] + '@';
104+
string = parts[1];
105+
}
106+
// Avoid `split(regex)` for IE8 compatibility. See #17.
107+
string = string.replace(regexSeparators, '\x2E');
108+
var labels = string.split('.');
109+
var encoded = map(labels, fn).join('.');
110+
return result + encoded;
92111
}
93112

94113
/**
@@ -98,7 +117,7 @@
98117
* UCS-2 exposes as separate characters) into a single code point,
99118
* matching UTF-16.
100119
* @see `punycode.ucs2.encode`
101-
* @see <http://mathiasbynens.be/notes/javascript-encoding>
120+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
102121
* @memberOf punycode.ucs2
103122
* @name decode
104123
* @param {String} string The Unicode input string (UCS-2).
@@ -192,7 +211,7 @@
192211

193212
/**
194213
* Bias adaptation function as per section 3.4 of RFC 3492.
195-
* http://tools.ietf.org/html/rfc3492#section-3.4
214+
* https://tools.ietf.org/html/rfc3492#section-3.4
196215
* @private
197216
*/
198217
function adapt(delta, numPoints, firstTime) {
@@ -307,8 +326,8 @@
307326
}
308327

309328
/**
310-
* Converts a string of Unicode symbols to a Punycode string of ASCII-only
311-
* symbols.
329+
* Converts a string of Unicode symbols (e.g. a domain name label) to a
330+
* Punycode string of ASCII-only symbols.
312331
* @memberOf punycode
313332
* @param {String} input The string of Unicode symbols.
314333
* @returns {String} The resulting Punycode string of ASCII-only symbols.
@@ -421,33 +440,37 @@
421440
}
422441

423442
/**
424-
* Converts a Punycode string representing a domain name to Unicode. Only the
425-
* Punycoded parts of the domain name will be converted, i.e. it doesn't
426-
* matter if you call it on a string that has already been converted to
427-
* Unicode.
443+
* Converts a Punycode string representing a domain name or an email address
444+
* to Unicode. Only the Punycoded parts of the input will be converted, i.e.
445+
* it doesn't matter if you call it on a string that has already been
446+
* converted to Unicode.
428447
* @memberOf punycode
429-
* @param {String} domain The Punycode domain name to convert to Unicode.
448+
* @param {String} input The Punycoded domain name or email address to
449+
* convert to Unicode.
430450
* @returns {String} The Unicode representation of the given Punycode
431451
* string.
432452
*/
433-
function toUnicode(domain) {
434-
return mapDomain(domain, function(string) {
453+
function toUnicode(input) {
454+
return mapDomain(input, function(string) {
435455
return regexPunycode.test(string)
436456
? decode(string.slice(4).toLowerCase())
437457
: string;
438458
});
439459
}
440460

441461
/**
442-
* Converts a Unicode string representing a domain name to Punycode. Only the
443-
* non-ASCII parts of the domain name will be converted, i.e. it doesn't
444-
* matter if you call it with a domain that's already in ASCII.
462+
* Converts a Unicode string representing a domain name or an email address to
463+
* Punycode. Only the non-ASCII parts of the domain name will be converted,
464+
* i.e. it doesn't matter if you call it with a domain that's already in
465+
* ASCII.
445466
* @memberOf punycode
446-
* @param {String} domain The domain name to convert, as a Unicode string.
447-
* @returns {String} The Punycode representation of the given domain name.
467+
* @param {String} input The domain name or email address to convert, as a
468+
* Unicode string.
469+
* @returns {String} The Punycode representation of the given domain name or
470+
* email address.
448471
*/
449-
function toASCII(domain) {
450-
return mapDomain(domain, function(string) {
472+
function toASCII(input) {
473+
return mapDomain(input, function(string) {
451474
return regexNonASCII.test(string)
452475
? 'xn--' + encode(string)
453476
: string;
@@ -463,11 +486,11 @@
463486
* @memberOf punycode
464487
* @type String
465488
*/
466-
'version': '1.2.3',
489+
'version': '1.3.2',
467490
/**
468491
* An object of methods to convert from JavaScript's internal character
469492
* representation (UCS-2) to Unicode code points, and back.
470-
* @see <http://mathiasbynens.be/notes/javascript-encoding>
493+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
471494
* @memberOf punycode
472495
* @type Object
473496
*/
@@ -489,11 +512,11 @@
489512
typeof define.amd == 'object' &&
490513
define.amd
491514
) {
492-
define(function() {
515+
define('punycode', function() {
493516
return punycode;
494517
});
495-
} else if (freeExports && !freeExports.nodeType) {
496-
if (freeModule) { // in Node.js or RingoJS v0.8.0+
518+
} else if (freeExports && freeModule) {
519+
if (module.exports == freeExports) { // in Node.js or RingoJS v0.8.0+
497520
freeModule.exports = punycode;
498521
} else { // in Narwhal or RingoJS v0.7.0-
499522
for (key in punycode) {

0 commit comments

Comments
 (0)