r83538 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r83537‎ | r83538 | r83539 >
Date:18:58, 8 March 2011
Author:neilk
Status:deferred
Tags:
Comment:
refactored a common pattern when constructing URI string, moved encode & decode to be public class methods
Modified paths:
  • /trunk/extensions/UploadWizard/resources/mw.Uri.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/resources/mw.Uri.js
@@ -76,32 +76,43 @@
7777 }
7878 };
7979
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+ };
8192
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+ };
96101
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+ }
105114
 115+ mw.Uri.prototype = {
 116+
106117 // regular expressions to parse many common URIs.
107118 // @private
108119 _parser: {
@@ -145,7 +156,7 @@
146157 if ( uri.query ) {
147158 uri.query.replace( /(?:^|&)([^&=]*)=?([^&]*)/g, function ($0, $1, $2) {
148159 if ( $1 ) {
149 - query[ uri.decode( $1 ) ] = uri.decode( $2 );
 160+ query[ mw.Uri.decode( $1 ) ] = mw.Uri.decode( $2 );
150161 }
151162 } );
152163 }
@@ -157,14 +168,7 @@
158169 * @return {String}
159170 */
160171 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, '' ) );
169173 },
170174
171175 /**
@@ -172,10 +176,7 @@
173177 * @return {String}
174178 */
175179 getHostPort: function() {
176 - return this.host
177 - + ( !mw.isEmpty( this.port ) ? ':' + this.port
178 - : ''
179 - );
 180+ return this.host + _cat( ':', this.port, '' );
180181 },
181182
182183 /**
@@ -184,11 +185,7 @@
185186 * @return {String}
186187 */
187188 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();
193190 },
194191
195192 /**
@@ -200,7 +197,7 @@
201198 var pairs = [];
202199 var _this = this;
203200 $.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 ) );
205202 } );
206203 return pairs.join( '&' );
207204 },
@@ -210,14 +207,7 @@
211208 * @return {String}
212209 */
213210 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, '' );
222212 },
223213
224214 /**

Status & tagging log