Make file names match module names.
[mediawiki.git] / resources / mediawiki / mediawiki.Uri.js
blob7ff8dda47a93221fb09529801fd614c591c936b4
1 /**
2  * Library for simple URI parsing and manipulation.  Requires jQuery.
3  *
4  * Do not expect full RFC 3986 compliance. Intended to be minimal, but featureful.
5  * The use cases we have in mind are constructing 'next page' or 'previous page' URLs,
6  * detecting whether we need to use cross-domain proxies for an API, constructing
7  * simple URL-based API calls, etc.
8  *
9  * Intended to compress very well if you use a JS-parsing minifier.
10  *
11  * Dependencies: mw, jQuery
12  *
13  * Example:
14  *
15  *     var uri = new mw.Uri( 'http://foo.com/mysite/mypage.php?quux=2' );
16  *
17  *     if ( uri.host == 'foo.com' ) {
18  *         uri.host = 'www.foo.com';
19  *         uri.extend( { bar: 1 } );
20  *
21  *         $( 'a#id1' ).attr( 'href', uri );
22  *         // anchor with id 'id1' now links to http://foo.com/mysite/mypage.php?bar=1&quux=2
23  *
24  *         $( 'a#id2' ).attr( 'href', uri.clone().extend( { bar: 3, pif: 'paf' } ) );
25  *         // anchor with id 'id2' now links to http://foo.com/mysite/mypage.php?bar=3&quux=2&pif=paf
26  *     }
27  *
28  * Parsing here is regex based, so may not work on all URIs, but is good enough for most.
29  *
30  * Given a URI like
31  * 'http://usr:pwd@www.test.com:81/dir/dir.2/index.htm?q1=0&&test1&test2=&test3=value+%28escaped%29&r=1&r=2#top':
32  * The returned object will have the following properties:
33  *
34  *    protocol  'http'
35  *    user      'usr'
36  *    password  'pwd'
37  *    host      'www.test.com'
38  *    port      '81'
39  *    path      '/dir/dir.2/index.htm'
40  *    query     {
41  *                  q1: 0,
42  *                  test1: null,
43  *                  test2: '',
44  *                  test3: 'value (escaped)'
45  *                  r: [1, 2]
46  *              }
47  *    fragment  'top'
48  *
49  * n.b. 'password' is not technically allowed for HTTP URIs, but it is possible with other
50  * sorts of URIs.
51  * You can modify the properties directly. Then use the toString() method to extract the
52  * full URI string again.
53  *
54  * Parsing based on parseUri 1.2.2 (c) Steven Levithan <stevenlevithan.com> MIT License
55  * http://stevenlevithan.com/demo/parseuri/js/
56  *
57  */
59 ( function( $ ) {
61         /**
62          * Function that's useful when constructing the URI string -- we frequently encounter the pattern of
63          * having to add something to the URI as we go, but only if it's present, and to include a character before or after if so.
64          * @param {String} to prepend, if value not empty
65          * @param {String} value to include, if not empty
66          * @param {String} to append, if value not empty
67          * @param {Boolean} raw -- if true, do not URI encode
68          * @return {String}
69          */
70         function cat( pre, val, post, raw ) {
71                 if ( val === undefined || val === null || val === '' ) {
72                         return '';
73                 } else {
74                         return pre + ( raw ? val : mw.Uri.encode( val ) ) + post;
75                 }
76         }
78         // Regular expressions to parse many common URIs.
79         var parser = {
80                 strict: /^(?:([^:\/?#]+):)?(?:\/\/(?:(?:([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)?((?:[^?#\/]*\/)*[^?#]*)(?:\?([^#]*))?(?:#(.*))?/,
81                 loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?(?:(?:([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?((?:\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?[^?#\/]*)(?:\?([^#]*))?(?:#(.*))?/
82         },
84         // The order here matches the order of captured matches in the above parser regexes.
85         properties = [
86                 'protocol',  // http
87                 'user',      // usr
88                 'password',  // pwd
89                 'host',      // www.test.com
90                 'port',      // 81
91                 'path',      // /dir/dir.2/index.htm
92                 'query',     // q1=0&&test1&test2=value (will become { q1: 0, test1: '', test2: 'value' } )
93                 'fragment'   // top
94         ];
96         /**
97          * Constructs URI object. Throws error if arguments are illegal/impossible, or otherwise don't parse.
98          * @constructor
99          * @param {!Object|String} URI string, or an Object with appropriate properties (especially another URI object to clone). Object must have non-blank 'protocol', 'host', and 'path' properties.
100          * @param {Boolean} strict mode (when parsing a string)
101          */
102         mw.Uri = function( uri, strictMode ) {
103                 strictMode = !!strictMode;
104                 if ( uri !== undefined && uri !== null || uri !== '' ) {
105                         if ( typeof uri === 'string' ) {
106                                 this._parse( uri, strictMode );
107                         } else if ( typeof uri === 'object' ) {
108                                 var _this = this;
109                                 $.each( properties, function( i, property ) {
110                                         _this[property] = uri[property];
111                                 } );
112                                 if ( this.query === undefined ) {
113                                         this.query = {};
114                                 }
115                         }
116                 }
117                 if ( !( this.protocol && this.host && this.path ) ) {
118                         throw new Error( 'Bad constructor arguments' );
119                 }
120         };
122         /**
123          * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more compliant with RFC 3986
124          * Similar to rawurlencode from PHP and our JS library mw.util.rawurlencode, but we also replace space with a +
125          * @param {String} string
126          * @return {String} encoded for URI
127          */
128         mw.Uri.encode = function( s ) {
129                 return encodeURIComponent( s )
130                         .replace( /!/g, '%21').replace( /'/g, '%27').replace( /\(/g, '%28')
131                         .replace( /\)/g, '%29').replace( /\*/g, '%2A')
132                         .replace( /%20/g, '+' );
133         };
135         /**
136          * Standard decodeURIComponent, with '+' to space
137          * @param {String} string encoded for URI
138          * @return {String} decoded string
139          */
140         mw.Uri.decode = function( s ) {
141                 return decodeURIComponent( s ).replace( /\+/g, ' ' );
142         };
144         mw.Uri.prototype = {
146                 /**
147                  * Parse a string and set our properties accordingly.
148                  * @param {String} URI
149                  * @param {Boolean} strictness
150                  * @return {Boolean} success
151                  */
152                 _parse: function( str, strictMode ) {
153                         var matches = parser[ strictMode ? 'strict' : 'loose' ].exec( str );
154                         var uri = this;
155                         $.each( properties, function( i, property ) {
156                                 uri[ property ] = matches[ i+1 ];
157                         } );
159                         // uri.query starts out as the query string; we will parse it into key-val pairs then make
160                         // that object the "query" property.
161                         // we overwrite query in uri way to make cloning easier, it can use the same list of properties.
162                         var q = {};
163                         // using replace to iterate over a string
164                         if ( uri.query ) {
165                                 uri.query.replace( /(?:^|&)([^&=]*)(?:(=)([^&]*))?/g, function ($0, $1, $2, $3) {
166                                         if ( $1 ) {
167                                                 var k = mw.Uri.decode( $1 );
168                                                 var v = ( $2 === '' || $2 === undefined ) ? null : mw.Uri.decode( $3 );
169                                                 if ( typeof q[ k ] === 'string' ) {
170                                                         q[ k ] = [ q[ k ] ];
171                                                 }
172                                                 if ( typeof q[ k ] === 'object' ) {
173                                                         q[ k ].push( v );
174                                                 } else {
175                                                         q[ k ] = v;
176                                                 }
177                                         }
178                                 } );
179                         }
180                         this.query = q;
181                 },
183                 /**
184                  * Returns user and password portion of a URI.
185                  * @return {String}
186                  */
187                 getUserInfo: function() {
188                         return cat( '', this.user, cat( ':', this.password, '' ) );
189                 },
191                 /**
192                  * Gets host and port portion of a URI.
193                  * @return {String}
194                  */
195                 getHostPort: function() {
196                         return this.host + cat( ':', this.port, '' );
197                 },
199                 /**
200                  * Returns the userInfo and host and port portion of the URI.
201                  * In most real-world URLs, this is simply the hostname, but it is more general.
202                  * @return {String}
203                  */
204                 getAuthority: function() {
205                         return cat( '', this.getUserInfo(), '@' ) + this.getHostPort();
206                 },
208                 /**
209                  * Returns the query arguments of the URL, encoded into a string
210                  * Does not preserve the order of arguments passed into the URI. Does handle escaping.
211                  * @return {String}
212                  */
213                 getQueryString: function() {
214                         var args = [];
215                         $.each( this.query, function( key, val ) {
216                                 var k = mw.Uri.encode( key );
217                                 var vals = val === null ? [ null ] : $.makeArray( val );
218                                 $.each( vals, function( i, v ) {
219                                         args.push( k + ( v === null ? '' : '=' + mw.Uri.encode( v ) ) );
220                                 } );
221                         } );
222                         return args.join( '&' );
223                 },
225                 /**
226                  * Returns everything after the authority section of the URI
227                  * @return {String}
228                  */
229                 getRelativePath: function() {
230                         return this.path + cat( '?', this.getQueryString(), '', true ) + cat( '#', this.fragment, '' );
231                 },
233                 /**
234                  * Gets the entire URI string. May not be precisely the same as input due to order of query arguments.
235                  * @return {String} the URI string
236                  */
237                 toString: function() {
238                         return this.protocol + '://' + this.getAuthority() + this.getRelativePath();
239                 },
241                 /**
242                  * Clone this URI
243                  * @return {Object} new URI object with same properties
244                  */
245                 clone: function() {
246                         return new mw.Uri( this );
247                 },
249                 /**
250                  * Extend the query -- supply query parameters to override or add to ours
251                  * @param {Object} query parameters in key-val form to override or add
252                  * @return {Object} this URI object
253                  */
254                 extend: function( parameters ) {
255                         $.extend( this.query, parameters );
256                         return this;
257                 }
258         };
260 } )( jQuery );