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 <fedor@indutny.com>
PR-URL: https://github.com/iojs/io.js/pull/6
This commit is contained in:
Mathias Bynens 2014-11-28 10:50:12 +01:00 committed by Fedor Indutny
parent 574407a67f
commit 232c4c27cc

View File

@ -1,12 +1,17 @@
/*! http://mths.be/punycode v1.2.3 by @mathias */ /*! https://mths.be/punycode v1.3.2 by @mathias */
;(function(root) { ;(function(root) {
/** Detect free variables */ /** Detect free variables */
var freeExports = typeof exports == 'object' && exports; var freeExports = typeof exports == 'object' && exports &&
!exports.nodeType && exports;
var freeModule = typeof module == 'object' && module && var freeModule = typeof module == 'object' && module &&
module.exports == freeExports && module; !module.nodeType && module;
var freeGlobal = typeof global == 'object' && global; var freeGlobal = typeof global == 'object' && global;
if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { if (
freeGlobal.global === freeGlobal ||
freeGlobal.window === freeGlobal ||
freeGlobal.self === freeGlobal
) {
root = freeGlobal; root = freeGlobal;
} }
@ -32,8 +37,8 @@
/** Regular expressions */ /** Regular expressions */
regexPunycode = /^xn--/, regexPunycode = /^xn--/,
regexNonASCII = /[^ -~]/, // unprintable ASCII chars + non-ASCII chars regexNonASCII = /[^\x20-\x7E]/, // unprintable ASCII chars + non-ASCII chars
regexSeparators = /\x2E|\u3002|\uFF0E|\uFF61/g, // RFC 3490 separators regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g, // RFC 3490 separators
/** Error messages */ /** Error messages */
errors = { errors = {
@ -72,23 +77,37 @@
*/ */
function map(array, fn) { function map(array, fn) {
var length = array.length; var length = array.length;
var result = [];
while (length--) { while (length--) {
array[length] = fn(array[length]); result[length] = fn(array[length]);
} }
return array; return result;
} }
/** /**
* A simple `Array#map`-like wrapper to work with domain name strings. * A simple `Array#map`-like wrapper to work with domain name strings or email
* addresses.
* @private * @private
* @param {String} domain The domain name. * @param {String} domain The domain name or email address.
* @param {Function} callback The function that gets called for every * @param {Function} callback The function that gets called for every
* character. * character.
* @returns {Array} A new string of characters returned by the callback * @returns {Array} A new string of characters returned by the callback
* function. * function.
*/ */
function mapDomain(string, fn) { function mapDomain(string, fn) {
return map(string.split(regexSeparators), fn).join('.'); var parts = string.split('@');
var result = '';
if (parts.length > 1) {
// In email addresses, only the domain name should be punycoded. Leave
// the local part (i.e. everything up to `@`) intact.
result = parts[0] + '@';
string = parts[1];
}
// Avoid `split(regex)` for IE8 compatibility. See #17.
string = string.replace(regexSeparators, '\x2E');
var labels = string.split('.');
var encoded = map(labels, fn).join('.');
return result + encoded;
} }
/** /**
@ -98,7 +117,7 @@
* UCS-2 exposes as separate characters) into a single code point, * UCS-2 exposes as separate characters) into a single code point,
* matching UTF-16. * matching UTF-16.
* @see `punycode.ucs2.encode` * @see `punycode.ucs2.encode`
* @see <http://mathiasbynens.be/notes/javascript-encoding> * @see <https://mathiasbynens.be/notes/javascript-encoding>
* @memberOf punycode.ucs2 * @memberOf punycode.ucs2
* @name decode * @name decode
* @param {String} string The Unicode input string (UCS-2). * @param {String} string The Unicode input string (UCS-2).
@ -192,7 +211,7 @@
/** /**
* Bias adaptation function as per section 3.4 of RFC 3492. * Bias adaptation function as per section 3.4 of RFC 3492.
* http://tools.ietf.org/html/rfc3492#section-3.4 * https://tools.ietf.org/html/rfc3492#section-3.4
* @private * @private
*/ */
function adapt(delta, numPoints, firstTime) { function adapt(delta, numPoints, firstTime) {
@ -307,8 +326,8 @@
} }
/** /**
* Converts a string of Unicode symbols to a Punycode string of ASCII-only * Converts a string of Unicode symbols (e.g. a domain name label) to a
* symbols. * Punycode string of ASCII-only symbols.
* @memberOf punycode * @memberOf punycode
* @param {String} input The string of Unicode symbols. * @param {String} input The string of Unicode symbols.
* @returns {String} The resulting Punycode string of ASCII-only symbols. * @returns {String} The resulting Punycode string of ASCII-only symbols.
@ -421,17 +440,18 @@
} }
/** /**
* Converts a Punycode string representing a domain name to Unicode. Only the * Converts a Punycode string representing a domain name or an email address
* Punycoded parts of the domain name will be converted, i.e. it doesn't * to Unicode. Only the Punycoded parts of the input will be converted, i.e.
* matter if you call it on a string that has already been converted to * it doesn't matter if you call it on a string that has already been
* Unicode. * converted to Unicode.
* @memberOf punycode * @memberOf punycode
* @param {String} domain The Punycode domain name to convert to Unicode. * @param {String} input The Punycoded domain name or email address to
* convert to Unicode.
* @returns {String} The Unicode representation of the given Punycode * @returns {String} The Unicode representation of the given Punycode
* string. * string.
*/ */
function toUnicode(domain) { function toUnicode(input) {
return mapDomain(domain, function(string) { return mapDomain(input, function(string) {
return regexPunycode.test(string) return regexPunycode.test(string)
? decode(string.slice(4).toLowerCase()) ? decode(string.slice(4).toLowerCase())
: string; : string;
@ -439,15 +459,18 @@
} }
/** /**
* Converts a Unicode string representing a domain name to Punycode. Only the * Converts a Unicode string representing a domain name or an email address to
* non-ASCII parts of the domain name will be converted, i.e. it doesn't * Punycode. Only the non-ASCII parts of the domain name will be converted,
* matter if you call it with a domain that's already in ASCII. * i.e. it doesn't matter if you call it with a domain that's already in
* ASCII.
* @memberOf punycode * @memberOf punycode
* @param {String} domain The domain name to convert, as a Unicode string. * @param {String} input The domain name or email address to convert, as a
* @returns {String} The Punycode representation of the given domain name. * Unicode string.
* @returns {String} The Punycode representation of the given domain name or
* email address.
*/ */
function toASCII(domain) { function toASCII(input) {
return mapDomain(domain, function(string) { return mapDomain(input, function(string) {
return regexNonASCII.test(string) return regexNonASCII.test(string)
? 'xn--' + encode(string) ? 'xn--' + encode(string)
: string; : string;
@ -463,11 +486,11 @@
* @memberOf punycode * @memberOf punycode
* @type String * @type String
*/ */
'version': '1.2.3', 'version': '1.3.2',
/** /**
* An object of methods to convert from JavaScript's internal character * An object of methods to convert from JavaScript's internal character
* representation (UCS-2) to Unicode code points, and back. * representation (UCS-2) to Unicode code points, and back.
* @see <http://mathiasbynens.be/notes/javascript-encoding> * @see <https://mathiasbynens.be/notes/javascript-encoding>
* @memberOf punycode * @memberOf punycode
* @type Object * @type Object
*/ */
@ -489,11 +512,11 @@
typeof define.amd == 'object' && typeof define.amd == 'object' &&
define.amd define.amd
) { ) {
define(function() { define('punycode', function() {
return punycode; return punycode;
}); });
} else if (freeExports && !freeExports.nodeType) { } else if (freeExports && freeModule) {
if (freeModule) { // in Node.js or RingoJS v0.8.0+ if (module.exports == freeExports) { // in Node.js or RingoJS v0.8.0+
freeModule.exports = punycode; freeModule.exports = punycode;
} else { // in Narwhal or RingoJS v0.7.0- } else { // in Narwhal or RingoJS v0.7.0-
for (key in punycode) { for (key in punycode) {