Merge "Add tests for WikiMap and WikiReference"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.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 ( mw, $ ) {
54 /**
55 * Function that's useful when constructing the URI string -- we frequently encounter the pattern
56 * of having to add something to the URI as we go, but only if it's present, and to include a
57 * character before or after if so.
58 *
59 * @private
60 * @static
61 * @param {string|undefined} pre To prepend
62 * @param {string} val To include
63 * @param {string} post To append
64 * @param {boolean} raw If true, val will not be encoded
65 * @return {string} Result
66 */
67 function cat( pre, val, post, raw ) {
68 if ( val === undefined || val === null || val === '' ) {
69 return '';
70 }
71 /* jshint latedef:false */
72 return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
73 /* jshint latedef:true */
74 }
75
76 /**
77 * Regular expressions to parse many common URIs.
78 *
79 * As they are gnarly, they have been moved to separate files to allow us to format them in the
80 * 'extended' regular expression format (which JavaScript normally doesn't support). The subset of
81 * features handled is minimal, but just the free whitespace gives us a lot.
82 *
83 * @private
84 * @static
85 * @property {Object} parser
86 */
87 var parser = {
88 strict: mw.template.get( 'mediawiki.Uri', 'strict.regexp' ).render(),
89 loose: mw.template.get( 'mediawiki.Uri', 'loose.regexp' ).render()
90 },
91
92 /**
93 * The order here matches the order of captured matches in the `parser` property regexes.
94 *
95 * @private
96 * @static
97 * @property {Array} properties
98 */
99 properties = [
100 'protocol',
101 'user',
102 'password',
103 'host',
104 'port',
105 'path',
106 'query',
107 'fragment'
108 ];
109
110 /**
111 * @property {string} protocol For example `http` (always present)
112 */
113 /**
114 * @property {string|undefined} user For example `usr`
115 */
116 /**
117 * @property {string|undefined} password For example `pwd`
118 */
119 /**
120 * @property {string} host For example `www.example.com` (always present)
121 */
122 /**
123 * @property {string|undefined} port For example `81`
124 */
125 /**
126 * @property {string} path For example `/dir/dir.2/index.htm` (always present)
127 */
128 /**
129 * @property {Object} query For example `{ a: '0', b: '', c: 'value' }` (always present)
130 */
131 /**
132 * @property {string|undefined} fragment For example `top`
133 */
134
135 /**
136 * A factory method to create a Uri class with a default location to resolve relative URLs
137 * against (including protocol-relative URLs).
138 *
139 * @method
140 * @param {string|Function} documentLocation A full url, or function returning one.
141 * If passed a function, the return value may change over time and this will be honoured. (T74334)
142 * @member mw
143 */
144 mw.UriRelative = function ( documentLocation ) {
145 var getDefaultUri = ( function () {
146 // Cache
147 var href, uri;
148
149 return function () {
150 var hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
151 if ( href === hrefCur ) {
152 return uri;
153 }
154 href = hrefCur;
155 uri = new Uri( href );
156 return uri;
157 };
158 }() );
159
160 /**
161 * @class mw.Uri
162 * @constructor
163 *
164 * Construct a new URI object. Throws error if arguments are illegal/impossible, or
165 * otherwise don't parse.
166 *
167 * @param {Object|string} [uri] URI string, or an Object with appropriate properties (especially
168 * another URI object to clone). Object must have non-blank `protocol`, `host`, and `path`
169 * properties. If omitted (or set to `undefined`, `null` or empty string), then an object
170 * will be created for the default `uri` of this constructor (`location.href` for mw.Uri,
171 * other values for other instances -- see mw.UriRelative for details).
172 * @param {Object|boolean} [options] Object with options, or (backwards compatibility) a boolean
173 * for strictMode
174 * @param {boolean} [options.strictMode=false] Trigger strict mode parsing of the url.
175 * @param {boolean} [options.overrideKeys=false] Whether to let duplicate query parameters
176 * override each other (`true`) or automagically convert them to an array (`false`).
177 */
178 /* jshint latedef:false */
179 function Uri( uri, options ) {
180 var prop,
181 defaultUri = getDefaultUri();
182
183 options = typeof options === 'object' ? options : { strictMode: !!options };
184 options = $.extend( {
185 strictMode: false,
186 overrideKeys: false
187 }, options );
188
189 if ( uri !== undefined && uri !== null && uri !== '' ) {
190 if ( typeof uri === 'string' ) {
191 this.parse( uri, options );
192 } else if ( typeof uri === 'object' ) {
193 // Copy data over from existing URI object
194 for ( prop in uri ) {
195 // Only copy direct properties, not inherited ones
196 if ( uri.hasOwnProperty( prop ) ) {
197 // Deep copy object properties
198 if ( $.isArray( uri[ prop ] ) || $.isPlainObject( uri[ prop ] ) ) {
199 this[ prop ] = $.extend( true, {}, uri[ prop ] );
200 } else {
201 this[ prop ] = uri[ prop ];
202 }
203 }
204 }
205 if ( !this.query ) {
206 this.query = {};
207 }
208 }
209 } else {
210 // If we didn't get a URI in the constructor, use the default one.
211 return defaultUri.clone();
212 }
213
214 // protocol-relative URLs
215 if ( !this.protocol ) {
216 this.protocol = defaultUri.protocol;
217 }
218 // No host given:
219 if ( !this.host ) {
220 this.host = defaultUri.host;
221 // port ?
222 if ( !this.port ) {
223 this.port = defaultUri.port;
224 }
225 }
226 if ( this.path && this.path[ 0 ] !== '/' ) {
227 // A real relative URL, relative to defaultUri.path. We can't really handle that since we cannot
228 // figure out whether the last path component of defaultUri.path is a directory or a file.
229 throw new Error( 'Bad constructor arguments' );
230 }
231 if ( !( this.protocol && this.host && this.path ) ) {
232 throw new Error( 'Bad constructor arguments' );
233 }
234 }
235
236 /**
237 * Encode a value for inclusion in a url.
238 *
239 * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more
240 * compliant with RFC 3986. Similar to rawurlencode from PHP and our JS library
241 * mw.util.rawurlencode, except this also replaces spaces with `+`.
242 *
243 * @static
244 * @param {string} s String to encode
245 * @return {string} Encoded string for URI
246 */
247 Uri.encode = function ( s ) {
248 return encodeURIComponent( s )
249 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
250 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' )
251 .replace( /%20/g, '+' );
252 };
253
254 /**
255 * Decode a url encoded value.
256 *
257 * Reversed #encode. Standard decodeURIComponent, with addition of replacing
258 * `+` with a space.
259 *
260 * @static
261 * @param {string} s String to decode
262 * @return {string} Decoded string
263 */
264 Uri.decode = function ( s ) {
265 return decodeURIComponent( s.replace( /\+/g, '%20' ) );
266 };
267
268 Uri.prototype = {
269
270 /**
271 * Parse a string and set our properties accordingly.
272 *
273 * @private
274 * @param {string} str URI, see constructor.
275 * @param {Object} options See constructor.
276 */
277 parse: function ( str, options ) {
278 var q, matches,
279 uri = this;
280
281 // Apply parser regex and set all properties based on the result
282 matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
283 $.each( properties, function ( i, property ) {
284 uri[ property ] = matches[ i + 1 ];
285 } );
286
287 // uri.query starts out as the query string; we will parse it into key-val pairs then make
288 // that object the "query" property.
289 // we overwrite query in uri way to make cloning easier, it can use the same list of properties.
290 q = {};
291 // using replace to iterate over a string
292 if ( uri.query ) {
293 uri.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, function ( $0, $1, $2, $3 ) {
294 var k, v;
295 if ( $1 ) {
296 k = Uri.decode( $1 );
297 v = ( $2 === '' || $2 === undefined ) ? null : Uri.decode( $3 );
298
299 // If overrideKeys, always (re)set top level value.
300 // If not overrideKeys but this key wasn't set before, then we set it as well.
301 if ( options.overrideKeys || q[ k ] === undefined ) {
302 q[ k ] = v;
303
304 // Use arrays if overrideKeys is false and key was already seen before
305 } else {
306 // Once before, still a string, turn into an array
307 if ( typeof q[ k ] === 'string' ) {
308 q[ k ] = [ q[ k ] ];
309 }
310 // Add to the array
311 if ( $.isArray( q[ k ] ) ) {
312 q[ k ].push( v );
313 }
314 }
315 }
316 } );
317 }
318 uri.query = q;
319 },
320
321 /**
322 * Get user and password section of a URI.
323 *
324 * @return {string}
325 */
326 getUserInfo: function () {
327 return cat( '', this.user, cat( ':', this.password, '' ) );
328 },
329
330 /**
331 * Get host and port section of a URI.
332 *
333 * @return {string}
334 */
335 getHostPort: function () {
336 return this.host + cat( ':', this.port, '' );
337 },
338
339 /**
340 * Get the userInfo, host and port section of the URI.
341 *
342 * In most real-world URLs this is simply the hostname, but the definition of 'authority' section is more general.
343 *
344 * @return {string}
345 */
346 getAuthority: function () {
347 return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
348 },
349
350 /**
351 * Get the query arguments of the URL, encoded into a string.
352 *
353 * Does not preserve the original order of arguments passed in the URI. Does handle escaping.
354 *
355 * @return {string}
356 */
357 getQueryString: function () {
358 var args = [];
359 $.each( this.query, function ( key, val ) {
360 var k = Uri.encode( key ),
361 vals = $.isArray( val ) ? val : [ val ];
362 $.each( vals, function ( i, v ) {
363 if ( v === null ) {
364 args.push( k );
365 } else if ( k === 'title' ) {
366 args.push( k + '=' + mw.util.wikiUrlencode( v ) );
367 } else {
368 args.push( k + '=' + Uri.encode( v ) );
369 }
370 } );
371 } );
372 return args.join( '&' );
373 },
374
375 /**
376 * Get everything after the authority section of the URI.
377 *
378 * @return {string}
379 */
380 getRelativePath: function () {
381 return this.path + cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment, '' );
382 },
383
384 /**
385 * Get the entire URI string.
386 *
387 * May not be precisely the same as input due to order of query arguments.
388 *
389 * @return {string} The URI string
390 */
391 toString: function () {
392 return this.protocol + '://' + this.getAuthority() + this.getRelativePath();
393 },
394
395 /**
396 * Clone this URI
397 *
398 * @return {Object} New URI object with same properties
399 */
400 clone: function () {
401 return new Uri( this );
402 },
403
404 /**
405 * Extend the query section of the URI with new parameters.
406 *
407 * @param {Object} parameters Query parameters to add to ours (or to override ours with) as an
408 * object
409 * @return {Object} This URI object
410 */
411 extend: function ( parameters ) {
412 $.extend( this.query, parameters );
413 return this;
414 }
415 };
416
417 return Uri;
418 };
419
420 // Default to the current browsing location (for relative URLs).
421 mw.Uri = mw.UriRelative( function () {
422 return location.href;
423 } );
424
425 }( mediaWiki, jQuery ) );