Index: trunk/extensions/UploadWizard/resources/mw.Uri.js |
— | — | @@ -76,32 +76,43 @@ |
77 | 77 | } |
78 | 78 | }; |
79 | 79 | |
80 | | - mw.Uri.prototype = { |
| 80 | + /** |
| 81 | + * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more compliant with RFC 3986 |
| 82 | + * Similar to rawurlencode from PHP and our JS library mw.util.rawurlencode, but we also replace space with a + |
| 83 | + * @param {String} string |
| 84 | + * @return {String} encoded for URI |
| 85 | + */ |
| 86 | + mw.Uri.encode = function( s ) { |
| 87 | + return encodeURIComponent( s ) |
| 88 | + .replace( /!/g, '%21').replace( /'/g, '%27').replace( /\(/g, '%28') |
| 89 | + .replace( /\)/g, '%29').replace( /\*/g, '%2A') |
| 90 | + .replace( /%20/g, '+' ); |
| 91 | + }; |
81 | 92 | |
82 | | - /** |
83 | | - * Standard encodeURIComponent, with extra stuff to make all browsers work similarly and more compliant with RFC 3986 |
84 | | - * @param {String} string |
85 | | - * @return {String} encoded for URI |
86 | | - */ |
87 | | - encode: function( component ) { |
88 | | - return encodeURIComponent( component ) |
89 | | - .replace( /!/g, '%21') |
90 | | - .replace( /'/g, '%27') |
91 | | - .replace( /\(/g, '%28') |
92 | | - .replace( /\)/g, '%29') |
93 | | - .replace( /\*/g, '%2A') |
94 | | - .replace( /%20/g, '+' ); |
95 | | - }, |
| 93 | + /** |
| 94 | + * Standard decodeURIComponent, with '+' to space |
| 95 | + * @param {String} string encoded for URI |
| 96 | + * @return {String} decoded string |
| 97 | + */ |
| 98 | + mw.Uri.decode = function( s ) { |
| 99 | + return decodeURIComponent( s ).replace( /\+/g, ' ' ); |
| 100 | + }; |
96 | 101 | |
97 | | - /** |
98 | | - * Standard decodeURIComponent, with '+' to space |
99 | | - * @param {String} string encoded for URI |
100 | | - * @return {String} decoded string |
101 | | - */ |
102 | | - decode: function( component ) { |
103 | | - return decodeURIComponent( component ).replace( /\+/g, ' ' ); |
104 | | - }, |
| 102 | + /** |
| 103 | + * Function that's useful when constructing the URI string -- we frequently encounter the pattern of |
| 104 | + * 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. |
| 105 | + * @param {String} to prepend, if value not empty |
| 106 | + * @param {String} value to include, if not empty |
| 107 | + * @param {String} to append, if value not empty |
| 108 | + * @param {Boolean} raw -- if true, do not URI encode |
| 109 | + * @return {String} |
| 110 | + */ |
| 111 | + function _cat( pre, val, post, raw ) { |
| 112 | + return mw.isEmpty( val ) ? '' : pre + ( raw ? val : mw.Uri.encode( val ) ) + post; |
| 113 | + } |
105 | 114 | |
| 115 | + mw.Uri.prototype = { |
| 116 | + |
106 | 117 | // regular expressions to parse many common URIs. |
107 | 118 | // @private |
108 | 119 | _parser: { |
— | — | @@ -145,7 +156,7 @@ |
146 | 157 | if ( uri.query ) { |
147 | 158 | uri.query.replace( /(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) { |
148 | 159 | if ( $1 ) { |
149 | | - query[ uri.decode( $1 ) ] = uri.decode( $2 ); |
| 160 | + query[ mw.Uri.decode( $1 ) ] = mw.Uri.decode( $2 ); |
150 | 161 | } |
151 | 162 | } ); |
152 | 163 | } |
— | — | @@ -157,14 +168,7 @@ |
158 | 169 | * @return {String} |
159 | 170 | */ |
160 | 171 | getUserInfo: function() { |
161 | | - var userInfo = ''; |
162 | | - if ( !mw.isEmpty( this.user ) ) { |
163 | | - userInfo += this.encode( this.user ); |
164 | | - if ( !mw.isEmpty( this.password ) ) { |
165 | | - userInfo += ':' + this.encode( this.password ); |
166 | | - } |
167 | | - } |
168 | | - return userInfo; |
| 172 | + return _cat( '', this.user, _cat( ':', this.password, '' ) ); |
169 | 173 | }, |
170 | 174 | |
171 | 175 | /** |
— | — | @@ -172,10 +176,7 @@ |
173 | 177 | * @return {String} |
174 | 178 | */ |
175 | 179 | getHostPort: function() { |
176 | | - return this.host |
177 | | - + ( !mw.isEmpty( this.port ) ? ':' + this.port |
178 | | - : '' |
179 | | - ); |
| 180 | + return this.host + _cat( ':', this.port, '' ); |
180 | 181 | }, |
181 | 182 | |
182 | 183 | /** |
— | — | @@ -184,11 +185,7 @@ |
185 | 186 | * @return {String} |
186 | 187 | */ |
187 | 188 | getAuthority: function() { |
188 | | - var userInfo = this.getUserInfo(); |
189 | | - return ( !mw.isEmpty( userInfo ) ? userInfo + '@' |
190 | | - : '' |
191 | | - ) |
192 | | - + this.getHostPort(); |
| 189 | + return _cat( '', this.getUserInfo(), '@' ) + this.getHostPort(); |
193 | 190 | }, |
194 | 191 | |
195 | 192 | /** |
— | — | @@ -200,7 +197,7 @@ |
201 | 198 | var pairs = []; |
202 | 199 | var _this = this; |
203 | 200 | $.each( this.query, function( key, value ) { |
204 | | - pairs.push( _this.encode( key ) + '=' + _this.encode( value ) ); |
| 201 | + pairs.push( mw.Uri.encode( key ) + '=' + mw.Uri.encode( value ) ); |
205 | 202 | } ); |
206 | 203 | return pairs.join( '&' ); |
207 | 204 | }, |
— | — | @@ -210,14 +207,7 @@ |
211 | 208 | * @return {String} |
212 | 209 | */ |
213 | 210 | getRelativePath: function() { |
214 | | - var queryString = this.getQueryString(); |
215 | | - return this.path |
216 | | - + ( !mw.isEmpty( queryString ) ? '?' + queryString |
217 | | - : '' |
218 | | - ) |
219 | | - + ( !mw.isEmpty( this.fragment ) ? '#' + this.encode( this.fragment ) |
220 | | - : '' |
221 | | - ); |
| 211 | + return this.path + _cat( '?', this.getQueryString(), '', true ) + _cat( '#', this.fragment, '' ); |
222 | 212 | }, |
223 | 213 | |
224 | 214 | /** |