forked from allmarkedup/purl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.url.js
170 lines (135 loc) · 5.32 KB
/
jquery.url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* JQuery URL Parser plugin
* Developed and maintanined by Mark Perkins, [email protected]
* Source repository: https://github.com/allmarkedup/jQuery-URL-Parser
* Licensed under an MIT-style license. See https://github.com/allmarkedup/jQuery-URL-Parser/blob/master/LICENSE for details.
*/
;(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD available; use anonymous module
define(['jquery'], factory);
} else {
// No AMD available; mutate global vars
factory(jQuery);
}
})(function($, undefined) {
var tag2attr = {
a : 'href',
img : 'src',
form : 'action',
base : 'href',
script : 'src',
iframe : 'src',
link : 'href'
},
key = ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","fragment"], // keys available to query
aliases = { "anchor" : "fragment" }, // aliases for backwards compatability
parser = {
strict : /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, //less intuitive, more accurate to the specs
loose : /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // more intuitive, fails on relative paths and deviates from specs
},
querystring_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g, // supports both ampersand and semicolon-delimted query string key/value pairs
fragment_parser = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g; // supports both ampersand and semicolon-delimted fragment key/value pairs
function parseUri( url, strictMode )
{
var str = decodeURI( unescape(url) ),
res = parser[ strictMode || false ? "strict" : "loose" ].exec( str ),
uri = { attr : {}, param : {}, seg : {} },
i = 14;
while ( i-- )
{
uri.attr[ key[i] ] = res[i] || "";
}
// build query and fragment parameters
uri.param['query'] = {};
uri.param['fragment'] = {};
uri.attr['query'].replace( querystring_parser, function ( $0, $1, $2 ){
if ($1)
{
uri.param['query'][$1] = $2;
}
});
uri.attr['fragment'].replace( fragment_parser, function ( $0, $1, $2 ){
if ($1)
{
uri.param['fragment'][$1] = $2;
}
});
// split path and fragement into segments
uri.seg['path'] = uri.attr.path.replace(/^\/+|\/+$/g,'').split('/');
uri.seg['fragment'] = uri.attr.fragment.replace(/^\/+|\/+$/g,'').split('/');
// compile a 'base' domain attribute
uri.attr['base'] = uri.attr.host ? (uri.attr.protocol ? uri.attr.protocol+"://"+uri.attr.host : uri.attr.host) + (uri.attr.port ? ":"+uri.attr.port : '') : '';
return uri;
};
function getAttrName( elm )
{
var tn = elm.tagName;
if ( tn !== undefined ) return tag2attr[tn.toLowerCase()];
return tn;
}
$.fn.url = function( strictMode )
{
var url = '';
if ( this.length )
{
url = $(this).attr( getAttrName(this[0]) ) || '';
}
return $.url( url, strictMode );
};
$.url = function( url, strictMode )
{
if ( arguments.length === 1 && url === true )
{
strictMode = true;
url = undefined;
}
strictMode = strictMode || false;
url = url || window.location.toString();
return {
data : parseUri(url, strictMode),
// get various attributes from the URI
attr : function( attr )
{
attr = aliases[attr] || attr;
return attr !== undefined ? this.data.attr[attr] : this.data.attr;
},
// return query string parameters
param : function( param )
{
return param !== undefined ? this.data.param.query[param] : this.data.param.query;
},
// return fragment parameters
fparam : function( param )
{
return param !== undefined ? this.data.param.fragment[param] : this.data.param.fragment;
},
// return path segments
segment : function( seg )
{
if ( seg === undefined )
{
return this.data.seg.path;
}
else
{
seg = seg < 0 ? this.data.seg.path.length + seg : seg - 1; // negative segments count from the end
return this.data.seg.path[seg];
}
},
// return fragment segments
fsegment : function( seg )
{
if ( seg === undefined )
{
return this.data.seg.fragment;
}
else
{
seg = seg < 0 ? this.data.seg.fragment.length + seg : seg - 1; // negative segments count from the end
return this.data.seg.fragment[seg];
}
}
};
};
});