From: Krinkle Date: Mon, 8 Nov 2010 18:13:40 +0000 (+0000) Subject: As per r 75486 CR comments, no prototyping in mw core. X-Git-Tag: 1.31.0-rc.0~33984 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=d8998d9ac35bd43aa06a338bca0843e8a0eae4da;p=lhc%2Fweb%2Fwiklou.git 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) --- diff --git a/resources/mediawiki.util/mediawiki.util.js b/resources/mediawiki.util/mediawiki.util.js index c09fc7021a..abf82c4f73 100644 --- a/resources/mediawiki.util/mediawiki.util.js +++ b/resources/mediawiki.util/mediawiki.util.js @@ -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] ); diff --git a/resources/mediawiki.util/mediawiki.util.test.js b/resources/mediawiki.util/mediawiki.util.test.js index 74b8435a26..dc4eab6d13 100644 --- a/resources/mediawiki.util/mediawiki.util.test.js +++ b/resources/mediawiki.util/mediawiki.util.test.js @@ -28,7 +28,7 @@ contain = result; } this.addedTests.push([code, result, contain]); - this.$table.append('' + mw.html.escape(code) + '' + mw.html.escape(result) + '?'); + this.$table.append('' + mw.html.escape(code).replace(/ /g, '  ') + '' + mw.html.escape(result).replace(/ /g, '  ') + '?'); }, /* Initialisation */ @@ -45,21 +45,27 @@ mw.util.$content.html( '

Below is a list of tests to confirm proper functionality of the mediaWiki.util functions

' + '
' + - '
ExecShould returnDoes returnEqual ?
' + '' + + '' + + '
ExecShould returnDoes returnEqual ?
' ); 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', - 'function (string)'); - mw.test.addTest('typeof Array.prototype.compare', - 'function (string)'); - mw.test.addTest('typeof Array.prototype.indexOf', + mw.test.addTest('$.trimRight(\' foo bar \')', + ' foo bar (string)'); + mw.test.addTest('typeof $.compareArray', '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)'); diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index d83eac0561..55754e222f 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -1,44 +1,37 @@ /* - * 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*$/, ""); - }; -} - -/* - * 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"); - }; -} +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; + } +}); /* * Core MediaWiki JavaScript Library