r76320 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76319‎ | r76320 | r76321 >
Date:18:13, 8 November 2010
Author:krinkle
Status:ok
Tags:
Comment:
As per r 75486 CR comments, no prototyping in mw core.
* Removing those already in jQuery
* Moved othere to jQuery (including Array.compare which was deleted earlier)
* Added tests to the Test Suite
(Follow-up on r75294, r75552, r75486, r75294)
Modified paths:
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.js (modified) (history)
  • /trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js (modified) (history)
  • /trunk/phase3/resources/mediawiki/mediawiki.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki/mediawiki.js
@@ -1,46 +1,39 @@
22 /*
3 - * JavaScript backwards-compatibility and support
 3+ * JavaScript backwards-compatibility alternatives and convenience functions
44 */
55
6 -// Implementation of string trimming functionality introduced natively in JavaScript 1.8.1
7 -if ( typeof String.prototype.trim === 'undefined' ) {
8 - // Add removing trailing and leading whitespace functionality cross-browser
9 - // See also: http://blog.stevenlevithan.com/archives/faster-trim-javascript
10 - String.prototype.trim = function() {
11 - return this.replace( /^\s+|\s+$/g, '' );
12 - };
13 -}
14 -if ( typeof String.prototype.trimLeft === 'undefined' ) {
15 - String.prototype.trimLeft = function() {
16 - return this.replace( /^\s\s*/, "" );
17 - };
18 -}
19 -if ( typeof String.prototype.trimRight === 'undefined' ) {
20 - String.prototype.trimRight = function() {
21 - return this.replace(/\s\s*$/, "");
22 - };
23 -}
 6+jQuery.extend({
 7+ trimLeft : function( str ) {
 8+ return str == null ? '' : str.toString().replace( /^\s+/, '' );
 9+ },
 10+ trimRight : function( str ) {
 11+ return str == null ?
 12+ '' : str.toString().replace( /\s+$/, '' );
 13+ },
 14+ ucFirst : function( str ) {
 15+ return str.substr( 0, 1 ).toUpperCase() + str.substr( 1, str.length );
 16+ },
 17+ escapeRE : function( str ) {
 18+ return str.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
 19+ },
 20+ compareArray : function( arrThis, arrAgainst ) {
 21+ if ( arrThis.length != arrAgainst.length ) {
 22+ return false;
 23+ }
 24+ for ( var i = 0; i < arrThis.length; i++ ) {
 25+ if ( arrThis[i] instanceof Array ) {
 26+ if ( !$.compareArray( arrThis[i], arrAgainst[i] ) ) {
 27+ return false;
 28+ }
 29+ } else if ( arrThis[i] !== arrAgainst[i] ) {
 30+ return false;
 31+ }
 32+ }
 33+ return true;
 34+ }
 35+});
2436
2537 /*
26 - * Prototype enhancements
27 - */
28 -
29 -// Capitalize the first character of the given string
30 -if ( typeof String.prototype.ucFirst === 'undefined' ) {
31 - String.prototype.ucFirst = function() {
32 - return this.substr(0, 1).toUpperCase() + this.substr(1, this.length);
33 - };
34 -}
35 -
36 -// Escape all RegExp special characters such that the result can be safely used
37 -// in a RegExp as a literal.
38 -if ( typeof String.prototype.escapeRE === 'undefined' ) {
39 - String.prototype.escapeRE = function() {
40 - return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1");
41 - };
42 -}
43 -
44 -/*
4538 * Core MediaWiki JavaScript Library
4639 */
4740
Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
@@ -28,7 +28,7 @@
2929 contain = result;
3030 }
3131 this.addedTests.push([code, result, contain]);
32 - this.$table.append('<tr><td>' + mw.html.escape(code) + '</td><td>' + mw.html.escape(result) + '<td></td></td><td>?</td></tr>');
 32+ this.$table.append('<tr><td>' + mw.html.escape(code).replace(/ /g, '&nbsp;&nbsp;') + '</td><td>' + mw.html.escape(result).replace(/ /g, '&nbsp;&nbsp;') + '<td></td></td><td>?</td></tr>');
