http://www.mediawiki.org/wiki/Special:Code/MediaWiki/76320
Revision: 76320
Author: krinkle
Date: 2010-11-08 18:13:40 +0000 (Mon, 08 Nov 2010)
Log Message:
-----------
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/mediawiki.js
trunk/phase3/resources/mediawiki.util/mediawiki.util.js
trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
Modified: trunk/phase3/resources/mediawiki/mediawiki.js
===================================================================
--- trunk/phase3/resources/mediawiki/mediawiki.js 2010-11-08 18:05:48 UTC
(rev 76319)
+++ trunk/phase3/resources/mediawiki/mediawiki.js 2010-11-08 18:13:40 UTC
(rev 76320)
@@ -1,46 +1,39 @@
/*
- * JavaScript backwards-compatibility and support
+ * JavaScript backwards-compatibility alternatives and convenience functions
*/
-// Implementation of string trimming functionality introduced natively in
JavaScript 1.8.1
-if ( typeof String.prototype.trim === 'undefined' ) {
- // Add removing trailing and leading whitespace functionality
cross-browser
- // See also:
http://blog.stevenlevithan.com/archives/faster-trim-javascript
- String.prototype.trim = function() {
- return this.replace( /^\s+|\s+$/g, '' );
- };
-}
-if ( typeof String.prototype.trimLeft === 'undefined' ) {
- String.prototype.trimLeft = function() {
- return this.replace( /^\s\s*/, "" );
- };
-}
-if ( typeof String.prototype.trimRight === 'undefined' ) {
- String.prototype.trimRight = function() {
- return this.replace(/\s\s*$/, "");
- };
-}
+jQuery.extend({
+ trimLeft : function( str ) {
+ return str == null ? '' : str.toString().replace( /^\s+/, '' );
+ },
+ trimRight : function( str ) {
+ return str == null ?
+ '' : str.toString().replace( /\s+$/, '' );
+ },
+ ucFirst : function( str ) {
+ return str.substr( 0, 1 ).toUpperCase() + str.substr( 1,
str.length );
+ },
+ escapeRE : function( str ) {
+ return str.replace ( /([\\{}()|.?*+^$\[\]])/g, "\\$1" );
+ },
+ compareArray : function( arrThis, arrAgainst ) {
+ if ( arrThis.length != arrAgainst.length ) {
+ return false;
+ }
+ for ( var i = 0; i < arrThis.length; i++ ) {
+ if ( arrThis[i] instanceof Array ) {
+ if ( !$.compareArray( arrThis[i], arrAgainst[i]
) ) {
+ return false;
+ }
+ } else if ( arrThis[i] !== arrAgainst[i] ) {
+ return false;
+ }
+ }
+ return true;
+ }
+});
/*
- * Prototype enhancements
- */
-
-// Capitalize the first character of the given string
-if ( typeof String.prototype.ucFirst === 'undefined' ) {
- String.prototype.ucFirst = function() {
- return this.substr(0, 1).toUpperCase() + this.substr(1,
this.length);
- };
-}
-
-// Escape all RegExp special characters such that the result can be safely used
-// in a RegExp as a literal.
-if ( typeof String.prototype.escapeRE === 'undefined' ) {
- String.prototype.escapeRE = function() {
- return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1");
- };
-}
-
-/*
* Core MediaWiki JavaScript Library
*/
Modified: trunk/phase3/resources/mediawiki.util/mediawiki.util.js
===================================================================
--- trunk/phase3/resources/mediawiki.util/mediawiki.util.js 2010-11-08
18:05:48 UTC (rev 76319)
+++ trunk/phase3/resources/mediawiki.util/mediawiki.util.js 2010-11-08
18:13:40 UTC (rev 76320)
@@ -140,7 +140,7 @@
'getParamValue' : function( param, url ) {
url = url ? url : document.location.href;
// Get last match, stop at hash
- var re = new RegExp( '[^#]*[&?]' + param.escapeRE() +
'=([^&#]*)' );
+ var re = new RegExp( '[^#]*[&?]' + $.escapeRE( param )
+ '=([^&#]*)' );
var m = re.exec( url );
if ( m && m.length > 1 ) {
return decodeURIComponent( m[1] );
Modified: trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
===================================================================
--- trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
2010-11-08 18:05:48 UTC (rev 76319)
+++ trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js
2010-11-08 18:13:40 UTC (rev 76320)
@@ -28,7 +28,7 @@
contain = result;
}
this.addedTests.push([code, result, contain]);
- this.$table.append('<tr><td>' + mw.html.escape(code) +
'</td><td>' + mw.html.escape(result) + '<td></td></td><td>?</td></tr>');
+ this.$table.append('<tr><td>' +
mw.html.escape(code).replace(/ /g, ' ') + '</td><td>' +
mw.html.escape(result).replace(/ /g, ' ') +
'<td></td></td><td>?</td></tr>');
},
/* Initialisation */
@@ -45,21 +45,27 @@
mw.util.$content.html(
'<p>Below is a list of
tests to confirm proper functionality of the mediaWiki.util functions</p>' +
'<hr />' +
- '<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>'
+ '<table
id="mw-mwutiltest-table" class="wikitable sortable" style="white-space:break;
font-family:monospace,\'Courier New\'">' +
+
'<tr><th>Exec</th><th>Should return</th><th>Does return</th><th>Equal
?</th></tr>' +
+ '</table>'
);
mw.test.$table =
$('table#mw-mwutiltest-table');
// Populate tests
- mw.test.addTest('typeof
String.prototype.trim',
+ mw.test.addTest('typeof
$.trimLeft',
'function (string)');
- mw.test.addTest('typeof
String.prototype.trimLeft',
+ mw.test.addTest('$.trimLeft(\'
foo bar \')',
+ 'foo bar (string)');
+ mw.test.addTest('typeof
$.trimRight',
'function (string)');
- mw.test.addTest('typeof
String.prototype.trimRight',
+ mw.test.addTest('$.trimRight(\'
foo bar \')',
+ ' foo bar (string)');
+ mw.test.addTest('typeof
$.compareArray',
'function (string)');
- mw.test.addTest('typeof
Array.prototype.compare',
- 'function (string)');
- mw.test.addTest('typeof
Array.prototype.indexOf',
- 'function (string)');
+
mw.test.addTest('$.compareArray( [1, "a", [], [2, \'b\'] ], [1, \'a\', [], [2,
"b"] ] )',
+ 'true (boolean)');
+
mw.test.addTest('$.compareArray( [1], [2] )',
+ 'false (boolean)');
mw.test.addTest('4',
'4 (number)');
mw.test.addTest('typeof
mediaWiki',
@@ -70,13 +76,13 @@
'object (string)');
mw.test.addTest('typeof
mw.html',
'object (string)');
- mw.test.addTest('typeof
String.prototype.ucFirst',
+ mw.test.addTest('typeof
$.ucFirst',
'function (string)');
-
mw.test.addTest('\'mediawiki\'.ucFirst()',
+ mw.test.addTest('$.ucFirst(
\'mediawiki\' )',
'Mediawiki (string)');
- mw.test.addTest('typeof
String.prototype.escapeRE',
+ mw.test.addTest('typeof
$.escapeRE',
'function (string)');
-
mw.test.addTest('\'.st{e}$st\'.escapeRE()',
+ mw.test.addTest('$.escapeRE(
\'.st{e}$st\' )',
'\\.st\\{e\\}\\$st
(string)');
mw.test.addTest('typeof
$.fn.checkboxShiftClick',
'function (string)');
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs