Fix parsing ports
This commit is contained in:
Родитель
8dbc5eac6f
Коммит
0481954d71
|
@ -2,13 +2,14 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
|
|||
if (URL === null || URL === "") {
|
||||
return URL;
|
||||
}
|
||||
|
||||
var URLobj = null;
|
||||
try {
|
||||
URLobj = new window.URL(URL);
|
||||
}
|
||||
|
||||
catch (err) {
|
||||
if (ignoreProtocol) {
|
||||
if (ignoreProtocol) {
|
||||
try {
|
||||
URLobj = new window.URL("http://" + URL);
|
||||
}
|
||||
|
@ -20,11 +21,11 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
|
|||
return URL;
|
||||
}
|
||||
}
|
||||
|
||||
var parser = document.createElement('a');
|
||||
parser.href = URL;
|
||||
|
||||
|
||||
|
||||
var protocol = parser.protocol;
|
||||
var host = parser.hostname;
|
||||
var path = parser.pathname;
|
||||
|
@ -50,21 +51,30 @@ function processURL(URL, ignoreProtocol, ignoreSubdomain, ignorePath, ignorePort
|
|||
}
|
||||
else {
|
||||
var result = host.match(/[^./]+\.[^./]+$/); // catch the two last parts, it's de hostname and the tld
|
||||
baseHost=result[0];
|
||||
baseHost = result[0];
|
||||
}
|
||||
var returnURL = "";
|
||||
if (!ignoreProtocol) {
|
||||
returnURL += protocol + "//";
|
||||
}
|
||||
|
||||
if (!ignoreSubdomain) {
|
||||
returnURL += host;
|
||||
}
|
||||
else {
|
||||
returnURL += baseHost;//return the hostname and the tld of the website if ignoreSubdomain is check
|
||||
}
|
||||
|
||||
if (ignorePort) {
|
||||
returnURL = returnURL.replace(':' + port, "");
|
||||
if (port) {
|
||||
returnURL = returnURL.replace(':' + port, '');
|
||||
}
|
||||
} else {
|
||||
if (port) {
|
||||
returnURL += ':' + port;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ignorePath && path !== null && path) {
|
||||
returnURL += path;
|
||||
}
|
||||
|
|
|
@ -1349,13 +1349,13 @@ describe('Testing url parser', function () {
|
|||
var url = testCombinations[i];
|
||||
(function (url) {
|
||||
|
||||
var props = 'ignoreProtocol: ' + url.ignoreProtocol + ' ignoreSubdomain:' + url.ignoreSubdomain + ' ignorePath: ' + url.ignorePath + ' ignorePort:' + url.ignorePort
|
||||
var props = 'ignoreProtocol: ' + url.ignoreProtocol + ' ignoreSubdomain:' + url.ignoreSubdomain + ' ignorePath: ' + url.ignorePath + ' ignorePort:' + url.ignorePort;
|
||||
|
||||
it(props, function () {
|
||||
var result = processURL(url.url, url.ignoreProtocol, url.ignoreSubdomain, url.ignorePath, url.ignorePort);
|
||||
var parser = document.createElement('a');
|
||||
parser.href = url.url;
|
||||
expect(url.url + '->' + result).toEqual(url.url + '->' + url.expected)
|
||||
expect(url.url + '->' + result).toEqual(url.url + '->' + url.expected);
|
||||
});
|
||||
}(url));
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче