idna-uts46-hx
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -0,1 +1,14 @@ | ||
2017-04-18 | ||
========== | ||
* Switched automated tests to fit our own way | ||
* dropped out grunt | ||
* introduced nyc | ||
* introduced mocha.opts | ||
* npm test now runs both: tests and coverage report | ||
* add .nyc_output to gitignore | ||
* renamed test source code files to .spec.js as this is very common | ||
* re-validated source codes, still open: idna-map.js | ||
* updated .jshintrc config | ||
* added IDN translation cases found on unicode.org (see test-uts46.spec.js). Need to investigate further | ||
2017-04-10 | ||
@@ -2,0 +15,0 @@ ========== |
{ | ||
"name": "idna-uts46-hx", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A UTS #46 processing algorithm for IDNA2008 strings", | ||
@@ -12,4 +12,16 @@ "main": "uts46.js", | ||
], | ||
"nyc": { | ||
"reporter": [ | ||
"html", | ||
"text" | ||
], | ||
"exclude": [ | ||
"**/*.spec.js", | ||
"idna-map.js" | ||
] | ||
}, | ||
"scripts": { | ||
"test": "grunt test" | ||
"test": "npm run test-COVERAGE --silent", | ||
"test-MOCHA": "./node_modules/.bin/_mocha --opts mocha.opts $(find ./test -type f -name '*.spec.js')", | ||
"test-COVERAGE": "cross-env NODE_ENV=development nyc npm run test-MOCHA --silent" | ||
}, | ||
@@ -34,7 +46,6 @@ "repository": { | ||
"chai": "3.5.0", | ||
"grunt": "1.0.1", | ||
"grunt-mocha-istanbul": "5.0.2", | ||
"grunt-mocha-test": "0.13.2", | ||
"cross-env": "4.0.0", | ||
"istanbul": "0.4.5", | ||
"mocha": "3.2.0", | ||
"nyc": "10.2.0", | ||
"requirejs-browser": "2.1.9" | ||
@@ -41,0 +52,0 @@ }, |
177
uts46.js
@@ -1,105 +0,116 @@ | ||
/* istanbul ignore next */ | ||
(function (root, factory) { | ||
(function(root, factory) { | ||
/* istanbul ignore next */ | ||
if (typeof define === 'function' && define.amd) { | ||
define(['punycode', './idna-map'], function (punycode, idna_map) { | ||
define(['punycode', './idna-map'], function(punycode, idna_map) { | ||
return factory(punycode, idna_map); | ||
}); | ||
} else if (typeof exports === 'object') { | ||
} | ||
else if (typeof exports === 'object') { | ||
module.exports = factory(require('punycode'), require('./idna-map')); | ||
} else { | ||
} | ||
else { | ||
root.uts46 = factory(root.punycode, root.idna_map); | ||
} | ||
}(this, function (punycode, idna_map) { | ||
}(this, function(punycode, idna_map) { | ||
function mapLabel(label, useStd3ASCII, transitional) { | ||
var mapped = []; | ||
for (var ch of label) { | ||
var cp = ch.codePointAt(0); | ||
var composite = idna_map.mapChar(cp); | ||
var flags = (composite >> 23); | ||
var kind = (composite >> 21) & 3; | ||
var index = (composite >> 5) & 0xffff; | ||
var length = composite & 0x1f; | ||
var value = idna_map.mapStr.substr(index, length); | ||
if (kind == 0 || (useStd3ASCII && (flags & 1))) { | ||
throw new Error("Illegal char " + ch); | ||
} else if (kind == 1) { | ||
mapped.push(value); | ||
} else if (kind == 2) { | ||
mapped.push(transitional ? value : ch); | ||
} else if (kind == 3) { | ||
mapped.push(ch); | ||
function mapLabel(label, useStd3ASCII, transitional) { | ||
var mapped = []; | ||
for (var ch of label) { | ||
var cp = ch.codePointAt(0); | ||
var composite = idna_map.mapChar(cp); | ||
var flags = (composite >> 23); | ||
var kind = (composite >> 21) & 3; | ||
var index = (composite >> 5) & 0xffff; | ||
var length = composite & 0x1f; | ||
var value = idna_map.mapStr.substr(index, length); | ||
if (kind === 0 || (useStd3ASCII && (flags & 1))) { | ||
throw new Error("Illegal char " + ch); | ||
} | ||
else if (kind === 1) { | ||
mapped.push(value); | ||
} | ||
else if (kind === 2) { | ||
mapped.push(transitional ? value : ch); | ||
} | ||
/* istanbul ignore next */ | ||
else if (kind === 3) { | ||
mapped.push(ch); | ||
} | ||
} | ||
var newLabel = mapped.join("").normalize("NFC"); | ||
return newLabel; | ||
} | ||
var newLabel = mapped.join("").normalize("NFC"); | ||
return newLabel; | ||
} | ||
function process(domain, transitional, useStd3ASCII) { | ||
if (useStd3ASCII === undefined) | ||
function process(domain, transitional, useStd3ASCII) { | ||
/* istanbul ignore if */ | ||
if (useStd3ASCII === undefined) | ||
useStd3ASCII = false; | ||
var mappedIDNA = mapLabel(domain, useStd3ASCII, transitional); | ||
var mappedIDNA = mapLabel(domain, useStd3ASCII, transitional); | ||
// Step 3. Break | ||
var labels = mappedIDNA.split("."); | ||
// Step 3. Break | ||
var labels = mappedIDNA.split("."); | ||
// Step 4. Convert/Validate | ||
labels = labels.map(function (label) { | ||
if (label.startsWith("xn--")) { | ||
label = punycode.decode(label.substring(4)); | ||
validateLabel(label, useStd3ASCII, false); | ||
} else { | ||
validateLabel(label, useStd3ASCII, transitional); | ||
} | ||
return label; | ||
}); | ||
// Step 4. Convert/Validate | ||
labels = labels.map(function(label) { | ||
if (label.startsWith("xn--")) { | ||
label = punycode.decode(label.substring(4)); | ||
validateLabel(label, useStd3ASCII, false); | ||
} | ||
else { | ||
validateLabel(label, useStd3ASCII, transitional); | ||
} | ||
return label; | ||
}); | ||
return labels.join("."); | ||
} | ||
return labels.join("."); | ||
} | ||
function validateLabel(label, useStd3ASCII, transitional) { | ||
// 2. The label must not contain a U+002D HYPHEN-MINUS character in both the | ||
// third position and fourth positions. | ||
if (label[2] == '-' && label[3] == '-') | ||
throw new Error("Failed to validate " + label); | ||
function validateLabel(label, useStd3ASCII, transitional) { | ||
// 2. The label must not contain a U+002D HYPHEN-MINUS character in both the | ||
// third position and fourth positions. | ||
if (label[2] === '-' && label[3] === '-') | ||
throw new Error("Failed to validate " + label); | ||
// 3. The label must neither begin nor end with a U+002D HYPHEN-MINUS | ||
// character. | ||
if (label.startsWith('-') || label.endsWith('-')) | ||
throw new Error("Failed to validate " + label); | ||
// 3. The label must neither begin nor end with a U+002D HYPHEN-MINUS | ||
// character. | ||
if (label.startsWith('-') || label.endsWith('-')) | ||
throw new Error("Failed to validate " + label); | ||
// 4. The label must not contain a U+002E ( . ) FULL STOP. | ||
if (label.includes('.')) | ||
throw new Error("Failed to validate " + label); | ||
// 4. The label must not contain a U+002E ( . ) FULL STOP. | ||
// this should nerver happen as label is chunked internally by this character | ||
/* istanbul ignore if */ | ||
if (label.includes('.')) | ||
throw new Error("Failed to validate " + label); | ||
if (mapLabel(label, useStd3ASCII, transitional) != label) | ||
throw new Error("Failed to validate " + label); | ||
if (mapLabel(label, useStd3ASCII, transitional) !== label) | ||
throw new Error("Failed to validate " + label); | ||
// 5. The label must not begin with a combining mark, that is: | ||
// General_Category=Mark. | ||
var ch = label.codePointAt(0); | ||
if (idna_map.mapChar(ch) & (0x2 << 23)) | ||
throw new Error("Label contains illegal character: " + ch); | ||
} | ||
// 5. The label must not begin with a combining mark, that is: | ||
// General_Category=Mark. | ||
var ch = label.codePointAt(0); | ||
if (idna_map.mapChar(ch) & (0x2 << 23)) | ||
throw new Error("Label contains illegal character: " + ch); | ||
} | ||
function toAscii(domain, options) { | ||
if (options === undefined) | ||
options = { }; | ||
var transitional = 'transitional' in options ? options.transitional : true; | ||
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false; | ||
var labels = process(domain, transitional, useStd3ASCII).split('.'); | ||
return labels.map(punycode.toASCII).join('.'); | ||
} | ||
function toUnicode(domain, options) { | ||
if (options === undefined) | ||
options = { }; | ||
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false; | ||
return process(domain, false, useStd3ASCII); | ||
} | ||
function toAscii(domain, options) { | ||
if (options === undefined) | ||
options = {}; | ||
var transitional = 'transitional' in options ? options.transitional : true; | ||
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false; | ||
var labels = process(domain, transitional, useStd3ASCII).split('.'); | ||
return labels.map(punycode.toASCII).join('.'); | ||
} | ||
return { | ||
toUnicode: toUnicode, | ||
toAscii: toAscii, | ||
}; | ||
function toUnicode(domain, options) { | ||
if (options === undefined) | ||
options = {}; | ||
var useStd3ASCII = 'useStd3ASCII' in options ? options.useStd3ASCII : false; | ||
return process(domain, false, useStd3ASCII); | ||
} | ||
return { | ||
toUnicode: toUnicode, | ||
toAscii: toAscii, | ||
}; | ||
})); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197356
6
1509