diff options
Diffstat (limited to 'data/util.js')
-rw-r--r-- | data/util.js | 86 |
1 files changed, 64 insertions, 22 deletions
diff --git a/data/util.js b/data/util.js index 5e8afcc..09360a0 100644 --- a/data/util.js +++ b/data/util.js @@ -4,29 +4,46 @@ // http://www.opensource.org/licenses/mit-license.php "use strict"; // ============================================================== + /** - * get parameters of URL as an object (name, value) + * parse URL to get its parts. + * + * @param url + * @return object with all parsed parts of URL as properties + * + * Originally from http://james.padolsey.com/javascript/parsing-urls-with-the-dom/ + * Copyright February 19th, 2009, James Padolsey, <license undeclared> + * + * This function creates a new anchor element and uses location + * properties (inherent) to get the desired URL data. Some String + * operations are used (to normalize results across browsers). */ -function getParamsFromURL (url, base) { - if (!url || (url.toString().length === 0)) { - throw new Error("Missing URL value!"); - } - - var aElem = document.createElement("a"); - aElem.href = url; - - var paramsArr = url.pathname.split("?"); - if (paramsArr.length === 1) { - return {}; - } - - // get convert URL parameters to an Object - var params = {}, s = []; - paramsArr[1].split('&').forEach(function(par) { - s = par.split('='); - params[s[0]] = s[1]; - }); - return params; +function parseURL(url) { + var a = document.createElement('a'); + a.href = url; + return { + source: url, + protocol: a.protocol.replace(':',''), + host: a.hostname, + port: a.port, + query: a.search, + params: (function(){ + var ret = {}, + seg = a.search.replace(/^\?/,'').split('&'), + len = seg.length, i = 0, s; + for (;i<len;i++) { + if (!seg[i]) { continue; } + s = seg[i].split('='); + ret[s[0]] = s[1]; + } + return ret; + })(), + file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1], + hash: a.hash.replace('#',''), + path: a.pathname.replace(/^([^\/])/,'/$1'), + relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1], + segments: a.pathname.replace(/^\//,'').split('/') + }; } /** @@ -56,12 +73,37 @@ function getBugNo() { * @return String with the bug ID */ function getBugNoFromURL(url) { - var params = getParamsFromURL(url); + var params = parseURL(url).params; if (params && params.id) { return params.id; } } +/* + * From <a> element diggs out just plain email address + * Note that bugzilla.gnome.org doesn't have mailto: url but + * https://bugzilla.gnome.org/page.cgi?id=describeuser.html&login=mcepl%40redhat.com + * + * @param aElement Element with href attribute or something else + * @return String with the address or null + * + */ +function parseMailto(aElement) { + var emailStr = "", hrefStr = ""; + // use url utils + if (aElement) { + hrefStr = decodeURIComponent(aElement.getAttribute("href")); + emailStr = hrefStr.split(":"); + // workaround for Gnome bugzilla ... no mailto: here. + if (emailStr.length < 2) { + var params = parseURL("https://" + window.location.hostname + "/" + hrefStr).params; + return decodeURI(params.login); + } + return emailStr[1]; + } + return null; +} + /** * format date to be in ISO format (just day part) * |