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