source: Dev/branches/rest-dojo-ui/client/dojo/_base/url.js @ 263

Last change on this file since 263 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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