3333 },
3434
3535 /* Initialisation */
@@ -45,21 +45,27 @@
4646 mw.util.$content.html(
4747 '<p>Below is a list of tests to confirm proper functionality of the mediaWiki.util functions</p>' +
4848 '<hr />' +
49 - '<table id="mw-mwutiltest-table" class="wikitable sortable"><tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr></table>'
 49+ '<table id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break; font-family:monospace,\'Courier New\'">' +
 50+ '<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal ?</th></tr>' +
 51+ '</table>'
5052 );
5153 mw.test.$table = $('table#mw-mwutiltest-table');
5254
5355 // Populate tests
54 - mw.test.addTest('typeof String.prototype.trim',
 56+ mw.test.addTest('typeof $.trimLeft',
5557 'function (string)');
56 - mw.test.addTest('typeof String.prototype.trimLeft',
 58+ mw.test.addTest('$.trimLeft(\' foo bar \')',
 59+ 'foo bar (string)');
 60+ mw.test.addTest('typeof $.trimRight',
5761 'function (string)');
58 - mw.test.addTest('typeof String.prototype.trimRight',
 62+ mw.test.addTest('$.trimRight(\' foo bar \')',
 63+ ' foo bar (string)');
 64+ mw.test.addTest('typeof $.compareArray',
5965 'function (string)');
60 - mw.test.addTest('typeof Array.prototype.compare',
61 - 'function (string)');
62 - mw.test.addTest('typeof Array.prototype.indexOf',
63 - 'function (string)');
 66+ mw.test.addTest('$.compareArray( [1, "a", [], [2, \'b\'] ], [1, \'a\', [], [2, "b"] ] )',
 67+ 'true (boolean)');
 68+ mw.test.addTest('$.compareArray( [1], [2] )',
 69+ 'false (boolean)');
6470 mw.test.addTest('4',
6571 '4 (number)');
6672 mw.test.addTest('typeof mediaWiki',
@@ -70,13 +76,13 @@
7177 'object (string)');
7278 mw.test.addTest('typeof mw.html',
7379 'object (string)');
74 - mw.test.addTest('typeof String.prototype.ucFirst',
 80+ mw.test.addTest('typeof $.ucFirst',
7581 'function (string)');
76 - mw.test.addTest('\'mediawiki\'.ucFirst()',
 82+ mw.test.addTest('$.ucFirst( \'mediawiki\' )',
7783 'Mediawiki (string)');
78 - mw.test.addTest('typeof String.prototype.escapeRE',
 84+ mw.test.addTest('typeof $.escapeRE',
7985 'function (string)');
80 - mw.test.addTest('\'.st{e}$st\'.escapeRE()',
 86+ mw.test.addTest('$.escapeRE( \'.st{e}$st\' )',
8187 '\\.st\\{e\\}\\$st (string)');
8288 mw.test.addTest('typeof $.fn.checkboxShiftClick',
8389 'function (string)');
Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
@@ -140,7 +140,7 @@
141141 'getParamValue' : function( param, url ) {
142142 url = url ? url : document.location.href;
143143 // Get last match, stop at hash
144 - var re = new RegExp( '[^#]*[&?]' + param.escapeRE() + '=([^&#]*)' );
 144+ var re = new RegExp( '[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' );
145145 var m = re.exec( url );
146146 if ( m && m.length > 1 ) {
147147 return decodeURIComponent( m[1] );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r75294added string trimming for older browserskrinkle21:08, 23 October 2010
r75486Introducing mediaWiki.language, and mediaWiki.message which are modeled after...tparscal23:40, 26 October 2010
r75552Fixed unfinished line from r75275, commented addPortletLink() more, moved pro...krinkle15:35, 27 October 2010

Status & tagging log