kea-rails 1.0.1 → 1.0.2

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