]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #3927, #3976. Added elgg.parse_url() and elgg.parse_str().
authorBrett Profitt <brett.profitt@gmail.com>
Sat, 15 Oct 2011 05:30:33 +0000 (22:30 -0700)
committerBrett Profitt <brett.profitt@gmail.com>
Sat, 15 Oct 2011 05:30:33 +0000 (22:30 -0700)
js/lib/elgglib.js

index 9a372738db7049f92ee5e328f4a4f5d0409c1d2f..96bd04d7c9bad8e15c9e3ffe6576cf2d9f34360e 100644 (file)
@@ -352,6 +352,145 @@ elgg.forward = function(url) {
        location.href = elgg.normalize_url(url);
 };
 
+/**
+ * Parse a URL into its parts. Mimicks http://php.net/parse_url
+ *
+ * @param {String} url       The URL to parse
+ * @param {Int}    component A component to return
+ * @param {Bool}   expand Expand the query into an object? Else it's a string.
+ *
+ * @return {Object} The parsed URL
+ */
+elgg.parse_url = function(url, component, expand) {
+       // Adapted from http://blog.stevenlevithan.com/archives/parseuri
+       // which was release under the MIT
+       // It was modified to fix mailto: and javascript: support.
+       var
+       expand = expand || false,
+       component = component || false,
+       
+       re_str =
+               // scheme (and user@ testing)
+               '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
+               // possibly a user[:password]@
+               + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
+               // host and port
+               + '([^:/?#]*)(?::(\\d*))?)'
+               // path
+               + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
+               // query string
+               + '(?:\\?([^#]*))?'
+               // fragment
+               + '(?:#(.*))?)',
+       keys = {
+               'mailto':               {
+                       4: "scheme",
+                       5: "user",
+                       6: "host",
+                       9: "path",
+                       12: "query",
+                       13: "fragment"
+               },
+
+               'standard':             {
+                       1: "scheme",
+                       4: "user",
+                       5: "pass",
+                       6: "host",
+                       7: "port",
+                       9: "path",
+                       12: "query",
+                       13: "fragment"
+               }
+       },
+       results = {},
+       match_keys,
+       is_mailto = false;
+
+       var re = new RegExp(re_str);
+       var matches = re.exec(url);
+
+       // if the scheme field is undefined it means we're using a protocol
+       // without :// and an @. Feel free to fix this in the re if you can >:O
+       if (matches[1] == undefined) {
+               match_keys = keys['mailto'];
+               is_mailto = true;
+       } else {
+               match_keys = keys['standard'];
+       }
+
+       for (var i in match_keys) {
+               if (matches[i]) {
+                       results[match_keys[i]] = matches[i];
+               }
+       }
+
+       // merge everything to path if not standard
+       if (is_mailto) {
+               var path = '',
+               new_results = {};
+
+               if (typeof(results['user']) != 'undefined' && typeof(results['host']) != 'undefined') {
+                       path = results['user'] + '@' + results['host'];
+                       delete results['user'];
+                       delete results['host'];
+               } else if (typeof(results['user'])) {
+                       path = results['user'];
+                       delete results['user'];
+               } else if (typeof(results['host'])) {
+                       path = results['host'];
+                       delete results['host'];
+               }
+
+               if (typeof(results['path']) != 'undefined') {
+                       results['path'] = path + results['path'];
+               } else {
+                       results['path'] = path;
+               }
+
+               for (var prop in results) {
+                       new_results[prop] = results[prop];
+               }
+
+               results = new_results;
+       }
+
+       if (expand && typeof(results['query']) != 'undefined') {
+               results['query'] = elgg.parse_str(results['query']);
+       }
+
+       if (component) {
+               if (typeof(results[component]) != 'undefined') {
+                       return results[component];
+               } else {
+                       return false;
+               }
+       }
+       return results;
+}
+
+/**
+ * Returns an object with key/values of the parsed query string.
+ *
+ * @param  {String} string The string to parse
+ * @return {Object} The parsed object string
+ */
+elgg.parse_str = function(string) {
+       var params = {};
+       var result,
+               key,
+               value,
+               re = /([^&=]+)=?([^&]*)/g;
+
+       while (result = re.exec(string)) {
+               key = decodeURIComponent(result[1])
+               value = decodeURIComponent(result[2])
+               params[key] = value;
+       }
+       
+       return params;
+};
+
 /**
  * Returns a jQuery selector from a URL's fragement.  Defaults to expecting an ID.
  *