Merge "jquery.tablesorter: Silence an expected "sort-rowspan-error" warning"
[mediawiki.git] / resources / src / mediawiki.Uri / Uri.js
blobaec86da1f759519d1d7c61bf53ae79a8a1acf6e4
1 ( function () {
2 /**
3 * Function that's useful when constructing the URI string -- we frequently encounter the pattern
4 * of having to add something to the URI as we go, but only if it's present, and to include a
5 * character before or after if so.
7 * @private
8 * @static
9 * @param {string|undefined} pre To prepend
10 * @param {string} val To include
11 * @param {string} post To append
12 * @param {boolean} raw If true, val will not be encoded
13 * @return {string} Result
15 function cat( pre, val, post, raw ) {
16 if ( val === undefined || val === null || val === '' ) {
17 return '';
20 return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
23 /**
24 * Regular expressions to parse many common URIs.
26 * These are gnarly expressions. For improved readability, they have been moved to a separate
27 * file where they make use of named capture groups. That syntax isn't valid in JavaScript ES5,
28 * so the server-side strips these before delivering to the client.
30 * @private
31 * @static
32 * @property {Object} parser
34 const parser = {
35 strict: require( './strict.regexp.js' ),
36 loose: require( './loose.regexp.js' )
39 /**
40 * The order here matches the order of captured matches in the `parser` property regexes.
42 * @private
43 * @static
44 * @property {string[]} properties
46 const properties = [
47 'protocol',
48 'user',
49 'password',
50 'host',
51 'port',
52 'path',
53 'query',
54 'fragment'
57 /**
58 * A factory method to create an {@link mw.Uri} class with a default location to resolve relative URLs
59 * against (including protocol-relative URLs).
61 * @memberof mw
62 * @param {string|Function} documentLocation A full url, or function returning one.
63 * If passed a function, the return value may change over time and this will be honoured. (T74334)
64 * @return {mw.Uri} An mw.Uri class constructor
66 mw.UriRelative = function ( documentLocation ) {
67 const getDefaultUri = ( function () {
68 // Cache
69 let href, uri;
71 return function () {
72 const hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
73 if ( href === hrefCur ) {
74 return uri;
76 href = hrefCur;
77 uri = new Uri( href );
78 return uri;
80 }() );
81 /**
82 * Options for mw.Uri object.
84 * @typedef {Object} mw.Uri.UriOptions
85 * @property {boolean} [strictMode=false] Trigger strict mode parsing of the url.
86 * @property {boolean} [overrideKeys=false] Whether to let duplicate query parameters
87 * override each other (`true`) or automagically convert them to an array (`false`).
88 * @property {boolean} [arrayParams=false] Whether to parse array query parameters (e.g.
89 * `&foo[0]=a&foo[1]=b` or `&foo[]=a&foo[]=b`) or leave them alone. Currently this does not
90 * handle associative or multi-dimensional arrays, but that may be improved in the future.
91 * Implies `overrideKeys: true` (query parameters without `[...]` are not parsed as arrays).
94 /**
95 * @classdesc Create and manipulate MediaWiki URIs.
97 * Intended to be minimal, but featureful; do not expect full RFC 3986 compliance. The use cases we
98 * have in mind are constructing 'next page' or 'previous page' URLs, detecting whether we need to
99 * use cross-domain proxies for an API, constructing simple URL-based API calls, etc. Parsing here
100 * is regex-based, so may not work on all URIs, but is good enough for most.
102 * You can modify the properties directly, then use the {@link mw.Uri#toString toString} method to extract the full URI
103 * string again. Example:
104 * ```
105 * var uri = new mw.Uri( 'http://example.com/mysite/mypage.php?quux=2' );
107 * if ( uri.host == 'example.com' ) {
108 * uri.host = 'foo.example.com';
109 * uri.extend( { bar: 1 } );
111 * $( 'a#id1' ).attr( 'href', uri );
112 * // anchor with id 'id1' now links to http://foo.example.com/mysite/mypage.php?bar=1&quux=2
114 * $( 'a#id2' ).attr( 'href', uri.clone().extend( { bar: 3, pif: 'paf' } ) );
115 * // anchor with id 'id2' now links to http://foo.example.com/mysite/mypage.php?bar=3&quux=2&pif=paf
117 * ```
118 * Given a URI like
119 * `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`
120 * the returned object will have the following properties:
121 * ```
122 * protocol 'http'
123 * user 'usr'
124 * password 'pwd'
125 * host 'www.example.com'
126 * port '81'
127 * path '/dir/dir.2/index.htm'
128 * query {
129 * q1: '0',
130 * test1: null,
131 * test2: '',
132 * test3: 'value (escaped)'
133 * r: ['1', '2']
135 * fragment 'top'
136 * ```
137 * Note: 'password' is technically not allowed for HTTP URIs, but it is possible with other kinds
138 * of URIs.
140 * Parsing based on parseUri 1.2.2 (c) Steven Levithan <http://stevenlevithan.com>, MIT License.
141 * <http://stevenlevithan.com/demo/parseuri/js/>
143 * @class
144 * @name mw.Uri
146 * @constructor
147 * @description Construct a new URI object. Throws error if arguments are illegal/impossible, or
148 * otherwise don't parse.
149 * @param {Object|string} [uri] URI string, or an Object with appropriate properties (especially
150 * another URI object to clone). Object must have non-blank `protocol`, `host`, and `path`
151 * properties. If omitted (or set to `undefined`, `null` or empty string), then an object
152 * will be created for the default `uri` of this constructor (`location.href` for mw.Uri,
153 * other values for other instances -- see {@link mw.UriRelative} for details).
154 * @param {mw.Uri.UriOptions|boolean} [options] Object with options, or (backwards compatibility) a boolean
155 * for strictMode
156 * @throws {Error} when the query string or fragment contains an unknown % sequence
158 function Uri( uri, options ) {
159 const hasOptions = ( options !== undefined ),
160 defaultUri = getDefaultUri();
162 options = typeof options === 'object' ? options : { strictMode: !!options };
163 options = Object.assign( {
164 strictMode: false,
165 overrideKeys: false,
166 arrayParams: false
167 }, options );
169 this.arrayParams = options.arrayParams;
171 if ( uri !== undefined && uri !== null && uri !== '' ) {
172 if ( typeof uri === 'string' ) {
173 this.parse( uri, options );
174 } else if ( typeof uri === 'object' ) {
175 // Copy data over from existing URI object
176 for ( const prop in uri ) {
177 // Only copy direct properties, not inherited ones
178 if ( Object.prototype.hasOwnProperty.call( uri, prop ) ) {
179 // Deep copy object properties
180 if ( Array.isArray( uri[ prop ] ) || $.isPlainObject( uri[ prop ] ) ) {
181 this[ prop ] = $.extend( true, {}, uri[ prop ] );
182 } else {
183 this[ prop ] = uri[ prop ];
187 if ( !this.query ) {
188 this.query = {};
191 } else if ( hasOptions ) {
192 // We didn't get a URI in the constructor, but we got options.
193 const hrefCur = typeof documentLocation === 'string' ? documentLocation : documentLocation();
194 this.parse( hrefCur, options );
195 } else {
196 // We didn't get a URI or options in the constructor, use the default instance.
197 return defaultUri.clone();
200 // protocol-relative URLs
201 if ( !this.protocol ) {
202 this.protocol = defaultUri.protocol;
204 // No host given:
205 if ( !this.host ) {
206 this.host = defaultUri.host;
207 // port ?
208 if ( !this.port ) {
209 this.port = defaultUri.port;
212 if ( this.path && this.path[ 0 ] !== '/' ) {
213 // A real relative URL, relative to defaultUri.path. We can't really handle that since we cannot
214 // figure out whether the last path component of defaultUri.path is a directory or a file.
215 throw new Error( 'Bad constructor arguments' );
217 if ( !( this.protocol && this.host && this.path ) ) {
218 throw new Error( 'Bad constructor arguments' );
223 * For example `http` (always present).
225 * @name mw.Uri.prototype.protocol
226 * @type {string}
230 * For example `usr`.
232 * @name mw.Uri.prototype.user
233 * @type {string|undefined}
236 * For example `pwd`.
238 * @name mw.Uri.prototype.password
239 * @type {string|undefined}
242 * For example `www.example.com` (always present).
244 * @name mw.Uri.prototype.host
245 * @type {string}
248 * For example `81`.
250 * @name mw.Uri.prototype.port
251 * @type {string|undefined}
254 * For example `/dir/dir.2/index.htm` (always present).
256 * @name mw.Uri.prototype.path
257 * @type {string}
260 * For example `{ a: '0', b: '', c: 'value' }` (always present).
262 * @name mw.Uri.prototype.query
263 * @type {Object}
266 * For example `top`.
268 * @name mw.Uri.prototype.fragment
269 * @type {string|undefined}
273 * Encode a value for inclusion in a url.
275 * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more
276 * compliant with RFC 3986. Similar to rawurlencode from PHP and our JS library
277 * {@link module:mediawiki.util.rawurlencode mw.util.rawurlencode}, except this also replaces spaces with `+`.
279 * @method
280 * @name mw.Uri.encode
281 * @param {string} s String to encode
282 * @return {string} Encoded string for URI
284 Uri.encode = function ( s ) {
285 return encodeURIComponent( s )
286 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
287 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A' )
288 .replace( /%20/g, '+' );
292 * Decode a url encoded value.
294 * Reversed {@link mw.Uri.encode encode}. Standard decodeURIComponent, with addition of replacing
295 * `+` with a space.
297 * @method
298 * @name mw.Uri.decode
299 * @param {string} s String to decode
300 * @return {string} Decoded string
301 * @throws {Error} when the string contains an unknown % sequence
303 Uri.decode = function ( s ) {
304 return decodeURIComponent( s.replace( /\+/g, '%20' ) );
307 Uri.prototype = /** @lends mw.Uri.prototype */ {
310 * Parse a string and set our properties accordingly.
312 * @private
313 * @param {string} str URI, see constructor.
314 * @param {Object} options See constructor.
315 * @throws {Error} when the query string or fragment contains an unknown % sequence
317 parse: function ( str, options ) {
318 const hasOwn = Object.prototype.hasOwnProperty;
320 // Apply parser regex and set all properties based on the result
321 const matches = parser[ options.strictMode ? 'strict' : 'loose' ].exec( str );
322 properties.forEach( ( property, i ) => {
323 this[ property ] = matches[ i + 1 ];
324 } );
326 // uri.query starts out as the query string; we will parse it into key-val pairs then make
327 // that object the "query" property.
328 // we overwrite query in uri way to make cloning easier, it can use the same list of properties.
329 const q = {};
330 // using replace to iterate over a string
331 if ( this.query ) {
333 this.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, ( match, k, eq, v ) => {
334 let arrayKeyMatch, i;
335 if ( k ) {
336 k = Uri.decode( k );
337 v = ( eq === '' || eq === undefined ) ? null : Uri.decode( v );
338 arrayKeyMatch = k.match( /^([^[]+)\[(\d*)\]$/ );
340 // If arrayParams and this parameter name contains an array index...
341 if ( options.arrayParams && arrayKeyMatch ) {
342 // Remove the index from parameter name
343 k = arrayKeyMatch[ 1 ];
345 // Turn the parameter value into an array (throw away anything else)
346 if ( !Array.isArray( q[ k ] ) ) {
347 q[ k ] = [];
350 i = arrayKeyMatch[ 2 ];
351 if ( i === '' ) {
352 // If no explicit index, append at the end
353 i = q[ k ].length;
356 q[ k ][ i ] = v;
358 // If overrideKeys, always (re)set top level value.
359 // If not overrideKeys but this key wasn't set before, then we set it as well.
360 // arrayParams implies overrideKeys (no array handling for non-array params).
361 } else if ( options.arrayParams || options.overrideKeys || !hasOwn.call( q, k ) ) {
362 q[ k ] = v;
364 // Use arrays if overrideKeys is false and key was already seen before
365 } else {
366 // Once before, still a string, turn into an array
367 if ( typeof q[ k ] === 'string' ) {
368 q[ k ] = [ q[ k ] ];
370 // Add to the array
371 if ( Array.isArray( q[ k ] ) ) {
372 q[ k ].push( v );
376 } );
378 this.query = q;
380 // Decode uri.fragment, otherwise it gets double-encoded when serializing
381 if ( this.fragment !== undefined ) {
382 this.fragment = Uri.decode( this.fragment );
387 * Get user and password section of a URI.
389 * @return {string}
391 getUserInfo: function () {
392 return cat( '', this.user, cat( ':', this.password, '' ) );
396 * Get host and port section of a URI.
398 * @return {string}
400 getHostPort: function () {
401 return this.host + cat( ':', this.port, '' );
405 * Get the userInfo, host and port section of the URI.
407 * In most real-world URLs this is simply the hostname, but the definition of 'authority' section is more general.
409 * @return {string}
411 getAuthority: function () {
412 return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
416 * Get the query arguments of the URL, encoded into a string.
418 * Does not preserve the original order of arguments passed in the URI. Does handle escaping.
420 * @return {string}
422 getQueryString: function () {
423 const args = [],
424 arrayParams = this.arrayParams;
425 Object.keys( this.query ).forEach( ( key ) => {
426 const val = this.query[ key ];
427 const k = Uri.encode( key ),
428 isArrayParam = Array.isArray( val ),
429 vals = isArrayParam ? val : [ val ];
430 vals.forEach( ( v, i ) => {
431 let ki = k;
432 if ( arrayParams && isArrayParam ) {
433 ki += Uri.encode( '[' + i + ']' );
435 if ( v === null ) {
436 args.push( ki );
437 } else if ( k === 'title' ) {
438 args.push( ki + '=' + mw.util.wikiUrlencode( v ) );
439 } else {
440 args.push( ki + '=' + Uri.encode( v ) );
442 } );
443 } );
444 return args.join( '&' );
448 * Get everything after the authority section of the URI.
450 * @return {string}
452 getRelativePath: function () {
453 return this.path + cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment, '' );
457 * Get the entire URI string.
459 * Note that the output may not be precisely the same as the constructor input,
460 * due to order of query arguments.
461 * Note also that the fragment is not always roundtripped as-is; some characters will
462 * become encoded, including the slash character, which can cause problems with e.g.
463 * mediawiki.router. It is recommended to use the native URL class (via
464 * web2017-polyfills, which loads a polyfill if needed) in contexts where the fragment
465 * is important.
467 * @return {string} The URI string
469 toString: function () {
470 return this.protocol + '://' + this.getAuthority() + this.getRelativePath();
474 * Clone this URI.
476 * @return {Object} New URI object with same properties
478 clone: function () {
479 return new Uri( this );
483 * Extend the query section of the URI with new parameters.
485 * @param {Object} parameters Query parameters to add to ours (or to override ours with) as an
486 * object
487 * @return {Object} This URI object
489 extend: function ( parameters ) {
490 for ( const name in parameters ) {
491 const parameter = parameters[ name ];
492 if ( parameter !== undefined ) {
493 this.query[ name ] = parameter;
496 return this;
500 return Uri;
504 * Default to the current browsing location (for relative URLs).
506 * @ignore
507 * @return {mw.Uri}
509 mw.Uri = mw.UriRelative( () => location.href );
511 }() );