-
Notifications
You must be signed in to change notification settings - Fork 146
/
jquery.uri.js
287 lines (269 loc) · 8.02 KB
/
jquery.uri.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* $ URIs @VERSION
*
* Copyright (c) 2008,2009 Jeni Tennison
* Licensed under the MIT (MIT-LICENSE.txt)
*
*/
/**
* @fileOverview $ URIs
* @author <a href="mailto:[email protected]">Jeni Tennison</a>
* @copyright (c) 2008,2009 Jeni Tennison
* @license MIT license (MIT-LICENSE.txt)
* @version 1.0
*/
/**
* @class
* @name jQuery
* @exports $ as jQuery
* @description rdfQuery is a <a href="http://jquery.com/">jQuery</a> plugin. The only fields and methods listed here are those that come as part of the rdfQuery library.
*/
(function ($) {
var
mem = {},
uriRegex = /^(([a-z][\-a-z0-9+\.]*):)?(\/\/([^\/?#]+))?([^?#]*)?(\?([^#]*))?(#(.*))?$/i,
docURI,
parseURI = function (u) {
var m = u.match(uriRegex);
if (m === null) {
throw "Malformed URI: " + u;
}
return {
scheme: m[1] ? m[2].toLowerCase() : undefined,
authority: m[3] ? m[4] : undefined,
path: m[5] || '',
query: m[6] ? m[7] : undefined,
fragment: m[8] ? m[9] : undefined
};
},
removeDotSegments = function (u) {
var r = '', m = [];
if (/\./.test(u)) {
while (u !== undefined && u !== '') {
if (u === '.' || u === '..') {
u = '';
} else if (/^\.\.\//.test(u)) { // starts with ../
u = u.substring(3);
} else if (/^\.\//.test(u)) { // starts with ./
u = u.substring(2);
} else if (/^\/\.(\/|$)/.test(u)) { // starts with /./ or consists of /.
u = '/' + u.substring(3);
} else if (/^\/\.\.(\/|$)/.test(u)) { // starts with /../ or consists of /..
u = '/' + u.substring(4);
r = r.replace(/\/?[^\/]+$/, '');
} else {
m = u.match(/^(\/?[^\/]*)(\/.*)?$/);
u = m[2];
r = r + m[1];
}
}
return r;
} else {
return u;
}
},
merge = function (b, r) {
if (b.authority !== '' && (b.path === undefined || b.path === '')) {
return '/' + r;
} else {
return b.path.replace(/[^\/]+$/, '') + r;
}
};
/**
* Creates a new jQuery.uri object. This should be invoked as a method rather than constructed using new.
* @class Represents a URI
* @param {String} [relative='']
* @param {String|jQuery.uri} [base] Defaults to the base URI of the page
* @returns {jQuery.uri} The new jQuery.uri object.
* @example uri = jQuery.uri('/my/file.html');
*/
$.uri = function (relative, base) {
var uri;
relative = relative || '';
if (mem[relative]) {
return mem[relative];
}
base = base || $.uri.base();
if (typeof base === 'string') {
base = $.uri.absolute(base);
}
uri = new $.uri.fn.init(relative, base);
if (mem[uri]) {
return mem[uri];
} else {
mem[uri] = uri;
return uri;
}
};
$.uri.fn = $.uri.prototype = {
/**
* The scheme used in the URI
* @type String
*/
scheme: undefined,
/**
* The authority used in the URI
* @type String
*/
authority: undefined,
/**
* The path used in the URI
* @type String
*/
path: undefined,
/**
* The query part of the URI
* @type String
*/
query: undefined,
/**
* The fragment part of the URI
* @type String
*/
fragment: undefined,
init: function (relative, base) {
var r = {};
base = base || {};
$.extend(this, parseURI(relative));
if (this.scheme === undefined) {
this.scheme = base.scheme;
if (this.authority !== undefined) {
this.path = removeDotSegments(this.path);
} else {
this.authority = base.authority;
if (this.path === '') {
this.path = base.path;
if (this.query === undefined) {
this.query = base.query;
}
} else {
if (!/^\//.test(this.path)) {
this.path = merge(base, this.path);
}
this.path = removeDotSegments(this.path);
}
}
}
if (this.scheme === undefined) {
throw "Malformed URI: URI is not an absolute URI and no base supplied: " + relative;
}
return this;
},
/**
* Resolves a relative URI relative to this URI
* @param {String} relative
* @returns jQuery.uri
*/
resolve: function (relative) {
return $.uri(relative, this);
},
/**
* Creates a relative URI giving the path from this URI to the absolute URI passed as a parameter
* @param {String|jQuery.uri} absolute
* @returns String
*/
relative: function (absolute) {
var aPath, bPath, i = 0, j, resultPath = [], result = '';
if (typeof absolute === 'string') {
absolute = $.uri(absolute, {});
}
if (absolute.scheme !== this.scheme ||
absolute.authority !== this.authority) {
return absolute.toString();
}
if (absolute.path !== this.path) {
aPath = absolute.path.split('/');
bPath = this.path.split('/');
if (aPath[1] !== bPath[1]) {
result = absolute.path;
} else {
while (aPath[i] === bPath[i]) {
i += 1;
}
j = i;
for (; i < bPath.length - 1; i += 1) {
resultPath.push('..');
}
for (; j < aPath.length; j += 1) {
resultPath.push(aPath[j]);
}
result = resultPath.join('/');
}
result = absolute.query === undefined ? result : result + '?' + absolute.query;
result = absolute.fragment === undefined ? result : result + '#' + absolute.fragment;
return result;
}
if (absolute.query !== undefined && absolute.query !== this.query) {
return '?' + absolute.query + (absolute.fragment === undefined ? '' : '#' + absolute.fragment);
}
if (absolute.fragment !== undefined && absolute.fragment !== this.fragment) {
return '#' + absolute.fragment;
}
return '';
},
/**
* Returns the URI as an absolute string
* @returns String
*/
toString: function () {
var result = '';
if (this._string) {
return this._string;
} else {
result = this.scheme === undefined ? result : (result + this.scheme + ':');
result = this.authority === undefined ? result : (result + '//' + this.authority);
result = result + this.path;
result = this.query === undefined ? result : (result + '?' + this.query);
result = this.fragment === undefined ? result : (result + '#' + this.fragment);
this._string = result;
return result;
}
}
};
$.uri.fn.init.prototype = $.uri.fn;
/**
* Creates a {@link jQuery.uri} from a known-to-be-absolute URI
* @param {String}
* @returns {jQuery.uri}
*/
$.uri.absolute = function (uri) {
return $.uri(uri, {});
};
/**
* Creates a {@link jQuery.uri} from a relative URI and an optional base URI
* @returns {jQuery.uri}
* @see jQuery.uri
*/
$.uri.resolve = function (relative, base) {
return $.uri(relative, base);
};
/**
* Creates a string giving the relative path from a base URI to an absolute URI
* @param {String} absolute
* @param {String} base
* @returns {String}
*/
$.uri.relative = function (absolute, base) {
return $.uri(base, {}).relative(absolute);
};
/**
* Returns the base URI of the page
* @returns {jQuery.uri}
*/
$.uri.base = function () {
return $(document).base();
};
/**
* Returns the base URI in scope for the first selected element
* @methodOf jQuery#
* @name jQuery#base
* @returns {jQuery.uri}
* @example baseURI = $('img').base();
*/
$.fn.base = function () {
var base = $(this).parents().andSelf().find('base').attr('href'),
doc = $(this)[0].ownerDocument || document,
docURI = $.uri.absolute(doc.location === null ? document.location.href : doc.location.href);
return base === undefined ? docURI : $.uri(base, docURI);
};
})(jQuery);