forked from ericf/urljs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.js
666 lines (551 loc) · 19.6 KB
/
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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*!
* URL.js
*
* Copyright 2011 Eric Ferraiuolo
* https://github.com/ericf/urljs
*/
/**
* URL constructor and utility.
* Provides support for validating whether something is a URL,
* formats and cleans up URL-like inputs into something nice and pretty,
* ability to resolve one URL against another and returned the formatted result,
* and is a convenient API for working with URL Objects and the various parts of URLs.
*
* @constructor URL
* @param {String | URL} url - the URL String to parse or URL instance to copy
* @return {URL} url - instance of a URL all nice and parsed
*/
var URL = function () {
var u = this;
if ( ! (u && u.hasOwnProperty && (u instanceof URL))) {
u = new URL();
}
return u._init.apply(u, arguments);
};
(function(){
var ABSOLUTE = 'absolute',
RELATIVE = 'relative',
HTTP = 'http',
HTTPS = 'https',
COLON = ':',
SLASH_SLASH = '//',
AT = '@',
DOT = '.',
SLASH = '/',
DOT_DOT = '..',
DOT_DOT_SLASH = '../',
QUESTION = '?',
EQUALS = '=',
AMP = '&',
HASH = '#',
EMPTY_STRING = '',
TYPE = 'type',
SCHEME = 'scheme',
USER_INFO = 'userInfo',
HOST = 'host',
PORT = 'port',
PATH = 'path',
QUERY = 'query',
FRAGMENT = 'fragment',
URL_TYPE_REGEX = /^(?:(https?:\/\/|\/\/)|(\/|\?|#)|[^;:@=\.\s])/i,
URL_ABSOLUTE_REGEX = /^(?:(https?):\/\/|\/\/)(?:([^:@\s]+:?[^:@\s]+?)@)?((?:[^;:@=\/\?\.\s]+\.)+[A-Za-z0-9\-]{2,})(?::(\d+))?(?=\/|\?|#|$)([^\?#]+)?(?:\?([^#]+))?(?:#(.+))?/i,
URL_RELATIVE_REGEX = /^([^\?#]+)?(?:\?([^#]+))?(?:#(.+))?/i,
OBJECT = 'object',
STRING = 'string',
TRIM_REGEX = /^\s+|\s+$/g,
trim, isObject, isString;
// *** Utilities *** //
trim = String.prototype.trim ? function (s) {
return ( s && s.trim ? s.trim() : s );
} : function (s) {
try {
return s.replace(TRIM_REGEX, EMPTY_STRING);
} catch (e) { return s; }
};
isObject = function (o) {
return ( o && typeof o === OBJECT );
};
isString = function (o) {
return typeof o === STRING;
};
// *** Static *** //
/**
*
*/
URL.ABSOLUTE = ABSOLUTE;
/**
*
*/
URL.RELATIVE = RELATIVE;
/**
*
*/
URL.normalize = function (url) {
return new URL(url).toString();
};
/**
* Returns a resolved URL String using the baseUrl to resolve the url against.
* This attempts to resolve URLs like a browser would on a web page.
*
* @static
* @method resolve
* @param {String | URL} baseUrl - the URL String, or URL instance as the resolving base
* @param {String | URL} url - the URL String, or URL instance to resolve
* @return {String} resolvedUrl - a resolved URL String
*/
URL.resolve = function (baseUrl, url) {
return new URL(baseUrl).resolve(url).toString();
};
// *** Prototype *** //
URL.prototype = {
// *** Lifecycle Methods *** //
/**
* Initializes a new URL instance, or re-initializes an existing one.
* The URL constructor delegates to this method to do the initializing,
* and the mutator instance methods call this to re-initialize when something changes.
*
* @protected
* @method _init
* @param {String | URL} url - the URL String, or URL instance
* @return {URL} url - instance of a URL all nice and parsed/re-parsed
*/
_init : function (url) {
this.constructor = URL;
url = isString(url) ? url : url instanceof URL ? url.toString() : null;
this._original = url;
this._url = {};
this._isValid = this._parse(url);
return this;
},
// *** Object Methods *** //
/**
* Returns the formatted URL String.
* Overridden Object toString method to do something useful.
*
* @public
* @method toString
* @return {String} url - formatted URL string
*/
toString : function () {
var url = this._url,
urlParts = [],
type = url[TYPE],
scheme = url[SCHEME],
path = url[PATH],
query = url[QUERY],
fragment = url[FRAGMENT];
if (type === ABSOLUTE) {
urlParts.push(
scheme ? (scheme + COLON + SLASH_SLASH) : SLASH_SLASH,
this.authority()
);
if (path && path.indexOf(SLASH) !== 0) { // this should maybe go in _set
path = SLASH + path;
}
}
urlParts.push(
path,
query ? (QUESTION + this.queryString()) : EMPTY_STRING,
fragment ? (HASH + fragment) : EMPTY_STRING
);
return urlParts.join(EMPTY_STRING);
},
// *** Accessor/Mutator Methods *** //
original : function () {
return this._original;
},
/**
* Whether parsing from initialization or re-initialization produced something valid.
*
* @public
* @method isValid
* @return {Boolean} valid - whether the URL is valid
*/
isValid : function () {
return this._isValid;
},
/**
* URL is absolute if it has a scheme or is scheme-relative (//).
*
* @public
* @method isAbsolute
* @return {Boolean} absolute - whether the URL is absolute
*/
isAbsolute : function () {
return this._url[TYPE] === ABSOLUTE;
},
/**
* URL is relative if it host or path relative, i.e. doesn't contain a host.
*
* @public
* @method isRelative
* @return {Boolean} relative - whether the URL is relative
*/
isRelative : function () {
return this._url[TYPE] === RELATIVE;
},
/**
* URL is host relative if it's relative and the path begins with '/'.
*
* @public
* @method isHostRelative
* @return {Boolean} hostRelative - whether the URL is host-relative
*/
isHostRelative : function () {
var path = this._url[PATH];
return ( this.isRelative() && path && path.indexOf(SLASH) === 0 );
},
/**
* Returns the type of the URL, either: URL.ABSOLUTE or URL.RELATIVE.
*
* @public
* @method type
* @return {String} type - the type of the URL: URL.ABSOLUTE or URL.RELATIVE
*/
type : function () {
return this._url[TYPE];
},
/**
* Returns or sets the scheme of the URL.
* If URL is determined to be absolute (i.e. contains a host) and no scheme is provided,
* the scheme will default to http.
*
* @public
* @method scheme
* @param {String} scheme - Optional scheme to set on the URL
* @return {String | URL} the URL scheme or the URL instance
*/
scheme : function (scheme) {
return ( arguments.length ? this._set(SCHEME, scheme) : this._url[SCHEME] );
},
/**
* Returns or set the user info of the URL.
* The user info can optionally contain a password and is only valid for absolute URLs.
*
* @public
* @method userInfo
* @param {String} userInfo - Optional userInfo to set on the URL
* @return {String | URL} the URL userInfo or the URL instance
*/
userInfo : function (userInfo) {
return ( arguments.length ? this._set(USER_INFO, userInfo) : this._url[USER_INFO] );
},
/**
* Returns or sets the host of the URL.
* The host name, if set, must be something valid otherwise the URL will become invalid.
*
* @public
* @method host
* @param {String} host - Optional host to set on the URL
* @return {String | URL} the URL host or the URL instance
*/
host : function (host) {
return ( arguments.length ? this._set(HOST, host) : this._url[HOST] );
},
/**
* Returns the URL's domain, where the domain is the TLD and SLD of the host.
* e.g. foo.example.com -> example.com
*
* @public
* @method domain
* @return {String} domain - the URL domain
*/
domain : function () {
var host = this._url[HOST];
return ( host ? host.split(DOT).slice(-2).join(DOT) : undefined );
},
/**
* Returns or sets the port of the URL.
*
* @public
* @method port
* @param {Number} port - Optional port to set on the URL
* @return {Number | URL} the URL port or the URL instance
*/
port : function (port) {
return ( arguments.length ? this._set(PORT, port) : this._url[PORT] );
},
/**
* Returns the URL's authority which is the userInfo, host, and port combined.
* This only makes sense for absolute URLs
*
* @public
* @method authority
* @return {String} authority - the URL's authority (userInfo, host, and port)
*/
authority : function () {
var url = this._url,
userInfo = url[USER_INFO],
host = url[HOST],
port = url[PORT];
return [
userInfo ? (userInfo + AT) : EMPTY_STRING,
host,
port ? (COLON + port) : EMPTY_STRING,
].join(EMPTY_STRING);
},
/**
* Returns or sets the path of the URL.
*
* @public
* @method path
* @param {String} path - Optional path to set on the URL
* @return {String | URL} the URL path or the URL instance
*/
path : function (path) {
return ( arguments.length ? this._set(PATH, path) : this._url[PATH] );
},
/**
* Returns or sets the query of the URL.
* This takes or returns the parsed query as an Array of Arrays.
*
* @public
* @method query
* @param {Array} query - Optional query to set on the URL
* @return {Array | URL} the URL query or the URL instance
*/
query : function (query) {
return ( arguments.length ? this._set(QUERY, query) : this._url[QUERY] );
},
/**
* Returns or sets the query of the URL.
* This takes or returns the query as a String; doesn't include the '?'
*
* @public
* @method queryString
* @param {String} queryString - Optional queryString to set on the URL
* @return {String | URL} the URL queryString or the URL instance
*/
queryString : function (queryString) {
// parse and set queryString
if (arguments.length) {
return this._set(QUERY, this._parseQuery(queryString));
}
queryString = EMPTY_STRING;
var query = this._url[QUERY],
i, len;
if (query) {
for (i = 0, len = query.length; i < len; i++) {
queryString += query[i].join(EQUALS);
if (i < len - 1) {
queryString += AMP;
}
}
}
return queryString;
},
/**
* Returns or sets the fragment on the URL.
* The fragment does not contain the '#'.
*
* @public
* @method fragment
* @param {String} fragment - Optional fragment to set on the URL
* @return {String | URL} the URL fragment or the URL instance
*/
fragment : function (fragment) {
return ( arguments.length ? this._set(FRAGMENT, fragment) : this._url[FRAGMENT] );
},
/**
* Returns a new, resolved URL instance using this as the baseUrl.
* The URL passed in will be resolved against the baseUrl.
*
* @public
* @method resolve
* @param {String | URL} url - the URL String, or URL instance to resolve
* @return {URL} url - a resolved URL instance
*/
resolve : function (url) {
url = (url instanceof URL) ? url : new URL(url);
var resolved, path;
if ( ! (this.isValid() && url.isValid())) { return this; } // not sure what to do???
// the easy way
if (url.isAbsolute()) {
return ( this.isAbsolute() ? url.scheme() ? url : new URL(url).scheme(this.scheme()) : url );
}
// the hard way
resolved = new URL(this.isAbsolute() ? this : null);
if (url.path()) {
if (url.isHostRelative() || ! this.path()) {
path = url.path();
} else {
path = this.path().substring(0, this.path().lastIndexOf(SLASH) + 1) + url.path();
}
resolved.path(this._normalizePath(path)).query(url.query()).fragment(url.fragment());
} else if (url.query()) {
resolved.query(url.query()).fragment(url.fragment());
} else if (url.fragment()) {
resolved.fragment(url.fragment());
}
return resolved;
},
/**
* Returns a new, reduced relative URL instance using this as the baseUrl.
* The URL passed in will be compared to the baseUrl with the goal of
* returning a reduced-down URL to one that’s relative to the base (this).
* This method is basically the opposite of resolve.
*
* @public
* @method reduce
* @param {String | URL} url - the URL String, or URL instance to resolve
* @return {URL} url - the reduced URL instance
*/
reduce : function (url) {
url = (url instanceof URL) ? url : new URL(url);
var reduced = this.resolve(url);
if (this.isAbsolute() && reduced.isAbsolute()) {
if (reduced.scheme() === this.scheme() && reduced.authority() === this.authority()) {
reduced.scheme(null).userInfo(null).host(null).port(null);
}
}
return reduced;
},
// *** Private Methods *** //
/**
* Parses a URL into usable parts.
* Reasonable defaults are applied to parts of the URL which weren't present in the input,
* e.g. 'http://example.com' -> { type: 'absolute', scheme: 'http', host: 'example.com', path: '/' }
* If nothing or a falsy value is returned, the URL wasn't something valid.
*
* @private
* @method _parse
* @param {String} url - the URL string to parse
* @param {String} type - Optional type to seed parsing: URL.ABSOLUTE or URL.RELATIVE
* @return {Boolean} parsed - whether or not the URL string was parsed
*/
_parse : function (url, type) {
// make sure we have a good string
url = trim(url);
if ( ! (isString(url) && url.length > 0)) {
return false;
}
var urlParts, parsed;
// figure out type, absolute or relative, or quit
if ( ! type) {
type = url.match(URL_TYPE_REGEX);
type = type ? type[1] ? ABSOLUTE : type[2] ? RELATIVE : null : null;
}
switch (type) {
case ABSOLUTE:
urlParts = url.match(URL_ABSOLUTE_REGEX);
if (urlParts) {
parsed = {};
parsed[TYPE] = ABSOLUTE;
parsed[SCHEME] = urlParts[1] ? urlParts[1].toLowerCase() : undefined;
parsed[USER_INFO] = urlParts[2];
parsed[HOST] = urlParts[3].toLowerCase();
parsed[PORT] = urlParts[4] ? parseInt(urlParts[4], 10) : undefined;
parsed[PATH] = urlParts[5] || SLASH;
parsed[QUERY] = this._parseQuery(urlParts[6]);
parsed[FRAGMENT] = urlParts[7];
}
break;
case RELATIVE:
urlParts = url.match(URL_RELATIVE_REGEX);
if (urlParts) {
parsed = {};
parsed[TYPE] = RELATIVE;
parsed[PATH] = urlParts[1];
parsed[QUERY] = this._parseQuery(urlParts[2]);
parsed[FRAGMENT] = urlParts[3];
}
break;
// try to parse as absolute, if that fails then as relative
default:
return ( this._parse(url, ABSOLUTE) || this._parse(url, RELATIVE) );
break;
}
if (parsed) {
this._url = parsed;
return true;
} else {
return false;
}
},
/**
* Helper to parse a URL query string into an array of arrays.
* Order of the query paramerters is maintained, an example structure would be:
* queryString: 'foo=bar&baz' -> [['foo', 'bar'], ['baz']]
*
* @private
* @method _parseQuery
* @param {String} queryString - the query string to parse, should not include '?'
* @return {Array} parsedQuery - array of arrays representing the query parameters and values
*/
_parseQuery : function (queryString) {
if ( ! isString(queryString)) { return; }
queryString = trim(queryString);
var query = [],
queryParts = queryString.split(AMP),
queryPart, i, len;
for (i = 0, len = queryParts.length; i < len; i++) {
if (queryParts[i]) {
queryPart = queryParts[i].split(EQUALS);
query.push(queryPart[1] ? queryPart : [queryPart[0]]);
}
}
return query;
},
/**
* Helper for mutators to set a new URL-part value.
* After the URL-part is updated, the URL will be toString'd and re-parsed.
* This is a brute, but will make sure the URL stays in sync and is re-validated.
*
* @private
* @method _set
* @param {String} urlPart - the _url Object member String name
* @param {Object} val - the new value for the URL-part, mixed type
* @return {URL} this - returns this URL instance, chainable
*/
_set : function (urlPart, val) {
this._url[urlPart] = val;
if (val && (
urlPart === SCHEME ||
urlPart === USER_INFO ||
urlPart === HOST ||
urlPart === PORT )){
this._url[TYPE] = ABSOLUTE; // temp, set this to help clue parsing
}
if ( ! val && urlPart === HOST) {
this._url[TYPE] = RELATIVE; // temp, no host means relative
}
this._isValid = this._parse(this.toString());
return this;
},
/**
* Returns a normalized path String, by removing ../'s.
*
* @private
* @method _normalizePath
* @param {String} path — the path String to normalize
* @return {String} normalizedPath — the normalized path String
*/
_normalizePath : function (path) {
var pathParts, pathPart, pathStack, normalizedPath, i, len;
if (path.indexOf(DOT_DOT_SLASH) > -1) {
pathParts = path.split(SLASH);
pathStack = [];
for ( i = 0, len = pathParts.length; i < len; i++ ) {
pathPart = pathParts[i];
if (pathPart === DOT_DOT) {
pathStack.pop();
} else if (pathPart) {
pathStack.push(pathPart);
}
}
normalizedPath = pathStack.join(SLASH);
// prepend slash if needed
if (path[0] === SLASH) {
normalizedPath = SLASH + normalizedPath;
}
// append slash if needed
if (path[path.length - 1] === SLASH && normalizedPath.length > 1) {
normalizedPath += SLASH;
}
} else {
normalizedPath = path;
}
return normalizedPath;
}
};
}());