diff --git a/index.js b/index.js index c9b69dd..873041a 100644 --- a/index.js +++ b/index.js @@ -178,6 +178,19 @@ function serialize (cmpts, opts) { return uriTokens.join('') } +const hexLookUp = Array.from({ length: 127 }, (v, k) => /[^!"$&'()*+,.;=_`a-z{}~-]/.test(String.fromCharCode(k))) + +function nonSimpleDomain (value) { + let code = 0 + for (let i = 0, len = value.length; i < len; ++i) { + code = value.charCodeAt(i) + if (code > 126 || hexLookUp[code]) { + return true + } + } + return false +} + const URI_PARSE = /^(?:([^:/?#]+):)?(?:\/\/((?:([^/?#@]*)@)?(\[[^/?#\]]+\]|[^/?#:]*)(?::(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i function parse (uri, opts) { @@ -239,7 +252,7 @@ function parse (uri, opts) { // check if scheme can't handle IRIs if (!options.unicodeSupport && (!schemeHandler || !schemeHandler.unicodeSupport)) { // if host component is a domain name - if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost))) { + if (parsed.host && (options.domainHost || (schemeHandler && schemeHandler.domainHost)) && nonSimpleDomain(parsed.host)) { // convert Unicode IDN -> ASCII IDN try { parsed.host = URL.domainToASCII(parsed.host.toLowerCase())