graphiql_rails 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ (function(global) {
2
+ function GraphiqlRails() {
3
+ _.bindAll(this);
4
+ var queryParams = URI.parseQuery(window.location.search);
5
+ this.query = queryParams.query;
6
+ this.variables = queryParams.variables;
7
+ }
8
+
9
+ GraphiqlRails.prototype = {
10
+ onEditQuery: function(newQuery) {
11
+ this.query = newQuery;
12
+ this.updateURL();
13
+ },
14
+
15
+ onEditVariables: function(newVariables) {
16
+ this.variables = newVariables;
17
+ this.updateURL();
18
+ },
19
+
20
+ updateURL: function() {
21
+ var newState = URI.buildQuery({
22
+ query: this.query,
23
+ variables: this.variables
24
+ });
25
+ history.replaceState(null, null, '?' + newState);
26
+ }
27
+ };
28
+
29
+ global.GraphiqlRails = GraphiqlRails;
30
+ })(this);
@@ -0,0 +1,2161 @@
1
+ /*!
2
+ * URI.js - Mutating URLs
3
+ *
4
+ * Version: 1.17.0
5
+ *
6
+ * Author: Rodney Rehm
7
+ * Web: http://medialize.github.io/URI.js/
8
+ *
9
+ * Licensed under
10
+ * MIT License http://www.opensource.org/licenses/mit-license
11
+ * GPL v3 http://opensource.org/licenses/GPL-3.0
12
+ *
13
+ */
14
+ (function (root, factory) {
15
+ 'use strict';
16
+ // https://github.com/umdjs/umd/blob/master/returnExports.js
17
+ if (typeof exports === 'object') {
18
+ // Node
19
+ module.exports = factory(require('./punycode'), require('./IPv6'), require('./SecondLevelDomains'));
20
+ } else if (typeof define === 'function' && define.amd) {
21
+ // AMD. Register as an anonymous module.
22
+ define(['./punycode', './IPv6', './SecondLevelDomains'], factory);
23
+ } else {
24
+ // Browser globals (root is window)
25
+ root.URI = factory(root.punycode, root.IPv6, root.SecondLevelDomains, root);
26
+ }
27
+ }(this, function (punycode, IPv6, SLD, root) {
28
+ 'use strict';
29
+ /*global location, escape, unescape */
30
+ // FIXME: v2.0.0 renamce non-camelCase properties to uppercase
31
+ /*jshint camelcase: false */
32
+
33
+ // save current URI variable, if any
34
+ var _URI = root && root.URI;
35
+
36
+ function URI(url, base) {
37
+ var _urlSupplied = arguments.length >= 1;
38
+ var _baseSupplied = arguments.length >= 2;
39
+
40
+ // Allow instantiation without the 'new' keyword
41
+ if (!(this instanceof URI)) {
42
+ if (_urlSupplied) {
43
+ if (_baseSupplied) {
44
+ return new URI(url, base);
45
+ }
46
+
47
+ return new URI(url);
48
+ }
49
+
50
+ return new URI();
51
+ }
52
+
53
+ if (url === undefined) {
54
+ if (_urlSupplied) {
55
+ throw new TypeError('undefined is not a valid argument for URI');
56
+ }
57
+
58
+ if (typeof location !== 'undefined') {
59
+ url = location.href + '';
60
+ } else {
61
+ url = '';
62
+ }
63
+ }
64
+
65
+ this.href(url);
66
+
67
+ // resolve to base according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#constructor
68
+ if (base !== undefined) {
69
+ return this.absoluteTo(base);
70
+ }
71
+
72
+ return this;
73
+ }
74
+
75
+ URI.version = '1.17.0';
76
+
77
+ var p = URI.prototype;
78
+ var hasOwn = Object.prototype.hasOwnProperty;
79
+
80
+ function escapeRegEx(string) {
81
+ // https://github.com/medialize/URI.js/commit/85ac21783c11f8ccab06106dba9735a31a86924d#commitcomment-821963
82
+ return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
83
+ }
84
+
85
+ function getType(value) {
86
+ // IE8 doesn't return [Object Undefined] but [Object Object] for undefined value
87
+ if (value === undefined) {
88
+ return 'Undefined';
89
+ }
90
+
91
+ return String(Object.prototype.toString.call(value)).slice(8, -1);
92
+ }
93
+
94
+ function isArray(obj) {
95
+ return getType(obj) === 'Array';
96
+ }
97
+
98
+ function filterArrayValues(data, value) {
99
+ var lookup = {};
100
+ var i, length;
101
+
102
+ if (getType(value) === 'RegExp') {
103
+ lookup = null;
104
+ } else if (isArray(value)) {
105
+ for (i = 0, length = value.length; i < length; i++) {
106
+ lookup[value[i]] = true;
107
+ }
108
+ } else {
109
+ lookup[value] = true;
110
+ }
111
+
112
+ for (i = 0, length = data.length; i < length; i++) {
113
+ /*jshint laxbreak: true */
114
+ var _match = lookup && lookup[data[i]] !== undefined
115
+ || !lookup && value.test(data[i]);
116
+ /*jshint laxbreak: false */
117
+ if (_match) {
118
+ data.splice(i, 1);
119
+ length--;
120
+ i--;
121
+ }
122
+ }
123
+
124
+ return data;
125
+ }
126
+
127
+ function arrayContains(list, value) {
128
+ var i, length;
129
+
130
+ // value may be string, number, array, regexp
131
+ if (isArray(value)) {
132
+ // Note: this can be optimized to O(n) (instead of current O(m * n))
133
+ for (i = 0, length = value.length; i < length; i++) {
134
+ if (!arrayContains(list, value[i])) {
135
+ return false;
136
+ }
137
+ }
138
+
139
+ return true;
140
+ }
141
+
142
+ var _type = getType(value);
143
+ for (i = 0, length = list.length; i < length; i++) {
144
+ if (_type === 'RegExp') {
145
+ if (typeof list[i] === 'string' && list[i].match(value)) {
146
+ return true;
147
+ }
148
+ } else if (list[i] === value) {
149
+ return true;
150
+ }
151
+ }
152
+
153
+ return false;
154
+ }
155
+
156
+ function arraysEqual(one, two) {
157
+ if (!isArray(one) || !isArray(two)) {
158
+ return false;
159
+ }
160
+
161
+ // arrays can't be equal if they have different amount of content
162
+ if (one.length !== two.length) {
163
+ return false;
164
+ }
165
+
166
+ one.sort();
167
+ two.sort();
168
+
169
+ for (var i = 0, l = one.length; i < l; i++) {
170
+ if (one[i] !== two[i]) {
171
+ return false;
172
+ }
173
+ }
174
+
175
+ return true;
176
+ }
177
+
178
+ function trimSlashes(text) {
179
+ var trim_expression = /^\/+|\/+$/g;
180
+ return text.replace(trim_expression, '');
181
+ }
182
+
183
+ URI._parts = function() {
184
+ return {
185
+ protocol: null,
186
+ username: null,
187
+ password: null,
188
+ hostname: null,
189
+ urn: null,
190
+ port: null,
191
+ path: null,
192
+ query: null,
193
+ fragment: null,
194
+ // state
195
+ duplicateQueryParameters: URI.duplicateQueryParameters,
196
+ escapeQuerySpace: URI.escapeQuerySpace
197
+ };
198
+ };
199
+ // state: allow duplicate query parameters (a=1&a=1)
200
+ URI.duplicateQueryParameters = false;
201
+ // state: replaces + with %20 (space in query strings)
202
+ URI.escapeQuerySpace = true;
203
+ // static properties
204
+ URI.protocol_expression = /^[a-z][a-z0-9.+-]*$/i;
205
+ URI.idn_expression = /[^a-z0-9\.-]/i;
206
+ URI.punycode_expression = /(xn--)/i;
207
+ // well, 333.444.555.666 matches, but it sure ain't no IPv4 - do we care?
208
+ URI.ip4_expression = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
209
+ // credits to Rich Brown
210
+ // source: http://forums.intermapper.com/viewtopic.php?p=1096#1096
211
+ // specification: http://www.ietf.org/rfc/rfc4291.txt
212
+ URI.ip6_expression = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
213
+ // expression used is "gruber revised" (@gruber v2) determined to be the
214
+ // best solution in a regex-golf we did a couple of ages ago at
215
+ // * http://mathiasbynens.be/demo/url-regex
216
+ // * http://rodneyrehm.de/t/url-regex.html
217
+ URI.find_uri_expression = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/ig;
218
+ URI.findUri = {
219
+ // valid "scheme://" or "www."
220
+ start: /\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi,
221
+ // everything up to the next whitespace
222
+ end: /[\s\r\n]|$/,
223
+ // trim trailing punctuation captured by end RegExp
224
+ trim: /[`!()\[\]{};:'".,<>?«»“”„‘’]+$/
225
+ };
226
+ // http://www.iana.org/assignments/uri-schemes.html
227
+ // http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
228
+ URI.defaultPorts = {
229
+ http: '80',
230
+ https: '443',
231
+ ftp: '21',
232
+ gopher: '70',
233
+ ws: '80',
234
+ wss: '443'
235
+ };
236
+ // allowed hostname characters according to RFC 3986
237
+ // ALPHA DIGIT "-" "." "_" "~" "!" "$" "&" "'" "(" ")" "*" "+" "," ";" "=" %encoded
238
+ // I've never seen a (non-IDN) hostname other than: ALPHA DIGIT . -
239
+ URI.invalid_hostname_characters = /[^a-zA-Z0-9\.-]/;
240
+ // map DOM Elements to their URI attribute
241
+ URI.domAttributes = {
242
+ 'a': 'href',
243
+ 'blockquote': 'cite',
244
+ 'link': 'href',
245
+ 'base': 'href',
246
+ 'script': 'src',
247
+ 'form': 'action',
248
+ 'img': 'src',
249
+ 'area': 'href',
250
+ 'iframe': 'src',
251
+ 'embed': 'src',
252
+ 'source': 'src',
253
+ 'track': 'src',
254
+ 'input': 'src', // but only if type="image"
255
+ 'audio': 'src',
256
+ 'video': 'src'
257
+ };
258
+ URI.getDomAttribute = function(node) {
259
+ if (!node || !node.nodeName) {
260
+ return undefined;
261
+ }
262
+
263
+ var nodeName = node.nodeName.toLowerCase();
264
+ // <input> should only expose src for type="image"
265
+ if (nodeName === 'input' && node.type !== 'image') {
266
+ return undefined;
267
+ }
268
+
269
+ return URI.domAttributes[nodeName];
270
+ };
271
+
272
+ function escapeForDumbFirefox36(value) {
273
+ // https://github.com/medialize/URI.js/issues/91
274
+ return escape(value);
275
+ }
276
+
277
+ // encoding / decoding according to RFC3986
278
+ function strictEncodeURIComponent(string) {
279
+ // see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
280
+ return encodeURIComponent(string)
281
+ .replace(/[!'()*]/g, escapeForDumbFirefox36)
282
+ .replace(/\*/g, '%2A');
283
+ }
284
+ URI.encode = strictEncodeURIComponent;
285
+ URI.decode = decodeURIComponent;
286
+ URI.iso8859 = function() {
287
+ URI.encode = escape;
288
+ URI.decode = unescape;
289
+ };
290
+ URI.unicode = function() {
291
+ URI.encode = strictEncodeURIComponent;
292
+ URI.decode = decodeURIComponent;
293
+ };
294
+ URI.characters = {
295
+ pathname: {
296
+ encode: {
297
+ // RFC3986 2.1: For consistency, URI producers and normalizers should
298
+ // use uppercase hexadecimal digits for all percent-encodings.
299
+ expression: /%(24|26|2B|2C|3B|3D|3A|40)/ig,
300
+ map: {
301
+ // -._~!'()*
302
+ '%24': '$',
303
+ '%26': '&',
304
+ '%2B': '+',
305
+ '%2C': ',',
306
+ '%3B': ';',
307
+ '%3D': '=',
308
+ '%3A': ':',
309
+ '%40': '@'
310
+ }
311
+ },
312
+ decode: {
313
+ expression: /[\/\?#]/g,
314
+ map: {
315
+ '/': '%2F',
316
+ '?': '%3F',
317
+ '#': '%23'
318
+ }
319
+ }
320
+ },
321
+ reserved: {
322
+ encode: {
323
+ // RFC3986 2.1: For consistency, URI producers and normalizers should
324
+ // use uppercase hexadecimal digits for all percent-encodings.
325
+ expression: /%(21|23|24|26|27|28|29|2A|2B|2C|2F|3A|3B|3D|3F|40|5B|5D)/ig,
326
+ map: {
327
+ // gen-delims
328
+ '%3A': ':',
329
+ '%2F': '/',
330
+ '%3F': '?',
331
+ '%23': '#',
332
+ '%5B': '[',
333
+ '%5D': ']',
334
+ '%40': '@',
335
+ // sub-delims
336
+ '%21': '!',
337
+ '%24': '$',
338
+ '%26': '&',
339
+ '%27': '\'',
340
+ '%28': '(',
341
+ '%29': ')',
342
+ '%2A': '*',
343
+ '%2B': '+',
344
+ '%2C': ',',
345
+ '%3B': ';',
346
+ '%3D': '='
347
+ }
348
+ }
349
+ },
350
+ urnpath: {
351
+ // The characters under `encode` are the characters called out by RFC 2141 as being acceptable
352
+ // for usage in a URN. RFC2141 also calls out "-", ".", and "_" as acceptable characters, but
353
+ // these aren't encoded by encodeURIComponent, so we don't have to call them out here. Also
354
+ // note that the colon character is not featured in the encoding map; this is because URI.js
355
+ // gives the colons in URNs semantic meaning as the delimiters of path segements, and so it
356
+ // should not appear unencoded in a segment itself.
357
+ // See also the note above about RFC3986 and capitalalized hex digits.
358
+ encode: {
359
+ expression: /%(21|24|27|28|29|2A|2B|2C|3B|3D|40)/ig,
360
+ map: {
361
+ '%21': '!',
362
+ '%24': '$',
363
+ '%27': '\'',
364
+ '%28': '(',
365
+ '%29': ')',
366
+ '%2A': '*',
367
+ '%2B': '+',
368
+ '%2C': ',',
369
+ '%3B': ';',
370
+ '%3D': '=',
371
+ '%40': '@'
372
+ }
373
+ },
374
+ // These characters are the characters called out by RFC2141 as "reserved" characters that
375
+ // should never appear in a URN, plus the colon character (see note above).
376
+ decode: {
377
+ expression: /[\/\?#:]/g,
378
+ map: {
379
+ '/': '%2F',
380
+ '?': '%3F',
381
+ '#': '%23',
382
+ ':': '%3A'
383
+ }
384
+ }
385
+ }
386
+ };
387
+ URI.encodeQuery = function(string, escapeQuerySpace) {
388
+ var escaped = URI.encode(string + '');
389
+ if (escapeQuerySpace === undefined) {
390
+ escapeQuerySpace = URI.escapeQuerySpace;
391
+ }
392
+
393
+ return escapeQuerySpace ? escaped.replace(/%20/g, '+') : escaped;
394
+ };
395
+ URI.decodeQuery = function(string, escapeQuerySpace) {
396
+ string += '';
397
+ if (escapeQuerySpace === undefined) {
398
+ escapeQuerySpace = URI.escapeQuerySpace;
399
+ }
400
+
401
+ try {
402
+ return URI.decode(escapeQuerySpace ? string.replace(/\+/g, '%20') : string);
403
+ } catch(e) {
404
+ // we're not going to mess with weird encodings,
405
+ // give up and return the undecoded original string
406
+ // see https://github.com/medialize/URI.js/issues/87
407
+ // see https://github.com/medialize/URI.js/issues/92
408
+ return string;
409
+ }
410
+ };
411
+ // generate encode/decode path functions
412
+ var _parts = {'encode':'encode', 'decode':'decode'};
413
+ var _part;
414
+ var generateAccessor = function(_group, _part) {
415
+ return function(string) {
416
+ try {
417
+ return URI[_part](string + '').replace(URI.characters[_group][_part].expression, function(c) {
418
+ return URI.characters[_group][_part].map[c];
419
+ });
420
+ } catch (e) {
421
+ // we're not going to mess with weird encodings,
422
+ // give up and return the undecoded original string
423
+ // see https://github.com/medialize/URI.js/issues/87
424
+ // see https://github.com/medialize/URI.js/issues/92
425
+ return string;
426
+ }
427
+ };
428
+ };
429
+
430
+ for (_part in _parts) {
431
+ URI[_part + 'PathSegment'] = generateAccessor('pathname', _parts[_part]);
432
+ URI[_part + 'UrnPathSegment'] = generateAccessor('urnpath', _parts[_part]);
433
+ }
434
+
435
+ var generateSegmentedPathFunction = function(_sep, _codingFuncName, _innerCodingFuncName) {
436
+ return function(string) {
437
+ // Why pass in names of functions, rather than the function objects themselves? The
438
+ // definitions of some functions (but in particular, URI.decode) will occasionally change due
439
+ // to URI.js having ISO8859 and Unicode modes. Passing in the name and getting it will ensure
440
+ // that the functions we use here are "fresh".
441
+ var actualCodingFunc;
442
+ if (!_innerCodingFuncName) {
443
+ actualCodingFunc = URI[_codingFuncName];
444
+ } else {
445
+ actualCodingFunc = function(string) {
446
+ return URI[_codingFuncName](URI[_innerCodingFuncName](string));
447
+ };
448
+ }
449
+
450
+ var segments = (string + '').split(_sep);
451
+
452
+ for (var i = 0, length = segments.length; i < length; i++) {
453
+ segments[i] = actualCodingFunc(segments[i]);
454
+ }
455
+
456
+ return segments.join(_sep);
457
+ };
458
+ };
459
+
460
+ // This takes place outside the above loop because we don't want, e.g., encodeUrnPath functions.
461
+ URI.decodePath = generateSegmentedPathFunction('/', 'decodePathSegment');
462
+ URI.decodeUrnPath = generateSegmentedPathFunction(':', 'decodeUrnPathSegment');
463
+ URI.recodePath = generateSegmentedPathFunction('/', 'encodePathSegment', 'decode');
464
+ URI.recodeUrnPath = generateSegmentedPathFunction(':', 'encodeUrnPathSegment', 'decode');
465
+
466
+ URI.encodeReserved = generateAccessor('reserved', 'encode');
467
+
468
+ URI.parse = function(string, parts) {
469
+ var pos;
470
+ if (!parts) {
471
+ parts = {};
472
+ }
473
+ // [protocol"://"[username[":"password]"@"]hostname[":"port]"/"?][path]["?"querystring]["#"fragment]
474
+
475
+ // extract fragment
476
+ pos = string.indexOf('#');
477
+ if (pos > -1) {
478
+ // escaping?
479
+ parts.fragment = string.substring(pos + 1) || null;
480
+ string = string.substring(0, pos);
481
+ }
482
+
483
+ // extract query
484
+ pos = string.indexOf('?');
485
+ if (pos > -1) {
486
+ // escaping?
487
+ parts.query = string.substring(pos + 1) || null;
488
+ string = string.substring(0, pos);
489
+ }
490
+
491
+ // extract protocol
492
+ if (string.substring(0, 2) === '//') {
493
+ // relative-scheme
494
+ parts.protocol = null;
495
+ string = string.substring(2);
496
+ // extract "user:pass@host:port"
497
+ string = URI.parseAuthority(string, parts);
498
+ } else {
499
+ pos = string.indexOf(':');
500
+ if (pos > -1) {
501
+ parts.protocol = string.substring(0, pos) || null;
502
+ if (parts.protocol && !parts.protocol.match(URI.protocol_expression)) {
503
+ // : may be within the path
504
+ parts.protocol = undefined;
505
+ } else if (string.substring(pos + 1, pos + 3) === '//') {
506
+ string = string.substring(pos + 3);
507
+
508
+ // extract "user:pass@host:port"
509
+ string = URI.parseAuthority(string, parts);
510
+ } else {
511
+ string = string.substring(pos + 1);
512
+ parts.urn = true;
513
+ }
514
+ }
515
+ }
516
+
517
+ // what's left must be the path
518
+ parts.path = string;
519
+
520
+ // and we're done
521
+ return parts;
522
+ };
523
+ URI.parseHost = function(string, parts) {
524
+ // Copy chrome, IE, opera backslash-handling behavior.
525
+ // Back slashes before the query string get converted to forward slashes
526
+ // See: https://github.com/joyent/node/blob/386fd24f49b0e9d1a8a076592a404168faeecc34/lib/url.js#L115-L124
527
+ // See: https://code.google.com/p/chromium/issues/detail?id=25916
528
+ // https://github.com/medialize/URI.js/pull/233
529
+ string = string.replace(/\\/g, '/');
530
+
531
+ // extract host:port
532
+ var pos = string.indexOf('/');
533
+ var bracketPos;
534
+ var t;
535
+
536
+ if (pos === -1) {
537
+ pos = string.length;
538
+ }
539
+
540
+ if (string.charAt(0) === '[') {
541
+ // IPv6 host - http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04#section-6
542
+ // I claim most client software breaks on IPv6 anyways. To simplify things, URI only accepts
543
+ // IPv6+port in the format [2001:db8::1]:80 (for the time being)
544
+ bracketPos = string.indexOf(']');
545
+ parts.hostname = string.substring(1, bracketPos) || null;
546
+ parts.port = string.substring(bracketPos + 2, pos) || null;
547
+ if (parts.port === '/') {
548
+ parts.port = null;
549
+ }
550
+ } else {
551
+ var firstColon = string.indexOf(':');
552
+ var firstSlash = string.indexOf('/');
553
+ var nextColon = string.indexOf(':', firstColon + 1);
554
+ if (nextColon !== -1 && (firstSlash === -1 || nextColon < firstSlash)) {
555
+ // IPv6 host contains multiple colons - but no port
556
+ // this notation is actually not allowed by RFC 3986, but we're a liberal parser
557
+ parts.hostname = string.substring(0, pos) || null;
558
+ parts.port = null;
559
+ } else {
560
+ t = string.substring(0, pos).split(':');
561
+ parts.hostname = t[0] || null;
562
+ parts.port = t[1] || null;
563
+ }
564
+ }
565
+
566
+ if (parts.hostname && string.substring(pos).charAt(0) !== '/') {
567
+ pos++;
568
+ string = '/' + string;
569
+ }
570
+
571
+ return string.substring(pos) || '/';
572
+ };
573
+ URI.parseAuthority = function(string, parts) {
574
+ string = URI.parseUserinfo(string, parts);
575
+ return URI.parseHost(string, parts);
576
+ };
577
+ URI.parseUserinfo = function(string, parts) {
578
+ // extract username:password
579
+ var firstSlash = string.indexOf('/');
580
+ var pos = string.lastIndexOf('@', firstSlash > -1 ? firstSlash : string.length - 1);
581
+ var t;
582
+
583
+ // authority@ must come before /path
584
+ if (pos > -1 && (firstSlash === -1 || pos < firstSlash)) {
585
+ t = string.substring(0, pos).split(':');
586
+ parts.username = t[0] ? URI.decode(t[0]) : null;
587
+ t.shift();
588
+ parts.password = t[0] ? URI.decode(t.join(':')) : null;
589
+ string = string.substring(pos + 1);
590
+ } else {
591
+ parts.username = null;
592
+ parts.password = null;
593
+ }
594
+
595
+ return string;
596
+ };
597
+ URI.parseQuery = function(string, escapeQuerySpace) {
598
+ if (!string) {
599
+ return {};
600
+ }
601
+
602
+ // throw out the funky business - "?"[name"="value"&"]+
603
+ string = string.replace(/&+/g, '&').replace(/^\?*&*|&+$/g, '');
604
+
605
+ if (!string) {
606
+ return {};
607
+ }
608
+
609
+ var items = {};
610
+ var splits = string.split('&');
611
+ var length = splits.length;
612
+ var v, name, value;
613
+
614
+ for (var i = 0; i < length; i++) {
615
+ v = splits[i].split('=');
616
+ name = URI.decodeQuery(v.shift(), escapeQuerySpace);
617
+ // no "=" is null according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#collect-url-parameters
618
+ value = v.length ? URI.decodeQuery(v.join('='), escapeQuerySpace) : null;
619
+
620
+ if (hasOwn.call(items, name)) {
621
+ if (typeof items[name] === 'string' || items[name] === null) {
622
+ items[name] = [items[name]];
623
+ }
624
+
625
+ items[name].push(value);
626
+ } else {
627
+ items[name] = value;
628
+ }
629
+ }
630
+
631
+ return items;
632
+ };
633
+
634
+ URI.build = function(parts) {
635
+ var t = '';
636
+
637
+ if (parts.protocol) {
638
+ t += parts.protocol + ':';
639
+ }
640
+
641
+ if (!parts.urn && (t || parts.hostname)) {
642
+ t += '//';
643
+ }
644
+
645
+ t += (URI.buildAuthority(parts) || '');
646
+
647
+ if (typeof parts.path === 'string') {
648
+ if (parts.path.charAt(0) !== '/' && typeof parts.hostname === 'string') {
649
+ t += '/';
650
+ }
651
+
652
+ t += parts.path;
653
+ }
654
+
655
+ if (typeof parts.query === 'string' && parts.query) {
656
+ t += '?' + parts.query;
657
+ }
658
+
659
+ if (typeof parts.fragment === 'string' && parts.fragment) {
660
+ t += '#' + parts.fragment;
661
+ }
662
+ return t;
663
+ };
664
+ URI.buildHost = function(parts) {
665
+ var t = '';
666
+
667
+ if (!parts.hostname) {
668
+ return '';
669
+ } else if (URI.ip6_expression.test(parts.hostname)) {
670
+ t += '[' + parts.hostname + ']';
671
+ } else {
672
+ t += parts.hostname;
673
+ }
674
+
675
+ if (parts.port) {
676
+ t += ':' + parts.port;
677
+ }
678
+
679
+ return t;
680
+ };
681
+ URI.buildAuthority = function(parts) {
682
+ return URI.buildUserinfo(parts) + URI.buildHost(parts);
683
+ };
684
+ URI.buildUserinfo = function(parts) {
685
+ var t = '';
686
+
687
+ if (parts.username) {
688
+ t += URI.encode(parts.username);
689
+
690
+ if (parts.password) {
691
+ t += ':' + URI.encode(parts.password);
692
+ }
693
+
694
+ t += '@';
695
+ }
696
+
697
+ return t;
698
+ };
699
+ URI.buildQuery = function(data, duplicateQueryParameters, escapeQuerySpace) {
700
+ // according to http://tools.ietf.org/html/rfc3986 or http://labs.apache.org/webarch/uri/rfc/rfc3986.html
701
+ // being »-._~!$&'()*+,;=:@/?« %HEX and alnum are allowed
702
+ // the RFC explicitly states ?/foo being a valid use case, no mention of parameter syntax!
703
+ // URI.js treats the query string as being application/x-www-form-urlencoded
704
+ // see http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type
705
+
706
+ var t = '';
707
+ var unique, key, i, length;
708
+ for (key in data) {
709
+ if (hasOwn.call(data, key) && key) {
710
+ if (isArray(data[key])) {
711
+ unique = {};
712
+ for (i = 0, length = data[key].length; i < length; i++) {
713
+ if (data[key][i] !== undefined && unique[data[key][i] + ''] === undefined) {
714
+ t += '&' + URI.buildQueryParameter(key, data[key][i], escapeQuerySpace);
715
+ if (duplicateQueryParameters !== true) {
716
+ unique[data[key][i] + ''] = true;
717
+ }
718
+ }
719
+ }
720
+ } else if (data[key] !== undefined) {
721
+ t += '&' + URI.buildQueryParameter(key, data[key], escapeQuerySpace);
722
+ }
723
+ }
724
+ }
725
+
726
+ return t.substring(1);
727
+ };
728
+ URI.buildQueryParameter = function(name, value, escapeQuerySpace) {
729
+ // http://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type -- application/x-www-form-urlencoded
730
+ // don't append "=" for null values, according to http://dvcs.w3.org/hg/url/raw-file/tip/Overview.html#url-parameter-serialization
731
+ return URI.encodeQuery(name, escapeQuerySpace) + (value !== null ? '=' + URI.encodeQuery(value, escapeQuerySpace) : '');
732
+ };
733
+
734
+ URI.addQuery = function(data, name, value) {
735
+ if (typeof name === 'object') {
736
+ for (var key in name) {
737
+ if (hasOwn.call(name, key)) {
738
+ URI.addQuery(data, key, name[key]);
739
+ }
740
+ }
741
+ } else if (typeof name === 'string') {
742
+ if (data[name] === undefined) {
743
+ data[name] = value;
744
+ return;
745
+ } else if (typeof data[name] === 'string') {
746
+ data[name] = [data[name]];
747
+ }
748
+
749
+ if (!isArray(value)) {
750
+ value = [value];
751
+ }
752
+
753
+ data[name] = (data[name] || []).concat(value);
754
+ } else {
755
+ throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
756
+ }
757
+ };
758
+ URI.removeQuery = function(data, name, value) {
759
+ var i, length, key;
760
+
761
+ if (isArray(name)) {
762
+ for (i = 0, length = name.length; i < length; i++) {
763
+ data[name[i]] = undefined;
764
+ }
765
+ } else if (getType(name) === 'RegExp') {
766
+ for (key in data) {
767
+ if (name.test(key)) {
768
+ data[key] = undefined;
769
+ }
770
+ }
771
+ } else if (typeof name === 'object') {
772
+ for (key in name) {
773
+ if (hasOwn.call(name, key)) {
774
+ URI.removeQuery(data, key, name[key]);
775
+ }
776
+ }
777
+ } else if (typeof name === 'string') {
778
+ if (value !== undefined) {
779
+ if (getType(value) === 'RegExp') {
780
+ if (!isArray(data[name]) && value.test(data[name])) {
781
+ data[name] = undefined;
782
+ } else {
783
+ data[name] = filterArrayValues(data[name], value);
784
+ }
785
+ } else if (data[name] === String(value) && (!isArray(value) || value.length === 1)) {
786
+ data[name] = undefined;
787
+ } else if (isArray(data[name])) {
788
+ data[name] = filterArrayValues(data[name], value);
789
+ }
790
+ } else {
791
+ data[name] = undefined;
792
+ }
793
+ } else {
794
+ throw new TypeError('URI.removeQuery() accepts an object, string, RegExp as the first parameter');
795
+ }
796
+ };
797
+ URI.hasQuery = function(data, name, value, withinArray) {
798
+ if (typeof name === 'object') {
799
+ for (var key in name) {
800
+ if (hasOwn.call(name, key)) {
801
+ if (!URI.hasQuery(data, key, name[key])) {
802
+ return false;
803
+ }
804
+ }
805
+ }
806
+
807
+ return true;
808
+ } else if (typeof name !== 'string') {
809
+ throw new TypeError('URI.hasQuery() accepts an object, string as the name parameter');
810
+ }
811
+
812
+ switch (getType(value)) {
813
+ case 'Undefined':
814
+ // true if exists (but may be empty)
815
+ return name in data; // data[name] !== undefined;
816
+
817
+ case 'Boolean':
818
+ // true if exists and non-empty
819
+ var _booly = Boolean(isArray(data[name]) ? data[name].length : data[name]);
820
+ return value === _booly;
821
+
822
+ case 'Function':
823
+ // allow complex comparison
824
+ return !!value(data[name], name, data);
825
+
826
+ case 'Array':
827
+ if (!isArray(data[name])) {
828
+ return false;
829
+ }
830
+
831
+ var op = withinArray ? arrayContains : arraysEqual;
832
+ return op(data[name], value);
833
+
834
+ case 'RegExp':
835
+ if (!isArray(data[name])) {
836
+ return Boolean(data[name] && data[name].match(value));
837
+ }
838
+
839
+ if (!withinArray) {
840
+ return false;
841
+ }
842
+
843
+ return arrayContains(data[name], value);
844
+
845
+ case 'Number':
846
+ value = String(value);
847
+ /* falls through */
848
+ case 'String':
849
+ if (!isArray(data[name])) {
850
+ return data[name] === value;
851
+ }
852
+
853
+ if (!withinArray) {
854
+ return false;
855
+ }
856
+
857
+ return arrayContains(data[name], value);
858
+
859
+ default:
860
+ throw new TypeError('URI.hasQuery() accepts undefined, boolean, string, number, RegExp, Function as the value parameter');
861
+ }
862
+ };
863
+
864
+
865
+ URI.commonPath = function(one, two) {
866
+ var length = Math.min(one.length, two.length);
867
+ var pos;
868
+
869
+ // find first non-matching character
870
+ for (pos = 0; pos < length; pos++) {
871
+ if (one.charAt(pos) !== two.charAt(pos)) {
872
+ pos--;
873
+ break;
874
+ }
875
+ }
876
+
877
+ if (pos < 1) {
878
+ return one.charAt(0) === two.charAt(0) && one.charAt(0) === '/' ? '/' : '';
879
+ }
880
+
881
+ // revert to last /
882
+ if (one.charAt(pos) !== '/' || two.charAt(pos) !== '/') {
883
+ pos = one.substring(0, pos).lastIndexOf('/');
884
+ }
885
+
886
+ return one.substring(0, pos + 1);
887
+ };
888
+
889
+ URI.withinString = function(string, callback, options) {
890
+ options || (options = {});
891
+ var _start = options.start || URI.findUri.start;
892
+ var _end = options.end || URI.findUri.end;
893
+ var _trim = options.trim || URI.findUri.trim;
894
+ var _attributeOpen = /[a-z0-9-]=["']?$/i;
895
+
896
+ _start.lastIndex = 0;
897
+ while (true) {
898
+ var match = _start.exec(string);
899
+ if (!match) {
900
+ break;
901
+ }
902
+
903
+ var start = match.index;
904
+ if (options.ignoreHtml) {
905
+ // attribut(e=["']?$)
906
+ var attributeOpen = string.slice(Math.max(start - 3, 0), start);
907
+ if (attributeOpen && _attributeOpen.test(attributeOpen)) {
908
+ continue;
909
+ }
910
+ }
911
+
912
+ var end = start + string.slice(start).search(_end);
913
+ var slice = string.slice(start, end).replace(_trim, '');
914
+ if (options.ignore && options.ignore.test(slice)) {
915
+ continue;
916
+ }
917
+
918
+ end = start + slice.length;
919
+ var result = callback(slice, start, end, string);
920
+ string = string.slice(0, start) + result + string.slice(end);
921
+ _start.lastIndex = start + result.length;
922
+ }
923
+
924
+ _start.lastIndex = 0;
925
+ return string;
926
+ };
927
+
928
+ URI.ensureValidHostname = function(v) {
929
+ // Theoretically URIs allow percent-encoding in Hostnames (according to RFC 3986)
930
+ // they are not part of DNS and therefore ignored by URI.js
931
+
932
+ if (v.match(URI.invalid_hostname_characters)) {
933
+ // test punycode
934
+ if (!punycode) {
935
+ throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-] and Punycode.js is not available');
936
+ }
937
+
938
+ if (punycode.toASCII(v).match(URI.invalid_hostname_characters)) {
939
+ throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
940
+ }
941
+ }
942
+ };
943
+
944
+ // noConflict
945
+ URI.noConflict = function(removeAll) {
946
+ if (removeAll) {
947
+ var unconflicted = {
948
+ URI: this.noConflict()
949
+ };
950
+
951
+ if (root.URITemplate && typeof root.URITemplate.noConflict === 'function') {
952
+ unconflicted.URITemplate = root.URITemplate.noConflict();
953
+ }
954
+
955
+ if (root.IPv6 && typeof root.IPv6.noConflict === 'function') {
956
+ unconflicted.IPv6 = root.IPv6.noConflict();
957
+ }
958
+
959
+ if (root.SecondLevelDomains && typeof root.SecondLevelDomains.noConflict === 'function') {
960
+ unconflicted.SecondLevelDomains = root.SecondLevelDomains.noConflict();
961
+ }
962
+
963
+ return unconflicted;
964
+ } else if (root.URI === this) {
965
+ root.URI = _URI;
966
+ }
967
+
968
+ return this;
969
+ };
970
+
971
+ p.build = function(deferBuild) {
972
+ if (deferBuild === true) {
973
+ this._deferred_build = true;
974
+ } else if (deferBuild === undefined || this._deferred_build) {
975
+ this._string = URI.build(this._parts);
976
+ this._deferred_build = false;
977
+ }
978
+
979
+ return this;
980
+ };
981
+
982
+ p.clone = function() {
983
+ return new URI(this);
984
+ };
985
+
986
+ p.valueOf = p.toString = function() {
987
+ return this.build(false)._string;
988
+ };
989
+
990
+
991
+ function generateSimpleAccessor(_part){
992
+ return function(v, build) {
993
+ if (v === undefined) {
994
+ return this._parts[_part] || '';
995
+ } else {
996
+ this._parts[_part] = v || null;
997
+ this.build(!build);
998
+ return this;
999
+ }
1000
+ };
1001
+ }
1002
+
1003
+ function generatePrefixAccessor(_part, _key){
1004
+ return function(v, build) {
1005
+ if (v === undefined) {
1006
+ return this._parts[_part] || '';
1007
+ } else {
1008
+ if (v !== null) {
1009
+ v = v + '';
1010
+ if (v.charAt(0) === _key) {
1011
+ v = v.substring(1);
1012
+ }
1013
+ }
1014
+
1015
+ this._parts[_part] = v;
1016
+ this.build(!build);
1017
+ return this;
1018
+ }
1019
+ };
1020
+ }
1021
+
1022
+ p.protocol = generateSimpleAccessor('protocol');
1023
+ p.username = generateSimpleAccessor('username');
1024
+ p.password = generateSimpleAccessor('password');
1025
+ p.hostname = generateSimpleAccessor('hostname');
1026
+ p.port = generateSimpleAccessor('port');
1027
+ p.query = generatePrefixAccessor('query', '?');
1028
+ p.fragment = generatePrefixAccessor('fragment', '#');
1029
+
1030
+ p.search = function(v, build) {
1031
+ var t = this.query(v, build);
1032
+ return typeof t === 'string' && t.length ? ('?' + t) : t;
1033
+ };
1034
+ p.hash = function(v, build) {
1035
+ var t = this.fragment(v, build);
1036
+ return typeof t === 'string' && t.length ? ('#' + t) : t;
1037
+ };
1038
+
1039
+ p.pathname = function(v, build) {
1040
+ if (v === undefined || v === true) {
1041
+ var res = this._parts.path || (this._parts.hostname ? '/' : '');
1042
+ return v ? (this._parts.urn ? URI.decodeUrnPath : URI.decodePath)(res) : res;
1043
+ } else {
1044
+ if (this._parts.urn) {
1045
+ this._parts.path = v ? URI.recodeUrnPath(v) : '';
1046
+ } else {
1047
+ this._parts.path = v ? URI.recodePath(v) : '/';
1048
+ }
1049
+ this.build(!build);
1050
+ return this;
1051
+ }
1052
+ };
1053
+ p.path = p.pathname;
1054
+ p.href = function(href, build) {
1055
+ var key;
1056
+
1057
+ if (href === undefined) {
1058
+ return this.toString();
1059
+ }
1060
+
1061
+ this._string = '';
1062
+ this._parts = URI._parts();
1063
+
1064
+ var _URI = href instanceof URI;
1065
+ var _object = typeof href === 'object' && (href.hostname || href.path || href.pathname);
1066
+ if (href.nodeName) {
1067
+ var attribute = URI.getDomAttribute(href);
1068
+ href = href[attribute] || '';
1069
+ _object = false;
1070
+ }
1071
+
1072
+ // window.location is reported to be an object, but it's not the sort
1073
+ // of object we're looking for:
1074
+ // * location.protocol ends with a colon
1075
+ // * location.query != object.search
1076
+ // * location.hash != object.fragment
1077
+ // simply serializing the unknown object should do the trick
1078
+ // (for location, not for everything...)
1079
+ if (!_URI && _object && href.pathname !== undefined) {
1080
+ href = href.toString();
1081
+ }
1082
+
1083
+ if (typeof href === 'string' || href instanceof String) {
1084
+ this._parts = URI.parse(String(href), this._parts);
1085
+ } else if (_URI || _object) {
1086
+ var src = _URI ? href._parts : href;
1087
+ for (key in src) {
1088
+ if (hasOwn.call(this._parts, key)) {
1089
+ this._parts[key] = src[key];
1090
+ }
1091
+ }
1092
+ } else {
1093
+ throw new TypeError('invalid input');
1094
+ }
1095
+
1096
+ this.build(!build);
1097
+ return this;
1098
+ };
1099
+
1100
+ // identification accessors
1101
+ p.is = function(what) {
1102
+ var ip = false;
1103
+ var ip4 = false;
1104
+ var ip6 = false;
1105
+ var name = false;
1106
+ var sld = false;
1107
+ var idn = false;
1108
+ var punycode = false;
1109
+ var relative = !this._parts.urn;
1110
+
1111
+ if (this._parts.hostname) {
1112
+ relative = false;
1113
+ ip4 = URI.ip4_expression.test(this._parts.hostname);
1114
+ ip6 = URI.ip6_expression.test(this._parts.hostname);
1115
+ ip = ip4 || ip6;
1116
+ name = !ip;
1117
+ sld = name && SLD && SLD.has(this._parts.hostname);
1118
+ idn = name && URI.idn_expression.test(this._parts.hostname);
1119
+ punycode = name && URI.punycode_expression.test(this._parts.hostname);
1120
+ }
1121
+
1122
+ switch (what.toLowerCase()) {
1123
+ case 'relative':
1124
+ return relative;
1125
+
1126
+ case 'absolute':
1127
+ return !relative;
1128
+
1129
+ // hostname identification
1130
+ case 'domain':
1131
+ case 'name':
1132
+ return name;
1133
+
1134
+ case 'sld':
1135
+ return sld;
1136
+
1137
+ case 'ip':
1138
+ return ip;
1139
+
1140
+ case 'ip4':
1141
+ case 'ipv4':
1142
+ case 'inet4':
1143
+ return ip4;
1144
+
1145
+ case 'ip6':
1146
+ case 'ipv6':
1147
+ case 'inet6':
1148
+ return ip6;
1149
+
1150
+ case 'idn':
1151
+ return idn;
1152
+
1153
+ case 'url':
1154
+ return !this._parts.urn;
1155
+
1156
+ case 'urn':
1157
+ return !!this._parts.urn;
1158
+
1159
+ case 'punycode':
1160
+ return punycode;
1161
+ }
1162
+
1163
+ return null;
1164
+ };
1165
+
1166
+ // component specific input validation
1167
+ var _protocol = p.protocol;
1168
+ var _port = p.port;
1169
+ var _hostname = p.hostname;
1170
+
1171
+ p.protocol = function(v, build) {
1172
+ if (v !== undefined) {
1173
+ if (v) {
1174
+ // accept trailing ://
1175
+ v = v.replace(/:(\/\/)?$/, '');
1176
+
1177
+ if (!v.match(URI.protocol_expression)) {
1178
+ throw new TypeError('Protocol "' + v + '" contains characters other than [A-Z0-9.+-] or doesn\'t start with [A-Z]');
1179
+ }
1180
+ }
1181
+ }
1182
+ return _protocol.call(this, v, build);
1183
+ };
1184
+ p.scheme = p.protocol;
1185
+ p.port = function(v, build) {
1186
+ if (this._parts.urn) {
1187
+ return v === undefined ? '' : this;
1188
+ }
1189
+
1190
+ if (v !== undefined) {
1191
+ if (v === 0) {
1192
+ v = null;
1193
+ }
1194
+
1195
+ if (v) {
1196
+ v += '';
1197
+ if (v.charAt(0) === ':') {
1198
+ v = v.substring(1);
1199
+ }
1200
+
1201
+ if (v.match(/[^0-9]/)) {
1202
+ throw new TypeError('Port "' + v + '" contains characters other than [0-9]');
1203
+ }
1204
+ }
1205
+ }
1206
+ return _port.call(this, v, build);
1207
+ };
1208
+ p.hostname = function(v, build) {
1209
+ if (this._parts.urn) {
1210
+ return v === undefined ? '' : this;
1211
+ }
1212
+
1213
+ if (v !== undefined) {
1214
+ var x = {};
1215
+ var res = URI.parseHost(v, x);
1216
+ if (res !== '/') {
1217
+ throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1218
+ }
1219
+
1220
+ v = x.hostname;
1221
+ }
1222
+ return _hostname.call(this, v, build);
1223
+ };
1224
+
1225
+ // compound accessors
1226
+ p.origin = function(v, build) {
1227
+ var parts;
1228
+
1229
+ if (this._parts.urn) {
1230
+ return v === undefined ? '' : this;
1231
+ }
1232
+
1233
+ if (v === undefined) {
1234
+ var protocol = this.protocol();
1235
+ var authority = this.authority();
1236
+ if (!authority) return '';
1237
+ return (protocol ? protocol + '://' : '') + this.authority();
1238
+ } else {
1239
+ var origin = URI(v);
1240
+ this
1241
+ .protocol(origin.protocol())
1242
+ .authority(origin.authority())
1243
+ .build(!build);
1244
+ return this;
1245
+ }
1246
+ };
1247
+ p.host = function(v, build) {
1248
+ if (this._parts.urn) {
1249
+ return v === undefined ? '' : this;
1250
+ }
1251
+
1252
+ if (v === undefined) {
1253
+ return this._parts.hostname ? URI.buildHost(this._parts) : '';
1254
+ } else {
1255
+ var res = URI.parseHost(v, this._parts);
1256
+ if (res !== '/') {
1257
+ throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1258
+ }
1259
+
1260
+ this.build(!build);
1261
+ return this;
1262
+ }
1263
+ };
1264
+ p.authority = function(v, build) {
1265
+ if (this._parts.urn) {
1266
+ return v === undefined ? '' : this;
1267
+ }
1268
+
1269
+ if (v === undefined) {
1270
+ return this._parts.hostname ? URI.buildAuthority(this._parts) : '';
1271
+ } else {
1272
+ var res = URI.parseAuthority(v, this._parts);
1273
+ if (res !== '/') {
1274
+ throw new TypeError('Hostname "' + v + '" contains characters other than [A-Z0-9.-]');
1275
+ }
1276
+
1277
+ this.build(!build);
1278
+ return this;
1279
+ }
1280
+ };
1281
+ p.userinfo = function(v, build) {
1282
+ if (this._parts.urn) {
1283
+ return v === undefined ? '' : this;
1284
+ }
1285
+
1286
+ if (v === undefined) {
1287
+ if (!this._parts.username) {
1288
+ return '';
1289
+ }
1290
+
1291
+ var t = URI.buildUserinfo(this._parts);
1292
+ return t.substring(0, t.length -1);
1293
+ } else {
1294
+ if (v[v.length-1] !== '@') {
1295
+ v += '@';
1296
+ }
1297
+
1298
+ URI.parseUserinfo(v, this._parts);
1299
+ this.build(!build);
1300
+ return this;
1301
+ }
1302
+ };
1303
+ p.resource = function(v, build) {
1304
+ var parts;
1305
+
1306
+ if (v === undefined) {
1307
+ return this.path() + this.search() + this.hash();
1308
+ }
1309
+
1310
+ parts = URI.parse(v);
1311
+ this._parts.path = parts.path;
1312
+ this._parts.query = parts.query;
1313
+ this._parts.fragment = parts.fragment;
1314
+ this.build(!build);
1315
+ return this;
1316
+ };
1317
+
1318
+ // fraction accessors
1319
+ p.subdomain = function(v, build) {
1320
+ if (this._parts.urn) {
1321
+ return v === undefined ? '' : this;
1322
+ }
1323
+
1324
+ // convenience, return "www" from "www.example.org"
1325
+ if (v === undefined) {
1326
+ if (!this._parts.hostname || this.is('IP')) {
1327
+ return '';
1328
+ }
1329
+
1330
+ // grab domain and add another segment
1331
+ var end = this._parts.hostname.length - this.domain().length - 1;
1332
+ return this._parts.hostname.substring(0, end) || '';
1333
+ } else {
1334
+ var e = this._parts.hostname.length - this.domain().length;
1335
+ var sub = this._parts.hostname.substring(0, e);
1336
+ var replace = new RegExp('^' + escapeRegEx(sub));
1337
+
1338
+ if (v && v.charAt(v.length - 1) !== '.') {
1339
+ v += '.';
1340
+ }
1341
+
1342
+ if (v) {
1343
+ URI.ensureValidHostname(v);
1344
+ }
1345
+
1346
+ this._parts.hostname = this._parts.hostname.replace(replace, v);
1347
+ this.build(!build);
1348
+ return this;
1349
+ }
1350
+ };
1351
+ p.domain = function(v, build) {
1352
+ if (this._parts.urn) {
1353
+ return v === undefined ? '' : this;
1354
+ }
1355
+
1356
+ if (typeof v === 'boolean') {
1357
+ build = v;
1358
+ v = undefined;
1359
+ }
1360
+
1361
+ // convenience, return "example.org" from "www.example.org"
1362
+ if (v === undefined) {
1363
+ if (!this._parts.hostname || this.is('IP')) {
1364
+ return '';
1365
+ }
1366
+
1367
+ // if hostname consists of 1 or 2 segments, it must be the domain
1368
+ var t = this._parts.hostname.match(/\./g);
1369
+ if (t && t.length < 2) {
1370
+ return this._parts.hostname;
1371
+ }
1372
+
1373
+ // grab tld and add another segment
1374
+ var end = this._parts.hostname.length - this.tld(build).length - 1;
1375
+ end = this._parts.hostname.lastIndexOf('.', end -1) + 1;
1376
+ return this._parts.hostname.substring(end) || '';
1377
+ } else {
1378
+ if (!v) {
1379
+ throw new TypeError('cannot set domain empty');
1380
+ }
1381
+
1382
+ URI.ensureValidHostname(v);
1383
+
1384
+ if (!this._parts.hostname || this.is('IP')) {
1385
+ this._parts.hostname = v;
1386
+ } else {
1387
+ var replace = new RegExp(escapeRegEx(this.domain()) + '$');
1388
+ this._parts.hostname = this._parts.hostname.replace(replace, v);
1389
+ }
1390
+
1391
+ this.build(!build);
1392
+ return this;
1393
+ }
1394
+ };
1395
+ p.tld = function(v, build) {
1396
+ if (this._parts.urn) {
1397
+ return v === undefined ? '' : this;
1398
+ }
1399
+
1400
+ if (typeof v === 'boolean') {
1401
+ build = v;
1402
+ v = undefined;
1403
+ }
1404
+
1405
+ // return "org" from "www.example.org"
1406
+ if (v === undefined) {
1407
+ if (!this._parts.hostname || this.is('IP')) {
1408
+ return '';
1409
+ }
1410
+
1411
+ var pos = this._parts.hostname.lastIndexOf('.');
1412
+ var tld = this._parts.hostname.substring(pos + 1);
1413
+
1414
+ if (build !== true && SLD && SLD.list[tld.toLowerCase()]) {
1415
+ return SLD.get(this._parts.hostname) || tld;
1416
+ }
1417
+
1418
+ return tld;
1419
+ } else {
1420
+ var replace;
1421
+
1422
+ if (!v) {
1423
+ throw new TypeError('cannot set TLD empty');
1424
+ } else if (v.match(/[^a-zA-Z0-9-]/)) {
1425
+ if (SLD && SLD.is(v)) {
1426
+ replace = new RegExp(escapeRegEx(this.tld()) + '$');
1427
+ this._parts.hostname = this._parts.hostname.replace(replace, v);
1428
+ } else {
1429
+ throw new TypeError('TLD "' + v + '" contains characters other than [A-Z0-9]');
1430
+ }
1431
+ } else if (!this._parts.hostname || this.is('IP')) {
1432
+ throw new ReferenceError('cannot set TLD on non-domain host');
1433
+ } else {
1434
+ replace = new RegExp(escapeRegEx(this.tld()) + '$');
1435
+ this._parts.hostname = this._parts.hostname.replace(replace, v);
1436
+ }
1437
+
1438
+ this.build(!build);
1439
+ return this;
1440
+ }
1441
+ };
1442
+ p.directory = function(v, build) {
1443
+ if (this._parts.urn) {
1444
+ return v === undefined ? '' : this;
1445
+ }
1446
+
1447
+ if (v === undefined || v === true) {
1448
+ if (!this._parts.path && !this._parts.hostname) {
1449
+ return '';
1450
+ }
1451
+
1452
+ if (this._parts.path === '/') {
1453
+ return '/';
1454
+ }
1455
+
1456
+ var end = this._parts.path.length - this.filename().length - 1;
1457
+ var res = this._parts.path.substring(0, end) || (this._parts.hostname ? '/' : '');
1458
+
1459
+ return v ? URI.decodePath(res) : res;
1460
+
1461
+ } else {
1462
+ var e = this._parts.path.length - this.filename().length;
1463
+ var directory = this._parts.path.substring(0, e);
1464
+ var replace = new RegExp('^' + escapeRegEx(directory));
1465
+
1466
+ // fully qualifier directories begin with a slash
1467
+ if (!this.is('relative')) {
1468
+ if (!v) {
1469
+ v = '/';
1470
+ }
1471
+
1472
+ if (v.charAt(0) !== '/') {
1473
+ v = '/' + v;
1474
+ }
1475
+ }
1476
+
1477
+ // directories always end with a slash
1478
+ if (v && v.charAt(v.length - 1) !== '/') {
1479
+ v += '/';
1480
+ }
1481
+
1482
+ v = URI.recodePath(v);
1483
+ this._parts.path = this._parts.path.replace(replace, v);
1484
+ this.build(!build);
1485
+ return this;
1486
+ }
1487
+ };
1488
+ p.filename = function(v, build) {
1489
+ if (this._parts.urn) {
1490
+ return v === undefined ? '' : this;
1491
+ }
1492
+
1493
+ if (v === undefined || v === true) {
1494
+ if (!this._parts.path || this._parts.path === '/') {
1495
+ return '';
1496
+ }
1497
+
1498
+ var pos = this._parts.path.lastIndexOf('/');
1499
+ var res = this._parts.path.substring(pos+1);
1500
+
1501
+ return v ? URI.decodePathSegment(res) : res;
1502
+ } else {
1503
+ var mutatedDirectory = false;
1504
+
1505
+ if (v.charAt(0) === '/') {
1506
+ v = v.substring(1);
1507
+ }
1508
+
1509
+ if (v.match(/\.?\//)) {
1510
+ mutatedDirectory = true;
1511
+ }
1512
+
1513
+ var replace = new RegExp(escapeRegEx(this.filename()) + '$');
1514
+ v = URI.recodePath(v);
1515
+ this._parts.path = this._parts.path.replace(replace, v);
1516
+
1517
+ if (mutatedDirectory) {
1518
+ this.normalizePath(build);
1519
+ } else {
1520
+ this.build(!build);
1521
+ }
1522
+
1523
+ return this;
1524
+ }
1525
+ };
1526
+ p.suffix = function(v, build) {
1527
+ if (this._parts.urn) {
1528
+ return v === undefined ? '' : this;
1529
+ }
1530
+
1531
+ if (v === undefined || v === true) {
1532
+ if (!this._parts.path || this._parts.path === '/') {
1533
+ return '';
1534
+ }
1535
+
1536
+ var filename = this.filename();
1537
+ var pos = filename.lastIndexOf('.');
1538
+ var s, res;
1539
+
1540
+ if (pos === -1) {
1541
+ return '';
1542
+ }
1543
+
1544
+ // suffix may only contain alnum characters (yup, I made this up.)
1545
+ s = filename.substring(pos+1);
1546
+ res = (/^[a-z0-9%]+$/i).test(s) ? s : '';
1547
+ return v ? URI.decodePathSegment(res) : res;
1548
+ } else {
1549
+ if (v.charAt(0) === '.') {
1550
+ v = v.substring(1);
1551
+ }
1552
+
1553
+ var suffix = this.suffix();
1554
+ var replace;
1555
+
1556
+ if (!suffix) {
1557
+ if (!v) {
1558
+ return this;
1559
+ }
1560
+
1561
+ this._parts.path += '.' + URI.recodePath(v);
1562
+ } else if (!v) {
1563
+ replace = new RegExp(escapeRegEx('.' + suffix) + '$');
1564
+ } else {
1565
+ replace = new RegExp(escapeRegEx(suffix) + '$');
1566
+ }
1567
+
1568
+ if (replace) {
1569
+ v = URI.recodePath(v);
1570
+ this._parts.path = this._parts.path.replace(replace, v);
1571
+ }
1572
+
1573
+ this.build(!build);
1574
+ return this;
1575
+ }
1576
+ };
1577
+ p.segment = function(segment, v, build) {
1578
+ var separator = this._parts.urn ? ':' : '/';
1579
+ var path = this.path();
1580
+ var absolute = path.substring(0, 1) === '/';
1581
+ var segments = path.split(separator);
1582
+
1583
+ if (segment !== undefined && typeof segment !== 'number') {
1584
+ build = v;
1585
+ v = segment;
1586
+ segment = undefined;
1587
+ }
1588
+
1589
+ if (segment !== undefined && typeof segment !== 'number') {
1590
+ throw new Error('Bad segment "' + segment + '", must be 0-based integer');
1591
+ }
1592
+
1593
+ if (absolute) {
1594
+ segments.shift();
1595
+ }
1596
+
1597
+ if (segment < 0) {
1598
+ // allow negative indexes to address from the end
1599
+ segment = Math.max(segments.length + segment, 0);
1600
+ }
1601
+
1602
+ if (v === undefined) {
1603
+ /*jshint laxbreak: true */
1604
+ return segment === undefined
1605
+ ? segments
1606
+ : segments[segment];
1607
+ /*jshint laxbreak: false */
1608
+ } else if (segment === null || segments[segment] === undefined) {
1609
+ if (isArray(v)) {
1610
+ segments = [];
1611
+ // collapse empty elements within array
1612
+ for (var i=0, l=v.length; i < l; i++) {
1613
+ if (!v[i].length && (!segments.length || !segments[segments.length -1].length)) {
1614
+ continue;
1615
+ }
1616
+
1617
+ if (segments.length && !segments[segments.length -1].length) {
1618
+ segments.pop();
1619
+ }
1620
+
1621
+ segments.push(trimSlashes(v[i]));
1622
+ }
1623
+ } else if (v || typeof v === 'string') {
1624
+ v = trimSlashes(v);
1625
+ if (segments[segments.length -1] === '') {
1626
+ // empty trailing elements have to be overwritten
1627
+ // to prevent results such as /foo//bar
1628
+ segments[segments.length -1] = v;
1629
+ } else {
1630
+ segments.push(v);
1631
+ }
1632
+ }
1633
+ } else {
1634
+ if (v) {
1635
+ segments[segment] = trimSlashes(v);
1636
+ } else {
1637
+ segments.splice(segment, 1);
1638
+ }
1639
+ }
1640
+
1641
+ if (absolute) {
1642
+ segments.unshift('');
1643
+ }
1644
+
1645
+ return this.path(segments.join(separator), build);
1646
+ };
1647
+ p.segmentCoded = function(segment, v, build) {
1648
+ var segments, i, l;
1649
+
1650
+ if (typeof segment !== 'number') {
1651
+ build = v;
1652
+ v = segment;
1653
+ segment = undefined;
1654
+ }
1655
+
1656
+ if (v === undefined) {
1657
+ segments = this.segment(segment, v, build);
1658
+ if (!isArray(segments)) {
1659
+ segments = segments !== undefined ? URI.decode(segments) : undefined;
1660
+ } else {
1661
+ for (i = 0, l = segments.length; i < l; i++) {
1662
+ segments[i] = URI.decode(segments[i]);
1663
+ }
1664
+ }
1665
+
1666
+ return segments;
1667
+ }
1668
+
1669
+ if (!isArray(v)) {
1670
+ v = (typeof v === 'string' || v instanceof String) ? URI.encode(v) : v;
1671
+ } else {
1672
+ for (i = 0, l = v.length; i < l; i++) {
1673
+ v[i] = URI.encode(v[i]);
1674
+ }
1675
+ }
1676
+
1677
+ return this.segment(segment, v, build);
1678
+ };
1679
+
1680
+ // mutating query string
1681
+ var q = p.query;
1682
+ p.query = function(v, build) {
1683
+ if (v === true) {
1684
+ return URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1685
+ } else if (typeof v === 'function') {
1686
+ var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1687
+ var result = v.call(this, data);
1688
+ this._parts.query = URI.buildQuery(result || data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1689
+ this.build(!build);
1690
+ return this;
1691
+ } else if (v !== undefined && typeof v !== 'string') {
1692
+ this._parts.query = URI.buildQuery(v, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1693
+ this.build(!build);
1694
+ return this;
1695
+ } else {
1696
+ return q.call(this, v, build);
1697
+ }
1698
+ };
1699
+ p.setQuery = function(name, value, build) {
1700
+ var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1701
+
1702
+ if (typeof name === 'string' || name instanceof String) {
1703
+ data[name] = value !== undefined ? value : null;
1704
+ } else if (typeof name === 'object') {
1705
+ for (var key in name) {
1706
+ if (hasOwn.call(name, key)) {
1707
+ data[key] = name[key];
1708
+ }
1709
+ }
1710
+ } else {
1711
+ throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
1712
+ }
1713
+
1714
+ this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1715
+ if (typeof name !== 'string') {
1716
+ build = value;
1717
+ }
1718
+
1719
+ this.build(!build);
1720
+ return this;
1721
+ };
1722
+ p.addQuery = function(name, value, build) {
1723
+ var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1724
+ URI.addQuery(data, name, value === undefined ? null : value);
1725
+ this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1726
+ if (typeof name !== 'string') {
1727
+ build = value;
1728
+ }
1729
+
1730
+ this.build(!build);
1731
+ return this;
1732
+ };
1733
+ p.removeQuery = function(name, value, build) {
1734
+ var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1735
+ URI.removeQuery(data, name, value);
1736
+ this._parts.query = URI.buildQuery(data, this._parts.duplicateQueryParameters, this._parts.escapeQuerySpace);
1737
+ if (typeof name !== 'string') {
1738
+ build = value;
1739
+ }
1740
+
1741
+ this.build(!build);
1742
+ return this;
1743
+ };
1744
+ p.hasQuery = function(name, value, withinArray) {
1745
+ var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);
1746
+ return URI.hasQuery(data, name, value, withinArray);
1747
+ };
1748
+ p.setSearch = p.setQuery;
1749
+ p.addSearch = p.addQuery;
1750
+ p.removeSearch = p.removeQuery;
1751
+ p.hasSearch = p.hasQuery;
1752
+
1753
+ // sanitizing URLs
1754
+ p.normalize = function() {
1755
+ if (this._parts.urn) {
1756
+ return this
1757
+ .normalizeProtocol(false)
1758
+ .normalizePath(false)
1759
+ .normalizeQuery(false)
1760
+ .normalizeFragment(false)
1761
+ .build();
1762
+ }
1763
+
1764
+ return this
1765
+ .normalizeProtocol(false)
1766
+ .normalizeHostname(false)
1767
+ .normalizePort(false)
1768
+ .normalizePath(false)
1769
+ .normalizeQuery(false)
1770
+ .normalizeFragment(false)
1771
+ .build();
1772
+ };
1773
+ p.normalizeProtocol = function(build) {
1774
+ if (typeof this._parts.protocol === 'string') {
1775
+ this._parts.protocol = this._parts.protocol.toLowerCase();
1776
+ this.build(!build);
1777
+ }
1778
+
1779
+ return this;
1780
+ };
1781
+ p.normalizeHostname = function(build) {
1782
+ if (this._parts.hostname) {
1783
+ if (this.is('IDN') && punycode) {
1784
+ this._parts.hostname = punycode.toASCII(this._parts.hostname);
1785
+ } else if (this.is('IPv6') && IPv6) {
1786
+ this._parts.hostname = IPv6.best(this._parts.hostname);
1787
+ }
1788
+
1789
+ this._parts.hostname = this._parts.hostname.toLowerCase();
1790
+ this.build(!build);
1791
+ }
1792
+
1793
+ return this;
1794
+ };
1795
+ p.normalizePort = function(build) {
1796
+ // remove port of it's the protocol's default
1797
+ if (typeof this._parts.protocol === 'string' && this._parts.port === URI.defaultPorts[this._parts.protocol]) {
1798
+ this._parts.port = null;
1799
+ this.build(!build);
1800
+ }
1801
+
1802
+ return this;
1803
+ };
1804
+ p.normalizePath = function(build) {
1805
+ var _path = this._parts.path;
1806
+ if (!_path) {
1807
+ return this;
1808
+ }
1809
+
1810
+ if (this._parts.urn) {
1811
+ this._parts.path = URI.recodeUrnPath(this._parts.path);
1812
+ this.build(!build);
1813
+ return this;
1814
+ }
1815
+
1816
+ if (this._parts.path === '/') {
1817
+ return this;
1818
+ }
1819
+
1820
+ var _was_relative;
1821
+ var _leadingParents = '';
1822
+ var _parent, _pos;
1823
+
1824
+ // handle relative paths
1825
+ if (_path.charAt(0) !== '/') {
1826
+ _was_relative = true;
1827
+ _path = '/' + _path;
1828
+ }
1829
+
1830
+ // handle relative files (as opposed to directories)
1831
+ if (_path.slice(-3) === '/..' || _path.slice(-2) === '/.') {
1832
+ _path += '/';
1833
+ }
1834
+
1835
+ // resolve simples
1836
+ _path = _path
1837
+ .replace(/(\/(\.\/)+)|(\/\.$)/g, '/')
1838
+ .replace(/\/{2,}/g, '/');
1839
+
1840
+ // remember leading parents
1841
+ if (_was_relative) {
1842
+ _leadingParents = _path.substring(1).match(/^(\.\.\/)+/) || '';
1843
+ if (_leadingParents) {
1844
+ _leadingParents = _leadingParents[0];
1845
+ }
1846
+ }
1847
+
1848
+ // resolve parents
1849
+ while (true) {
1850
+ _parent = _path.indexOf('/..');
1851
+ if (_parent === -1) {
1852
+ // no more ../ to resolve
1853
+ break;
1854
+ } else if (_parent === 0) {
1855
+ // top level cannot be relative, skip it
1856
+ _path = _path.substring(3);
1857
+ continue;
1858
+ }
1859
+
1860
+ _pos = _path.substring(0, _parent).lastIndexOf('/');
1861
+ if (_pos === -1) {
1862
+ _pos = _parent;
1863
+ }
1864
+ _path = _path.substring(0, _pos) + _path.substring(_parent + 3);
1865
+ }
1866
+
1867
+ // revert to relative
1868
+ if (_was_relative && this.is('relative')) {
1869
+ _path = _leadingParents + _path.substring(1);
1870
+ }
1871
+
1872
+ _path = URI.recodePath(_path);
1873
+ this._parts.path = _path;
1874
+ this.build(!build);
1875
+ return this;
1876
+ };
1877
+ p.normalizePathname = p.normalizePath;
1878
+ p.normalizeQuery = function(build) {
1879
+ if (typeof this._parts.query === 'string') {
1880
+ if (!this._parts.query.length) {
1881
+ this._parts.query = null;
1882
+ } else {
1883
+ this.query(URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace));
1884
+ }
1885
+
1886
+ this.build(!build);
1887
+ }
1888
+
1889
+ return this;
1890
+ };
1891
+ p.normalizeFragment = function(build) {
1892
+ if (!this._parts.fragment) {
1893
+ this._parts.fragment = null;
1894
+ this.build(!build);
1895
+ }
1896
+
1897
+ return this;
1898
+ };
1899
+ p.normalizeSearch = p.normalizeQuery;
1900
+ p.normalizeHash = p.normalizeFragment;
1901
+
1902
+ p.iso8859 = function() {
1903
+ // expect unicode input, iso8859 output
1904
+ var e = URI.encode;
1905
+ var d = URI.decode;
1906
+
1907
+ URI.encode = escape;
1908
+ URI.decode = decodeURIComponent;
1909
+ try {
1910
+ this.normalize();
1911
+ } finally {
1912
+ URI.encode = e;
1913
+ URI.decode = d;
1914
+ }
1915
+ return this;
1916
+ };
1917
+
1918
+ p.unicode = function() {
1919
+ // expect iso8859 input, unicode output
1920
+ var e = URI.encode;
1921
+ var d = URI.decode;
1922
+
1923
+ URI.encode = strictEncodeURIComponent;
1924
+ URI.decode = unescape;
1925
+ try {
1926
+ this.normalize();
1927
+ } finally {
1928
+ URI.encode = e;
1929
+ URI.decode = d;
1930
+ }
1931
+ return this;
1932
+ };
1933
+
1934
+ p.readable = function() {
1935
+ var uri = this.clone();
1936
+ // removing username, password, because they shouldn't be displayed according to RFC 3986
1937
+ uri.username('').password('').normalize();
1938
+ var t = '';
1939
+ if (uri._parts.protocol) {
1940
+ t += uri._parts.protocol + '://';
1941
+ }
1942
+
1943
+ if (uri._parts.hostname) {
1944
+ if (uri.is('punycode') && punycode) {
1945
+ t += punycode.toUnicode(uri._parts.hostname);
1946
+ if (uri._parts.port) {
1947
+ t += ':' + uri._parts.port;
1948
+ }
1949
+ } else {
1950
+ t += uri.host();
1951
+ }
1952
+ }
1953
+
1954
+ if (uri._parts.hostname && uri._parts.path && uri._parts.path.charAt(0) !== '/') {
1955
+ t += '/';
1956
+ }
1957
+
1958
+ t += uri.path(true);
1959
+ if (uri._parts.query) {
1960
+ var q = '';
1961
+ for (var i = 0, qp = uri._parts.query.split('&'), l = qp.length; i < l; i++) {
1962
+ var kv = (qp[i] || '').split('=');
1963
+ q += '&' + URI.decodeQuery(kv[0], this._parts.escapeQuerySpace)
1964
+ .replace(/&/g, '%26');
1965
+
1966
+ if (kv[1] !== undefined) {
1967
+ q += '=' + URI.decodeQuery(kv[1], this._parts.escapeQuerySpace)
1968
+ .replace(/&/g, '%26');
1969
+ }
1970
+ }
1971
+ t += '?' + q.substring(1);
1972
+ }
1973
+
1974
+ t += URI.decodeQuery(uri.hash(), true);
1975
+ return t;
1976
+ };
1977
+
1978
+ // resolving relative and absolute URLs
1979
+ p.absoluteTo = function(base) {
1980
+ var resolved = this.clone();
1981
+ var properties = ['protocol', 'username', 'password', 'hostname', 'port'];
1982
+ var basedir, i, p;
1983
+
1984
+ if (this._parts.urn) {
1985
+ throw new Error('URNs do not have any generally defined hierarchical components');
1986
+ }
1987
+
1988
+ if (!(base instanceof URI)) {
1989
+ base = new URI(base);
1990
+ }
1991
+
1992
+ if (!resolved._parts.protocol) {
1993
+ resolved._parts.protocol = base._parts.protocol;
1994
+ }
1995
+
1996
+ if (this._parts.hostname) {
1997
+ return resolved;
1998
+ }
1999
+
2000
+ for (i = 0; (p = properties[i]); i++) {
2001
+ resolved._parts[p] = base._parts[p];
2002
+ }
2003
+
2004
+ if (!resolved._parts.path) {
2005
+ resolved._parts.path = base._parts.path;
2006
+ if (!resolved._parts.query) {
2007
+ resolved._parts.query = base._parts.query;
2008
+ }
2009
+ } else if (resolved._parts.path.substring(-2) === '..') {
2010
+ resolved._parts.path += '/';
2011
+ }
2012
+
2013
+ if (resolved.path().charAt(0) !== '/') {
2014
+ basedir = base.directory();
2015
+ basedir = basedir ? basedir : base.path().indexOf('/') === 0 ? '/' : '';
2016
+ resolved._parts.path = (basedir ? (basedir + '/') : '') + resolved._parts.path;
2017
+ resolved.normalizePath();
2018
+ }
2019
+
2020
+ resolved.build();
2021
+ return resolved;
2022
+ };
2023
+ p.relativeTo = function(base) {
2024
+ var relative = this.clone().normalize();
2025
+ var relativeParts, baseParts, common, relativePath, basePath;
2026
+
2027
+ if (relative._parts.urn) {
2028
+ throw new Error('URNs do not have any generally defined hierarchical components');
2029
+ }
2030
+
2031
+ base = new URI(base).normalize();
2032
+ relativeParts = relative._parts;
2033
+ baseParts = base._parts;
2034
+ relativePath = relative.path();
2035
+ basePath = base.path();
2036
+
2037
+ if (relativePath.charAt(0) !== '/') {
2038
+ throw new Error('URI is already relative');
2039
+ }
2040
+
2041
+ if (basePath.charAt(0) !== '/') {
2042
+ throw new Error('Cannot calculate a URI relative to another relative URI');
2043
+ }
2044
+
2045
+ if (relativeParts.protocol === baseParts.protocol) {
2046
+ relativeParts.protocol = null;
2047
+ }
2048
+
2049
+ if (relativeParts.username !== baseParts.username || relativeParts.password !== baseParts.password) {
2050
+ return relative.build();
2051
+ }
2052
+
2053
+ if (relativeParts.protocol !== null || relativeParts.username !== null || relativeParts.password !== null) {
2054
+ return relative.build();
2055
+ }
2056
+
2057
+ if (relativeParts.hostname === baseParts.hostname && relativeParts.port === baseParts.port) {
2058
+ relativeParts.hostname = null;
2059
+ relativeParts.port = null;
2060
+ } else {
2061
+ return relative.build();
2062
+ }
2063
+
2064
+ if (relativePath === basePath) {
2065
+ relativeParts.path = '';
2066
+ return relative.build();
2067
+ }
2068
+
2069
+ // determine common sub path
2070
+ common = URI.commonPath(relativePath, basePath);
2071
+
2072
+ // If the paths have nothing in common, return a relative URL with the absolute path.
2073
+ if (!common) {
2074
+ return relative.build();
2075
+ }
2076
+
2077
+ var parents = baseParts.path
2078
+ .substring(common.length)
2079
+ .replace(/[^\/]*$/, '')
2080
+ .replace(/.*?\//g, '../');
2081
+
2082
+ relativeParts.path = (parents + relativeParts.path.substring(common.length)) || './';
2083
+
2084
+ return relative.build();
2085
+ };
2086
+
2087
+ // comparing URIs
2088
+ p.equals = function(uri) {
2089
+ var one = this.clone();
2090
+ var two = new URI(uri);
2091
+ var one_map = {};
2092
+ var two_map = {};
2093
+ var checked = {};
2094
+ var one_query, two_query, key;
2095
+
2096
+ one.normalize();
2097
+ two.normalize();
2098
+
2099
+ // exact match
2100
+ if (one.toString() === two.toString()) {
2101
+ return true;
2102
+ }
2103
+
2104
+ // extract query string
2105
+ one_query = one.query();
2106
+ two_query = two.query();
2107
+ one.query('');
2108
+ two.query('');
2109
+
2110
+ // definitely not equal if not even non-query parts match
2111
+ if (one.toString() !== two.toString()) {
2112
+ return false;
2113
+ }
2114
+
2115
+ // query parameters have the same length, even if they're permuted
2116
+ if (one_query.length !== two_query.length) {
2117
+ return false;
2118
+ }
2119
+
2120
+ one_map = URI.parseQuery(one_query, this._parts.escapeQuerySpace);
2121
+ two_map = URI.parseQuery(two_query, this._parts.escapeQuerySpace);
2122
+
2123
+ for (key in one_map) {
2124
+ if (hasOwn.call(one_map, key)) {
2125
+ if (!isArray(one_map[key])) {
2126
+ if (one_map[key] !== two_map[key]) {
2127
+ return false;
2128
+ }
2129
+ } else if (!arraysEqual(one_map[key], two_map[key])) {
2130
+ return false;
2131
+ }
2132
+
2133
+ checked[key] = true;
2134
+ }
2135
+ }
2136
+
2137
+ for (key in two_map) {
2138
+ if (hasOwn.call(two_map, key)) {
2139
+ if (!checked[key]) {
2140
+ // two contains a parameter not present in one
2141
+ return false;
2142
+ }
2143
+ }
2144
+ }
2145
+
2146
+ return true;
2147
+ };
2148
+
2149
+ // state
2150
+ p.duplicateQueryParameters = function(v) {
2151
+ this._parts.duplicateQueryParameters = !!v;
2152
+ return this;
2153
+ };
2154
+
2155
+ p.escapeQuerySpace = function(v) {
2156
+ this._parts.escapeQuerySpace = !!v;
2157
+ return this;
2158
+ };
2159
+
2160
+ return URI;
2161
+ }));