2 * Library for simple URI parsing and manipulation.
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.
9 * You can modify the properties directly, then use the #toString method to extract the full URI
10 * string again. Example:
12 * var uri = new mw.Uri( 'http://example.com/mysite/mypage.php?quux=2' );
14 * if ( uri.host == 'example.com' ) {
15 * uri.host = 'foo.example.com';
16 * uri.extend( { bar: 1 } );
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
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
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:
32 * host 'www.example.com'
34 * path '/dir/dir.2/index.htm'
39 * test3: 'value (escaped)'
44 * (N.b., 'password' is technically not allowed for HTTP URIs, but it is possible with other kinds
47 * Parsing based on parseUri 1.2.2 (c) Steven Levithan <http://stevenlevithan.com>, MIT License.
48 * <http://stevenlevithan.com/demo/parseuri/js/>
53 ( function ( mw
, $ ) {
54 var parser
, properties
;
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.
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
69 function cat( pre
, val
, post
, raw
) {
70 if ( val
=== undefined || val
=== null || val
=== '' ) {
74 return pre
+ ( raw
? val
: mw
.Uri
.encode( val
) ) + post
;
78 * Regular expressions to parse many common URIs.
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.
86 * @property {Object} parser
89 strict
: mw
.template
.get( 'mediawiki.Uri', 'strict.regexp' ).render(),
90 loose
: mw
.template
.get( 'mediawiki.Uri', 'loose.regexp' ).render()
94 * The order here matches the order of captured matches in the `parser` property regexes.
98 * @property {Array} properties
112 * @property {string} protocol For example `http` (always present)
115 * @property {string|undefined} user For example `usr`
118 * @property {string|undefined} password For example `pwd`
121 * @property {string} host For example `www.example.com` (always present)
124 * @property {string|undefined} port For example `81`
127 * @property {string} path For example `/dir/dir.2/index.htm` (always present)
130 * @property {Object} query For example `{ a: '0', b: '', c: 'value' }` (always present)
133 * @property {string|undefined} fragment For example `top`
137 * A factory method to create a Uri class with a default location to resolve relative URLs
138 * against (including protocol-relative URLs).
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)
144 * @return {Function} Uri class
146 mw
.UriRelative = function ( documentLocation
) {
147 var getDefaultUri
= ( function () {
152 var hrefCur
= typeof documentLocation
=== 'string' ? documentLocation
: documentLocation();
153 if ( href
=== hrefCur
) {
157 // eslint-disable-next-line no-use-before-define
158 uri
= new Uri( href
);
164 * Construct a new URI object. Throws error if arguments are illegal/impossible, or
165 * otherwise don't parse.
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
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`).
180 function Uri( uri
, options
) {
182 hasOptions
= ( options
!== undefined ),
183 defaultUri
= getDefaultUri();
185 options
= typeof options
=== 'object' ? options
: { strictMode
: !!options
};
186 options
= $.extend( {
191 if ( uri
!== undefined && uri
!== null && uri
!== '' ) {
192 if ( typeof uri
=== 'string' ) {
193 this.parse( uri
, options
);
194 } else if ( typeof uri
=== 'object' ) {
195 // Copy data over from existing URI object
196 for ( prop
in uri
) {
197 // Only copy direct properties, not inherited ones
198 if ( uri
.hasOwnProperty( prop
) ) {
199 // Deep copy object properties
200 if ( Array
.isArray( uri
[ prop
] ) || $.isPlainObject( uri
[ prop
] ) ) {
201 this[ prop
] = $.extend( true, {}, uri
[ prop
] );
203 this[ prop
] = uri
[ prop
];
211 } else if ( hasOptions
) {
212 // We didn't get a URI in the constructor, but we got options.
213 hrefCur
= typeof documentLocation
=== 'string' ? documentLocation
: documentLocation();
214 this.parse( hrefCur
, options
);
216 // We didn't get a URI or options in the constructor, use the default instance.
217 return defaultUri
.clone();
220 // protocol-relative URLs
221 if ( !this.protocol
) {
222 this.protocol
= defaultUri
.protocol
;
226 this.host
= defaultUri
.host
;
229 this.port
= defaultUri
.port
;
232 if ( this.path
&& this.path
[ 0 ] !== '/' ) {
233 // A real relative URL, relative to defaultUri.path. We can't really handle that since we cannot
234 // figure out whether the last path component of defaultUri.path is a directory or a file.
235 throw new Error( 'Bad constructor arguments' );
237 if ( !( this.protocol
&& this.host
&& this.path
) ) {
238 throw new Error( 'Bad constructor arguments' );
243 * Encode a value for inclusion in a url.
245 * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more
246 * compliant with RFC 3986. Similar to rawurlencode from PHP and our JS library
247 * mw.util.rawurlencode, except this also replaces spaces with `+`.
250 * @param {string} s String to encode
251 * @return {string} Encoded string for URI
253 Uri
.encode = function ( s
) {
254 return encodeURIComponent( s
)
255 .replace( /!/g, '%21' ).replace( /'/g, '%27' ).replace( /\(/g, '%28' )
256 .replace( /\)/g, '%29' ).replace( /\*/g, '%2A
' )
257 .replace( /%20/g, '+' );
261 * Decode a url encoded value.
263 * Reversed #encode. Standard decodeURIComponent, with addition of replacing
267 * @param {string} s String to decode
268 * @return {string} Decoded string
270 Uri.decode = function ( s ) {
271 return decodeURIComponent( s.replace( /\+/g, '%20' ) );
277 * Parse a string and set our properties accordingly.
280 * @param {string} str URI, see constructor.
281 * @param {Object} options See constructor.
283 parse: function ( str, options ) {
286 hasOwn = Object.prototype.hasOwnProperty;
288 // Apply parser regex and set all properties based on the result
289 matches = parser[ options.strictMode ? 'strict
' : 'loose
' ].exec( str );
290 properties.forEach( function ( property, i ) {
291 uri[ property ] = matches[ i + 1 ];
294 // uri.query starts out as the query string; we will parse it into key-val pairs then make
295 // that object the "query" property.
296 // we overwrite query in uri way to make cloning easier, it can use the same list of properties.
298 // using replace to iterate over a string
300 uri.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, function ( $0, $1, $2, $3 ) {
303 k = Uri.decode( $1 );
304 v = ( $2 === '' || $2 === undefined ) ? null : Uri.decode( $3 );
306 // If overrideKeys, always (re)set top level value.
307 // If not overrideKeys but this key wasn't
set before
, then we
set it as well
.
308 if ( options
.overrideKeys
|| !hasOwn
.call( q
, k
) ) {
311 // Use arrays if overrideKeys is false and key was already seen before
313 // Once before, still a string, turn into an array
314 if ( typeof q
[ k
] === 'string' ) {
318 if ( Array
.isArray( q
[ k
] ) ) {
327 // Decode uri.fragment, otherwise it gets double-encoded when serializing
328 if ( uri
.fragment
!== undefined ) {
329 uri
.fragment
= Uri
.decode( uri
.fragment
);
334 * Get user and password section of a URI.
338 getUserInfo: function () {
339 return cat( '', this.user
, cat( ':', this.password
, '' ) );
343 * Get host and port section of a URI.
347 getHostPort: function () {
348 return this.host
+ cat( ':', this.port
, '' );
352 * Get the userInfo, host and port section of the URI.
354 * In most real-world URLs this is simply the hostname, but the definition of 'authority' section is more general.
358 getAuthority: function () {
359 return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
363 * Get the query arguments of the URL, encoded into a string.
365 * Does not preserve the original order of arguments passed in the URI. Does handle escaping.
369 getQueryString: function () {
371 $.each( this.query
, function ( key
, val
) {
372 var k
= Uri
.encode( key
),
373 vals
= Array
.isArray( val
) ? val
: [ val
];
374 vals
.forEach( function ( v
) {
377 } else if ( k
=== 'title' ) {
378 args
.push( k
+ '=' + mw
.util
.wikiUrlencode( v
) );
380 args
.push( k
+ '=' + Uri
.encode( v
) );
384 return args
.join( '&' );
388 * Get everything after the authority section of the URI.
392 getRelativePath: function () {
393 return this.path
+ cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment
, '' );
397 * Get the entire URI string.
399 * May not be precisely the same as input due to order of query arguments.
401 * @return {string} The URI string
403 toString: function () {
404 return this.protocol
+ '://' + this.getAuthority() + this.getRelativePath();
410 * @return {Object} New URI object with same properties
413 return new Uri( this );
417 * Extend the query section of the URI with new parameters.
419 * @param {Object} parameters Query parameters to add to ours (or to override ours with) as an
421 * @return {Object} This URI object
423 extend: function ( parameters
) {
424 $.extend( this.query
, parameters
);
432 // Default to the current browsing location (for relative URLs).
433 mw
.Uri
= mw
.UriRelative( function () {
434 return location
.href
;
437 }( mediaWiki
, jQuery
) );