mw.Uri: Add support for array parameters with explicit indexes
[lhc/web/wiklou.git] / resources / src / mediawiki.Uri / Uri.js
1 /**
2 * Library for simple URI parsing and manipulation.
3 *
4 * Intended to be minimal, but featureful; do not expect full RFC 3986 compliance. The use cases we
5 * have in mind are constructing 'next page' or 'previous page' URLs, detecting whether we need to
6 * use cross-domain proxies for an API, constructing simple URL-based API calls, etc. Parsing here
7 * is regex-based, so may not work on all URIs, but is good enough for most.
8 *
9 * You can modify the properties directly, then use the #toString method to extract the full URI
10 * string again. Example:
11 *
12 * var uri = new mw.Uri( 'http://example.com/mysite/mypage.php?quux=2' );
13 *
14 * if ( uri.host == 'example.com' ) {
15 * uri.host = 'foo.example.com';
16 * uri.extend( { bar: 1 } );
17 *
18 * $( 'a#id1' ).attr( 'href', uri );
19 * // anchor with id 'id1' now links to http://foo.example.com/mysite/mypage.php?bar=1&quux=2
20 *
21 * $( 'a#id2' ).attr( 'href', uri.clone().extend( { bar: 3, pif: 'paf' } ) );
22 * // anchor with id 'id2' now links to http://foo.example.com/mysite/mypage.php?bar=3&quux=2&pif=paf
23 * }
24 *
25 * Given a URI like
26 * `http://usr:pwd@www.example.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=&test3=value+%28escaped%29&r=1&r=2#top`
27 * the returned object will have the following properties:
28 *
29 * protocol 'http'
30 * user 'usr'
31 * password 'pwd'
32 * host 'www.example.com'
33 * port '81'
34 * path '/dir/dir.2/index.htm'
35 * query {
36 * q1: '0',
37 * test1: null,
38 * test2: '',
39 * test3: 'value (escaped)'
40 * r: ['1', '2']
41 * }
42 * fragment 'top'
43 *
44 * (N.b., 'password' is technically not allowed for HTTP URIs, but it is possible with other kinds
45 * of URIs.)
46 *
47 * Parsing based on parseUri 1.2.2 (c) Steven Levithan <http://stevenlevithan.com>, MIT License.
48 * <http://stevenlevithan.com/demo/parseuri/js/>
49 *
50 * @class mw.Uri
51 */
52
53 ( function () {
54 var parser, properties;
55
56 /**
57 * Function that's useful when constructing the URI string -- we frequently encounter the pattern
58 * of having to add something to the URI as we go, but only if it's present, and to include a
59 * character before or after if so.
60 *
61 * @private
62 * @static
63 * @param {string|undefined} pre To prepend
64 * @param {string} val To include
65 * @param {string} post To append
66 * @param {boolean} raw If true, val will not be encoded
67 * @return {string} Result
68 */
69 function cat( pre, val, post, raw ) {
70 if ( val === undefined || val === null || val === '' ) {
71 return '';
72 }
73
74 return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
75 }
76
77 /**
78 * Regular expressions to parse many common URIs.
79 *
80 * As they are gnarly, they have been moved to separate files to allow us to format them in the
81 * 'extended' regular expression format (which JavaScript normally doesn't support). The subset of
82 * features handled is minimal, but just the free whitespace gives us a lot.
83 *
84 * @private
85 * @static
86 * @property {Object} parser
87 */
88 parser = {
89 strict: mw.template.get( 'mediawiki.Uri', 'strict.regexp' ).render(),
90 loose: mw.template.get( 'mediawiki.Uri', 'loose.regexp' ).render()
91 };
92
93 /**
94 * The order here matches the order of captured matches in the `parser` property regexes.
95 *
96 * @private
97 * @static
98 * @property {string[]} properties
99 */
100 properties = [
101 'protocol',
102 'user',
103 'password',
104 'host',
105 'port',
106 'path',
107 'query',
108 'fragment'
109 ];
110
111 /**
112 * @property {string} protocol For example `http` (always present)
113 */
114 /**
115 * @property {string|undefined} user For example `usr`
116 */
117 /**
118 * @property {string|undefined} password For example `pwd`
119 */
120 /**
121 * @property {string} host For example `www.example.com` (always present)
122 */
123 /**
124 * @property {string|undefined} port For example `81`
125 */
126 /**
127 * @property {string} path For example `/dir/dir.2/index.htm` (always present)
128 */
129 /**
130 * @property {Object} query For example `{ a: '0', b: '', c: 'value' }` (always present)
131 */
132 /**
133 * @property {string|undefined} fragment For example `top`
134 */
135
136 /**
137 * A factory method to create an mw.Uri class with a default location to resolve relative URLs
138 * against (including protocol-relative URLs).
139 *
140 * @method
141 * @param {string|Function} documentLocation A full url, or function returning one.
142 * If passed a function, the return value may change over time and this will be honoured. (T74334)
143 * @member mw
144 * @return {Function} An mw.Uri class constructor
145 */
146 mw.UriRelative = function ( documentLocation ) {
147 var getDefaultUri = ( function () {
148 // Cache
149 var href, uri;
150
151 return function () {
152 var hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
153 if ( href === hrefCur ) {
154 return uri;
155 }
156 href = hrefCur;
157 // eslint-disable-next-line no-use-before-define
158 uri = new Uri( href );
159 return uri;
160 };
161 }() );
162
163 /**
164 * Construct a new URI object. Throws error if arguments are illegal/impossible, or
165 * otherwise don't parse.
166 *
167 * @class mw.Uri
168 * @constructor
169 * @param {Object|string} [uri] URI string, or an Object with appropriate properties (especially
170 * another URI object to clone). Object must have non-blank `protocol`, `host`, and `path`
171 * properties. If omitted (or set to `undefined`, `null` or empty string), then an object
172 * will be created for the default `uri` of this constructor (`location.href` for mw.Uri,
173 * other values for other instances -- see mw.UriRelative for details).
174 * @param {Object|boolean} [options] Object with options, or (backwards compatibility) a boolean
175 * for strictMode
176 * @param {boolean} [options.strictMode=false] Trigger strict mode parsing of the url.
177 * @param {boolean} [options.overrideKeys=false] Whether to let duplicate query parameters
178 * override each other (`true`) or automagically convert them to an array (`false`).
179 * @param {boolean} [options.arrayParams=false] Whether to parse array query parameters (e.g.
180 * `&foo[0]=a&foo[1]=b` or `&foo[]=a&foo[]=b`) or leave them alone. Currently this does not
181 * handle associative or multi-dimensional arrays, but that may be improved in the future.
182 * Implies `overrideKeys: true` (query parameters without `[...]` are not parsed as arrays).
183 * @throws {Error} when the query string or fragment contains an unknown % sequence
184 */
185 function Uri( uri, options ) {
186 var prop, hrefCur,
187 hasOptions = ( options !== undefined ),
188 defaultUri = getDefaultUri();
189
190 options = typeof options === 'object' ? options : { strictMode: !!options };
191 options = $.extend( {
192 strictMode: false,
193 overrideKeys: false,
194 arrayParams: false
195 }, options );
196
197 this.arrayParams = options.arrayParams;
198
199 if ( uri !== undefined && uri !== null && uri !== '' ) {
200 if ( typeof uri === 'string' ) {
201 this.parse( uri, options );
202 } else if ( typeof uri === 'object' ) {
203 // Copy data over from existing URI object
204 for ( prop in uri ) {
205 // Only copy direct properties, not inherited ones
206 if ( Object.prototype.hasOwnProperty.call( uri, prop ) ) {
207 // Deep copy object properties
208 if ( Array.isArray( uri[ prop ] ) || $.isPlainObject( uri[ prop ] ) ) {
209 this[ prop ] = $.extend( true, {}, uri[ prop ] );
210 } else {
211 this[ prop ] = uri[ prop ];
212 }
213 }
214 }
215 if ( !this.query ) {
216 this.query = {};
217 }
218 }
219 } else if ( hasOptions ) {
220 // We didn't get a URI in the constructor, but we got options.
221 hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
222 this.parse( hrefCur, options );
223 } else {
224 // We didn't get a URI or options in the constructor, use the default instance.
225 return defaultUri.clone();
226 }
227
228 // protocol-relative URLs
229 if ( !this.protocol ) {
230 this.protocol = defaultUri.protocol;
231 }
232 // No host given:
233 if ( !this.host ) {
234 this.host = defaultUri.host;
235 // port ?
236 if ( !this.port ) {
237 this.port = defaultUri.port;
238 }
239 }
240 if ( this.path && this.path[ 0 ] !== '/' ) {
241 // A real relative URL, relative to defaultUri.path. We can't really handle that since we cannot
242 // figure out whether the last path component of defaultUri.path is a directory or a file.
243 throw new Error( 'Bad constructor arguments' );
244 }
245 if ( !( this.protocol && this.host && this.path ) ) {
246 throw new Error( 'Bad constructor arguments' );
247 }
248 }
249
250 /**
251 * Encode a value for inclusion in a url.
252 *
253 * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more
254 * compliant with RFC 3986. Similar to rawurlencode from PHP and our JS library
255 * mw.util.rawurlencode, except this also replaces spaces with `+`.
256 *
257 * @static
258 * @param {string} s String to encode
259 * @return {string} Encoded string for URI
260 */
261 Uri.encode = function ( s ) {
262 return encodeURIComponent( s )
263 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
264 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' )
265 .replace( /%20/g, '+' );
266 };
267
268 /**
269 * Decode a url encoded value.
270 *
271 * Reversed #encode. Standard decodeURIComponent, with addition of replacing
272 * `+` with a space.
273 *
274 * @static
275 * @param {string} s String to decode
276 * @return {string} Decoded string
277 * @throws {Error} when the string contains an unknown % sequence
278 */
279 Uri.decode = function ( s ) {
280 return decodeURIComponent( s.replace( /\+/g, '%20' ) );
281 };
282
283 Uri.prototype = {
284
285 /**
286 * Parse a string and set our properties accordingly.
287 *
288 * @private
289 * @param {string} str URI, see constructor.
290 * @param {Object} options See constructor.
291 * @throws {Error} when the query string or fragment contains an unknown % sequence
292 */
293 parse: function ( str, options ) {
294 var q, matches,
295 uri = this,
296 hasOwn = Object.prototype.hasOwnProperty;
297
298 // Apply parser regex and set all properties based on the result
299 matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
300 properties.forEach( function ( property, i ) {
301 uri[ property ] = matches[ i + 1 ];
302 } );
303
304 // uri.query starts out as the query string; we will parse it into key-val pairs then make
305 // that object the "query" property.
306 // we overwrite query in uri way to make cloning easier, it can use the same list of properties.
307 q = {};
308 // using replace to iterate over a string
309 if ( uri.query ) {
310 uri.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, function ( match, k, eq, v ) {
311 var arrayKeyMatch, i;
312 if ( k ) {
313 k = Uri.decode( k );
314 v = ( eq === '' || eq === undefined ) ? null : Uri.decode( v );
315 arrayKeyMatch = k.match( /^([^[]+)\[(\d*)\]$/ );
316
317 // If arrayParams and this parameter name contains an array index...
318 if ( options.arrayParams && arrayKeyMatch ) {
319 // Remove the index from parameter name
320 k = arrayKeyMatch[ 1 ];
321
322 // Turn the parameter value into an array (throw away anything else)
323 if ( !Array.isArray( q[ k ] ) ) {
324 q[ k ] = [];
325 }
326
327 i = arrayKeyMatch[ 2 ];
328 if ( i === '' ) {
329 // If no explicit index, append at the end
330 i = q[ k ].length;
331 }
332
333 q[ k ][ i ] = v;
334
335 // If overrideKeys, always (re)set top level value.
336 // If not overrideKeys but this key wasn't set before, then we set it as well.
337 // arrayParams implies overrideKeys (no array handling for non-array params).
338 } else if ( options.arrayParams || options.overrideKeys || !hasOwn.call( q, k ) ) {
339 q[ k ] = v;
340
341 // Use arrays if overrideKeys is false and key was already seen before
342 } else {
343 // Once before, still a string, turn into an array
344 if ( typeof q[ k ] === 'string' ) {
345 q[ k ] = [ q[ k ] ];
346 }
347 // Add to the array
348 if ( Array.isArray( q[ k ] ) ) {
349 q[ k ].push( v );
350 }
351 }
352 }
353 } );
354 }
355 uri.query = q;
356
357 // Decode uri.fragment, otherwise it gets double-encoded when serializing
358 if ( uri.fragment !== undefined ) {
359 uri.fragment = Uri.decode( uri.fragment );
360 }
361 },
362
363 /**
364 * Get user and password section of a URI.
365 *
366 * @return {string}
367 */
368 getUserInfo: function () {
369 return cat( '', this.user, cat( ':', this.password, '' ) );
370 },
371
372 /**
373 * Get host and port section of a URI.
374 *
375 * @return {string}
376 */
377 getHostPort: function () {
378 return this.host + cat( ':', this.port, '' );
379 },
380
381 /**
382 * Get the userInfo, host and port section of the URI.
383 *
384 * In most real-world URLs this is simply the hostname, but the definition of 'authority' section is more general.
385 *
386 * @return {string}
387 */
388 getAuthority: function () {
389 return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
390 },
391
392 /**
393 * Get the query arguments of the URL, encoded into a string.
394 *
395 * Does not preserve the original order of arguments passed in the URI. Does handle escaping.
396 *
397 * @return {string}
398 */
399 getQueryString: function () {
400 var args = [],
401 arrayParams = this.arrayParams;
402 // eslint-disable-next-line no-jquery/no-each-util
403 $.each( this.query, function ( key, val ) {
404 var k = Uri.encode( key ),
405 isArrayParam = Array.isArray( val ),
406 vals = isArrayParam ? val : [ val ];
407 vals.forEach( function ( v, i ) {
408 var ki = k;
409 if ( arrayParams && isArrayParam ) {
410 ki += Uri.encode( '[' + i + ']' );
411 }
412 if ( v === null ) {
413 args.push( ki );
414 } else if ( k === 'title' ) {
415 args.push( ki + '=' + mw.util.wikiUrlencode( v ) );
416 } else {
417 args.push( ki + '=' + Uri.encode( v ) );
418 }
419 } );
420 } );
421 return args.join( '&' );
422 },
423
424 /**
425 * Get everything after the authority section of the URI.
426 *
427 * @return {string}
428 */
429 getRelativePath: function () {
430 return this.path + cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment, '' );
431 },
432
433 /**
434 * Get the entire URI string.
435 *
436 * May not be precisely the same as input due to order of query arguments.
437 *
438 * @return {string} The URI string
439 */
440 toString: function () {
441 return this.protocol + '://' + this.getAuthority() + this.getRelativePath();
442 },
443
444 /**
445 * Clone this URI
446 *
447 * @return {Object} New URI object with same properties
448 */
449 clone: function () {
450 return new Uri( this );
451 },
452
453 /**
454 * Extend the query section of the URI with new parameters.
455 *
456 * @param {Object} parameters Query parameters to add to ours (or to override ours with) as an
457 * object
458 * @return {Object} This URI object
459 */
460 extend: function ( parameters ) {
461 $.extend( this.query, parameters );
462 return this;
463 }
464 };
465
466 return Uri;
467 };
468
469 // Default to the current browsing location (for relative URLs).
470 mw.Uri = mw.UriRelative( function () {
471 return location.href;
472 } );
473
474 }() );