Skip to content
This repository
Browse code

Add port of Dojo's util/url

  • Loading branch information...
commit 41468001fdd864a1d01bef02d2c86a127af0431a 1 parent 96885cf
Kris Zyp authored November 07, 2011

Showing 1 changed file with 105 additions and 0 deletions. Show diff stats Hide diff stats

  1. 105  util/url.js
105  util/url.js
... ...
@@ -0,0 +1,105 @@
  1
+var
  2
+	ore = new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$"),
  3
+	ire = new RegExp("^((([^\\[:]+):)?([^@]+)@)?(\\[([^\\]]+)\\]|([^\\[:]*))(:([0-9]+))?$"),
  4
+	_Url = function(){
  5
+		var n = null,
  6
+			_a = arguments,
  7
+			uri = [_a[0]];
  8
+		// resolve uri components relative to each other
  9
+		for(var i = 1; i<_a.length; i++){
  10
+			if(!_a[i]){ continue; }
  11
+
  12
+			// Safari doesn't support this.constructor so we have to be explicit
  13
+			// FIXME: Tracked (and fixed) in Webkit bug 3537.
  14
+			//		http://bugs.webkit.org/show_bug.cgi?id=3537
  15
+			var relobj = new _Url(_a[i]+""),
  16
+				uriobj = new _Url(uri[0]+"");
  17
+
  18
+			if(
  19
+				relobj.path == "" &&
  20
+				!relobj.scheme &&
  21
+				!relobj.authority &&
  22
+				!relobj.query
  23
+			){
  24
+				if(relobj.fragment != n){
  25
+					uriobj.fragment = relobj.fragment;
  26
+				}
  27
+				relobj = uriobj;
  28
+			}else if(!relobj.scheme){
  29
+				relobj.scheme = uriobj.scheme;
  30
+
  31
+				if(!relobj.authority){
  32
+					relobj.authority = uriobj.authority;
  33
+
  34
+					if(relobj.path.charAt(0) != "/"){
  35
+						var path = uriobj.path.substring(0,
  36
+							uriobj.path.lastIndexOf("/") + 1) + relobj.path;
  37
+
  38
+						var segs = path.split("/");
  39
+						for(var j = 0; j < segs.length; j++){
  40
+							if(segs[j] == "."){
  41
+								// flatten "./" references
  42
+								if(j == segs.length - 1){
  43
+									segs[j] = "";
  44
+								}else{
  45
+									segs.splice(j, 1);
  46
+									j--;
  47
+								}
  48
+							}else if(j > 0 && !(j == 1 && segs[0] == "") &&
  49
+								segs[j] == ".." && segs[j-1] != ".."){
  50
+								// flatten "../" references
  51
+								if(j == (segs.length - 1)){
  52
+									segs.splice(j, 1);
  53
+									segs[j - 1] = "";
  54
+								}else{
  55
+									segs.splice(j - 1, 2);
  56
+									j -= 2;
  57
+								}
  58
+							}
  59
+						}
  60
+						relobj.path = segs.join("/");
  61
+					}
  62
+				}
  63
+			}
  64
+
  65
+			uri = [];
  66
+			if(relobj.scheme){
  67
+				uri.push(relobj.scheme, ":");
  68
+			}
  69
+			if(relobj.authority){
  70
+				uri.push("//", relobj.authority);
  71
+			}
  72
+			uri.push(relobj.path);
  73
+			if(relobj.query){
  74
+				uri.push("?", relobj.query);
  75
+			}
  76
+			if(relobj.fragment){
  77
+				uri.push("#", relobj.fragment);
  78
+			}
  79
+		}
  80
+
  81
+		this.uri = uri.join("");
  82
+
  83
+		// break the uri into its main components
  84
+		var r = this.uri.match(ore);
  85
+
  86
+		this.scheme = r[2] || (r[1] ? "" : n);
  87
+		this.authority = r[4] || (r[3] ? "" : n);
  88
+		this.path = r[5]; // can never be undefined
  89
+		this.query = r[7] || (r[6] ? "" : n);
  90
+		this.fragment	 = r[9] || (r[8] ? "" : n);
  91
+
  92
+		if(this.authority != n){
  93
+			// server based naming authority
  94
+			r = this.authority.match(ire);
  95
+
  96
+			this.user = r[3] || n;
  97
+			this.password = r[4] || n;
  98
+			this.host = r[6] || r[7]; // ipv6 || ipv4
  99
+			this.port = r[9] || n;
  100
+		}
  101
+	};
  102
+_Url.prototype.toString = function(){ return this.uri; };
  103
+exports.resolve = function(base, next){
  104
+	return new _Url(base, next).toString();
  105
+};

0 notes on commit 4146800

Please sign in to comment.
Something went wrong with that request. Please try again.