| Index: trunk/phase3/resources/mediawiki/mediawiki.js |
| — | — | @@ -1,46 +1,39 @@ |
| 2 | 2 | /* |
| 3 | | - * JavaScript backwards-compatibility and support |
| | 3 | + * JavaScript backwards-compatibility alternatives and convenience functions |
| 4 | 4 | */ |
| 5 | 5 | |
| 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 | +}); |
| 24 | 36 | |
| 25 | 37 | /* |
| 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 | | -/* |
| 45 | 38 | * Core MediaWiki JavaScript Library |
| 46 | 39 | */ |
| 47 | 40 | |
| Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.test.js |
| — | — | @@ -28,7 +28,7 @@ |
| 29 | 29 | contain = result; |
| 30 | 30 | } |
| 31 | 31 | 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, ' ') + '</td><td>' + mw.html.escape(result).replace(/ /g, ' ') + '<td></td></td><td>?</td></tr>'); |
| 33 | 33 | }, |
| 34 | 34 | |
| 35 | 35 | /* Initialisation */ |
| — | — | @@ -45,21 +45,27 @@ |
| 46 | 46 | mw.util.$content.html( |
| 47 | 47 | '<p>Below is a list of tests to confirm proper functionality of the mediaWiki.util functions</p>' + |
| 48 | 48 | '<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>' |
| 50 | 52 | ); |
| 51 | 53 | mw.test.$table = $('table#mw-mwutiltest-table'); |
| 52 | 54 | |
| 53 | 55 | // Populate tests |
| 54 | | - mw.test.addTest('typeof String.prototype.trim', |
| | 56 | + mw.test.addTest('typeof $.trimLeft', |
| 55 | 57 | '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', |
| 57 | 61 | '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', |
| 59 | 65 | '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)'); |
| 64 | 70 | mw.test.addTest('4', |
| 65 | 71 | '4 (number)'); |
| 66 | 72 | mw.test.addTest('typeof mediaWiki', |
| — | — | @@ -70,13 +76,13 @@ |
| 71 | 77 | 'object (string)'); |
| 72 | 78 | mw.test.addTest('typeof mw.html', |
| 73 | 79 | 'object (string)'); |
| 74 | | - mw.test.addTest('typeof String.prototype.ucFirst', |
| | 80 | + mw.test.addTest('typeof $.ucFirst', |
| 75 | 81 | 'function (string)'); |
| 76 | | - mw.test.addTest('\'mediawiki\'.ucFirst()', |
| | 82 | + mw.test.addTest('$.ucFirst( \'mediawiki\' )', |
| 77 | 83 | 'Mediawiki (string)'); |
| 78 | | - mw.test.addTest('typeof String.prototype.escapeRE', |
| | 84 | + mw.test.addTest('typeof $.escapeRE', |
| 79 | 85 | 'function (string)'); |
| 80 | | - mw.test.addTest('\'.st{e}$st\'.escapeRE()', |
| | 86 | + mw.test.addTest('$.escapeRE( \'.st{e}$st\' )', |
| 81 | 87 | '\\.st\\{e\\}\\$st (string)'); |
| 82 | 88 | mw.test.addTest('typeof $.fn.checkboxShiftClick', |
| 83 | 89 | 'function (string)'); |
| Index: trunk/phase3/resources/mediawiki.util/mediawiki.util.js |
| — | — | @@ -140,7 +140,7 @@ |
| 141 | 141 | 'getParamValue' : function( param, url ) { |
| 142 | 142 | url = url ? url : document.location.href; |
| 143 | 143 | // Get last match, stop at hash |
| 144 | | - var re = new RegExp( '[^#]*[&?]' + param.escapeRE() + '=([^&#]*)' ); |
| | 144 | + var re = new RegExp( '[^#]*[&?]' + $.escapeRE( param ) + '=([^&#]*)' ); |
| 145 | 145 | var m = re.exec( url ); |
| 146 | 146 | if ( m && m.length > 1 ) { |
| 147 | 147 | return decodeURIComponent( m[1] ); |