Browse Source

URL parser improvements

Added punny encoded domains support
Simplified brackets checks
TitanNano-voice_recorder
Igor Zhukov 10 years ago
parent
commit
6db82b8951
  1. 46
      app/js/services.js

46
app/js/services.js

@ -3557,7 +3557,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
// domain name // domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier // TLD identifier
"(?:\\.([a-z\\u00a1-\\uffff]{2,24}))" + "(?:\\.(xn--[0-9a-z]{2,16}|[a-z\\u00a1-\\uffff]{2,24}))" +
")" + ")" +
// port number // port number
"(?::\\d{2,5})?" + "(?::\\d{2,5})?" +
@ -3647,25 +3647,24 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
var url = false, var url = false,
protocol = match[5], protocol = match[5],
tld = match[6], tld = match[6],
excluded = '', excluded = '';
test = '';
if (tld) { if (tld) {
if (Config.TLD.indexOf(tld.toLowerCase()) !== -1) { if (!protocol && (tld.substr(0, 4) === 'xn--' || Config.TLD.indexOf(tld.toLowerCase()) !== -1)) {
protocol = protocol || 'http://'; protocol = 'http://';
} }
if (protocol) { if (protocol) {
test = checkBrackets(match[4]); var balanced = checkBrackets(match[4]);
if (test.length !== match[4].length) { if (balanced.length !== match[4].length) {
excluded = match[4].substring(test.length); excluded = match[4].substring(balanced.length);
match[4] = test; match[4] = balanced;
} }
url = (match[5] ? '' : protocol) + match[4]; url = (match[5] ? '' : protocol) + match[4];
} }
} else { } else { // IP address
url = (match[5] ? '' : 'http://') + match[4]; url = (match[5] ? '' : 'http://') + match[4];
} }
@ -3762,29 +3761,12 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
urlOpenBrackets = url.split('(').length - 1, urlOpenBrackets = url.split('(').length - 1,
urlCloseBrackets = url.split(')').length - 1; urlCloseBrackets = url.split(')').length - 1;
if (url.charAt(urlLength - 1) === ')') { while (urlCloseBrackets > urlOpenBrackets &&
if (urlOpenBrackets > urlCloseBrackets) { url.charAt(urlLength - 1) === ')') {
for (var j = urlLength - 1; j >= 0; j--) { url = url.substr(0, urlLength - 1);
if (url.charAt(j) !== ')') { urlCloseBrackets--;
break; urlLength--;
} else {
url = url.substring(0, j);
}
}
} else if (urlOpenBrackets < urlCloseBrackets) {
while (urlCloseBrackets - urlOpenBrackets) {
var lastCharIndex = url.length - 1;
if (url.charAt(lastCharIndex) === ')') {
url = url.substring(0, lastCharIndex);
urlCloseBrackets--;
} else {
break;
}
}
}
} }
return url; return url;
} }

Loading…
Cancel
Save