1 | 'use strict';
|
2 |
|
3 | var required = require('requires-port')
|
4 | , qs = require('querystringify')
|
5 | , controlOrWhitespace = /^[\x00-\x20\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/
|
6 | , CRHTLF = /[\n\r\t]/g
|
7 | , slashes = /^[A-Za-z][A-Za-z0-9+-.]*:\/\//
|
8 | , port = /:\d+$/
|
9 | , protocolre = /^([a-z][a-z0-9.+-]*:)?(\/\/)?([\\/]+)?([\S\s]*)/i
|
10 | , windowsDriveLetter = /^[a-zA-Z]:/;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function trimLeft(str) {
|
21 | return (str ? str : '').toString().replace(controlOrWhitespace, '');
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | var rules = [
|
37 | ['#', 'hash'],
|
38 | ['?', 'query'],
|
39 | function sanitize(address, url) {
|
40 | return isSpecial(url.protocol) ? address.replace(/\\/g, '/') : address;
|
41 | },
|
42 | ['/', 'pathname'],
|
43 | ['@', 'auth', 1],
|
44 | [NaN, 'host', undefined, 1, 1],
|
45 | [/:(\d*)$/, 'port', undefined, 1],
|
46 | [NaN, 'hostname', undefined, 1, 1]
|
47 | ];
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | var ignore = { hash: 1, query: 1 };
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function lolcation(loc) {
|
72 | var globalVar;
|
73 |
|
74 | if (typeof window !== 'undefined') globalVar = window;
|
75 | else if (typeof global !== 'undefined') globalVar = global;
|
76 | else if (typeof self !== 'undefined') globalVar = self;
|
77 | else globalVar = {};
|
78 |
|
79 | var location = globalVar.location || {};
|
80 | loc = loc || location;
|
81 |
|
82 | var finaldestination = {}
|
83 | , type = typeof loc
|
84 | , key;
|
85 |
|
86 | if ('blob:' === loc.protocol) {
|
87 | finaldestination = new Url(unescape(loc.pathname), {});
|
88 | } else if ('string' === type) {
|
89 | finaldestination = new Url(loc, {});
|
90 | for (key in ignore) delete finaldestination[key];
|
91 | } else if ('object' === type) {
|
92 | for (key in loc) {
|
93 | if (key in ignore) continue;
|
94 | finaldestination[key] = loc[key];
|
95 | }
|
96 |
|
97 | if (finaldestination.slashes === undefined) {
|
98 | finaldestination.slashes = slashes.test(loc.href);
|
99 | }
|
100 | }
|
101 |
|
102 | return finaldestination;
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function isSpecial(scheme) {
|
113 | return (
|
114 | scheme === 'file:' ||
|
115 | scheme === 'ftp:' ||
|
116 | scheme === 'http:' ||
|
117 | scheme === 'https:' ||
|
118 | scheme === 'ws:' ||
|
119 | scheme === 'wss:'
|
120 | );
|
121 | }
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function extractProtocol(address, location) {
|
140 | address = trimLeft(address);
|
141 | address = address.replace(CRHTLF, '');
|
142 | location = location || {};
|
143 |
|
144 | var match = protocolre.exec(address);
|
145 | var protocol = match[1] ? match[1].toLowerCase() : '';
|
146 | var forwardSlashes = !!match[2];
|
147 | var otherSlashes = !!match[3];
|
148 | var slashesCount = 0;
|
149 | var rest;
|
150 |
|
151 | if (forwardSlashes) {
|
152 | if (otherSlashes) {
|
153 | rest = match[2] + match[3] + match[4];
|
154 | slashesCount = match[2].length + match[3].length;
|
155 | } else {
|
156 | rest = match[2] + match[4];
|
157 | slashesCount = match[2].length;
|
158 | }
|
159 | } else {
|
160 | if (otherSlashes) {
|
161 | rest = match[3] + match[4];
|
162 | slashesCount = match[3].length;
|
163 | } else {
|
164 | rest = match[4]
|
165 | }
|
166 | }
|
167 |
|
168 | if (protocol === 'file:') {
|
169 | if (slashesCount >= 2) {
|
170 | rest = rest.slice(2);
|
171 | }
|
172 | } else if (isSpecial(protocol)) {
|
173 | rest = match[4];
|
174 | } else if (protocol) {
|
175 | if (forwardSlashes) {
|
176 | rest = rest.slice(2);
|
177 | }
|
178 | } else if (slashesCount >= 2 && isSpecial(location.protocol)) {
|
179 | rest = match[4];
|
180 | }
|
181 |
|
182 | return {
|
183 | protocol: protocol,
|
184 | slashes: forwardSlashes || isSpecial(protocol),
|
185 | slashesCount: slashesCount,
|
186 | rest: rest
|
187 | };
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function resolve(relative, base) {
|
199 | if (relative === '') return base;
|
200 |
|
201 | var path = (base || '/').split('/').slice(0, -1).concat(relative.split('/'))
|
202 | , i = path.length
|
203 | , last = path[i - 1]
|
204 | , unshift = false
|
205 | , up = 0;
|
206 |
|
207 | while (i--) {
|
208 | if (path[i] === '.') {
|
209 | path.splice(i, 1);
|
210 | } else if (path[i] === '..') {
|
211 | path.splice(i, 1);
|
212 | up++;
|
213 | } else if (up) {
|
214 | if (i === 0) unshift = true;
|
215 | path.splice(i, 1);
|
216 | up--;
|
217 | }
|
218 | }
|
219 |
|
220 | if (unshift) path.unshift('');
|
221 | if (last === '.' || last === '..') path.push('');
|
222 |
|
223 | return path.join('/');
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | function Url(address, location, parser) {
|
241 | address = trimLeft(address);
|
242 | address = address.replace(CRHTLF, '');
|
243 |
|
244 | if (!(this instanceof Url)) {
|
245 | return new Url(address, location, parser);
|
246 | }
|
247 |
|
248 | var relative, extracted, parse, instruction, index, key
|
249 | , instructions = rules.slice()
|
250 | , type = typeof location
|
251 | , url = this
|
252 | , i = 0;
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | if ('object' !== type && 'string' !== type) {
|
266 | parser = location;
|
267 | location = null;
|
268 | }
|
269 |
|
270 | if (parser && 'function' !== typeof parser) parser = qs.parse;
|
271 |
|
272 | location = lolcation(location);
|
273 |
|
274 |
|
275 |
|
276 |
|
277 | extracted = extractProtocol(address || '', location);
|
278 | relative = !extracted.protocol && !extracted.slashes;
|
279 | url.slashes = extracted.slashes || relative && location.slashes;
|
280 | url.protocol = extracted.protocol || location.protocol || '';
|
281 | address = extracted.rest;
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | if (
|
288 | extracted.protocol === 'file:' && (
|
289 | extracted.slashesCount !== 2 || windowsDriveLetter.test(address)) ||
|
290 | (!extracted.slashes &&
|
291 | (extracted.protocol ||
|
292 | extracted.slashesCount < 2 ||
|
293 | !isSpecial(url.protocol)))
|
294 | ) {
|
295 | instructions[3] = [/(.*)/, 'pathname'];
|
296 | }
|
297 |
|
298 | for (; i < instructions.length; i++) {
|
299 | instruction = instructions[i];
|
300 |
|
301 | if (typeof instruction === 'function') {
|
302 | address = instruction(address, url);
|
303 | continue;
|
304 | }
|
305 |
|
306 | parse = instruction[0];
|
307 | key = instruction[1];
|
308 |
|
309 | if (parse !== parse) {
|
310 | url[key] = address;
|
311 | } else if ('string' === typeof parse) {
|
312 | index = parse === '@'
|
313 | ? address.lastIndexOf(parse)
|
314 | : address.indexOf(parse);
|
315 |
|
316 | if (~index) {
|
317 | if ('number' === typeof instruction[2]) {
|
318 | url[key] = address.slice(0, index);
|
319 | address = address.slice(index + instruction[2]);
|
320 | } else {
|
321 | url[key] = address.slice(index);
|
322 | address = address.slice(0, index);
|
323 | }
|
324 | }
|
325 | } else if ((index = parse.exec(address))) {
|
326 | url[key] = index[1];
|
327 | address = address.slice(0, index.index);
|
328 | }
|
329 |
|
330 | url[key] = url[key] || (
|
331 | relative && instruction[3] ? location[key] || '' : ''
|
332 | );
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | if (instruction[4]) url[key] = url[key].toLowerCase();
|
339 | }
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 | if (parser) url.query = parser(url.query);
|
347 |
|
348 |
|
349 |
|
350 |
|
351 | if (
|
352 | relative
|
353 | && location.slashes
|
354 | && url.pathname.charAt(0) !== '/'
|
355 | && (url.pathname !== '' || location.pathname !== '')
|
356 | ) {
|
357 | url.pathname = resolve(url.pathname, location.pathname);
|
358 | }
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | if (url.pathname.charAt(0) !== '/' && isSpecial(url.protocol)) {
|
365 | url.pathname = '/' + url.pathname;
|
366 | }
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | if (!required(url.port, url.protocol)) {
|
374 | url.host = url.hostname;
|
375 | url.port = '';
|
376 | }
|
377 |
|
378 |
|
379 |
|
380 |
|
381 | url.username = url.password = '';
|
382 |
|
383 | if (url.auth) {
|
384 | index = url.auth.indexOf(':');
|
385 |
|
386 | if (~index) {
|
387 | url.username = url.auth.slice(0, index);
|
388 | url.username = encodeURIComponent(decodeURIComponent(url.username));
|
389 |
|
390 | url.password = url.auth.slice(index + 1);
|
391 | url.password = encodeURIComponent(decodeURIComponent(url.password))
|
392 | } else {
|
393 | url.username = encodeURIComponent(decodeURIComponent(url.auth));
|
394 | }
|
395 |
|
396 | url.auth = url.password ? url.username +':'+ url.password : url.username;
|
397 | }
|
398 |
|
399 | url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
|
400 | ? url.protocol +'//'+ url.host
|
401 | : 'null';
|
402 |
|
403 |
|
404 |
|
405 |
|
406 | url.href = url.toString();
|
407 | }
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | function set(part, value, fn) {
|
423 | var url = this;
|
424 |
|
425 | switch (part) {
|
426 | case 'query':
|
427 | if ('string' === typeof value && value.length) {
|
428 | value = (fn || qs.parse)(value);
|
429 | }
|
430 |
|
431 | url[part] = value;
|
432 | break;
|
433 |
|
434 | case 'port':
|
435 | url[part] = value;
|
436 |
|
437 | if (!required(value, url.protocol)) {
|
438 | url.host = url.hostname;
|
439 | url[part] = '';
|
440 | } else if (value) {
|
441 | url.host = url.hostname +':'+ value;
|
442 | }
|
443 |
|
444 | break;
|
445 |
|
446 | case 'hostname':
|
447 | url[part] = value;
|
448 |
|
449 | if (url.port) value += ':'+ url.port;
|
450 | url.host = value;
|
451 | break;
|
452 |
|
453 | case 'host':
|
454 | url[part] = value;
|
455 |
|
456 | if (port.test(value)) {
|
457 | value = value.split(':');
|
458 | url.port = value.pop();
|
459 | url.hostname = value.join(':');
|
460 | } else {
|
461 | url.hostname = value;
|
462 | url.port = '';
|
463 | }
|
464 |
|
465 | break;
|
466 |
|
467 | case 'protocol':
|
468 | url.protocol = value.toLowerCase();
|
469 | url.slashes = !fn;
|
470 | break;
|
471 |
|
472 | case 'pathname':
|
473 | case 'hash':
|
474 | if (value) {
|
475 | var char = part === 'pathname' ? '/' : '#';
|
476 | url[part] = value.charAt(0) !== char ? char + value : value;
|
477 | } else {
|
478 | url[part] = value;
|
479 | }
|
480 | break;
|
481 |
|
482 | case 'username':
|
483 | case 'password':
|
484 | url[part] = encodeURIComponent(value);
|
485 | break;
|
486 |
|
487 | case 'auth':
|
488 | var index = value.indexOf(':');
|
489 |
|
490 | if (~index) {
|
491 | url.username = value.slice(0, index);
|
492 | url.username = encodeURIComponent(decodeURIComponent(url.username));
|
493 |
|
494 | url.password = value.slice(index + 1);
|
495 | url.password = encodeURIComponent(decodeURIComponent(url.password));
|
496 | } else {
|
497 | url.username = encodeURIComponent(decodeURIComponent(value));
|
498 | }
|
499 | }
|
500 |
|
501 | for (var i = 0; i < rules.length; i++) {
|
502 | var ins = rules[i];
|
503 |
|
504 | if (ins[4]) url[ins[1]] = url[ins[1]].toLowerCase();
|
505 | }
|
506 |
|
507 | url.auth = url.password ? url.username +':'+ url.password : url.username;
|
508 |
|
509 | url.origin = url.protocol !== 'file:' && isSpecial(url.protocol) && url.host
|
510 | ? url.protocol +'//'+ url.host
|
511 | : 'null';
|
512 |
|
513 | url.href = url.toString();
|
514 |
|
515 | return url;
|
516 | }
|
517 |
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 | function toString(stringify) {
|
526 | if (!stringify || 'function' !== typeof stringify) stringify = qs.stringify;
|
527 |
|
528 | var query
|
529 | , url = this
|
530 | , host = url.host
|
531 | , protocol = url.protocol;
|
532 |
|
533 | if (protocol && protocol.charAt(protocol.length - 1) !== ':') protocol += ':';
|
534 |
|
535 | var result =
|
536 | protocol +
|
537 | ((url.protocol && url.slashes) || isSpecial(url.protocol) ? '//' : '');
|
538 |
|
539 | if (url.username) {
|
540 | result += url.username;
|
541 | if (url.password) result += ':'+ url.password;
|
542 | result += '@';
|
543 | } else if (url.password) {
|
544 | result += ':'+ url.password;
|
545 | result += '@';
|
546 | } else if (
|
547 | url.protocol !== 'file:' &&
|
548 | isSpecial(url.protocol) &&
|
549 | !host &&
|
550 | url.pathname !== '/'
|
551 | ) {
|
552 |
|
553 |
|
554 |
|
555 |
|
556 | result += '@';
|
557 | }
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 | if (host[host.length - 1] === ':' || (port.test(url.hostname) && !url.port)) {
|
565 | host += ':';
|
566 | }
|
567 |
|
568 | result += host + url.pathname;
|
569 |
|
570 | query = 'object' === typeof url.query ? stringify(url.query) : url.query;
|
571 | if (query) result += '?' !== query.charAt(0) ? '?'+ query : query;
|
572 |
|
573 | if (url.hash) result += url.hash;
|
574 |
|
575 | return result;
|
576 | }
|
577 |
|
578 | Url.prototype = { set: set, toString: toString };
|
579 |
|
580 | //
|
581 | // Expose the URL parser and some additional properties that might be useful for
|
582 | // others or testing.
|
583 | //
|
584 | Url.extractProtocol = extractProtocol;
|
585 | Url.location = lolcation;
|
586 | Url.trimLeft = trimLeft;
|
587 | Url.qs = qs;
|
588 |
|
589 | module.exports = Url;
|