From 24225e880a4e81aec5c8ffabbf86b3b30e51aafd Mon Sep 17 00:00:00 2001 From: Krinkle Date: Sat, 23 Oct 2010 21:08:58 +0000 Subject: [PATCH] added string trimming for older browsers --- resources/mediawiki/mediawiki.js | 53 +++++++++++++++++++-------- resources/mediawiki/mediawiki.util.js | 31 +++++++++++++++- 2 files changed, 67 insertions(+), 17 deletions(-) diff --git a/resources/mediawiki/mediawiki.js b/resources/mediawiki/mediawiki.js index 3350bb21ad..ea452d648c 100644 --- a/resources/mediawiki/mediawiki.js +++ b/resources/mediawiki/mediawiki.js @@ -2,30 +2,39 @@ * JavaScript backwards-compatibility and support */ -// Make calling .indexOf() on an array work on older browsers -if ( typeof Array.prototype.indexOf === 'undefined' ) { - Array.prototype.indexOf = function( needle ) { - for ( var i = 0; i < this.length; i++ ) { - if ( this[i] === needle ) { - return i; - } - } - return -1; +// New fallback String trimming functionality, was introduced natively in JavaScript 1.8.1 +if (typeof String.prototype.trimx === '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*$/, ""); }; } + // Add array comparison functionality -if ( typeof Array.prototype.compare === 'undefined' ) { - Array.prototype.compare = function( against ) { - if ( this.length != against.length ) { +if (typeof Array.prototype.compare === 'undefined') { + Array.prototype.compare = function (against) { + if (this.length !== against.length) { return false; } - for ( var i = 0; i < against.length; i++ ) { - if ( this[i].compare ) { - if ( !this[i].compare( against[i] ) ) { + for (var i = 0; i < against.length; i++) { + if (this[i].compare) { + if (!this[i].compare(against[i])) { return false; } } - if ( this[i] !== against[i] ) { + if (this[i] !== against[i]) { return false; } } @@ -33,6 +42,18 @@ if ( typeof Array.prototype.compare === 'undefined' ) { }; } +// Make calling .indexOf() on an array work on older browsers +if (typeof Array.prototype.indexOf === 'undefined') { + Array.prototype.indexOf = function (needle) { + for (var i = 0; i < this.length; i++) { + if (this[i] === needle) { + return i; + } + } + return -1; + }; +} + /* * Core MediaWiki JavaScript Library */ diff --git a/resources/mediawiki/mediawiki.util.js b/resources/mediawiki/mediawiki.util.js index 5ebf9ed799..dd501fe718 100644 --- a/resources/mediawiki/mediawiki.util.js +++ b/resources/mediawiki/mediawiki.util.js @@ -1,4 +1,3 @@ -/*jslint white: true, browser: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true */ /* * Utilities */ @@ -44,6 +43,13 @@ return $box; }; + // Prototype enhancements + if (typeof String.prototype.ucFirst === 'undefined') { + String.prototype.ucFirst = function () { + return this.substr(0, 1).toUpperCase() + this.substr(1, this.length); + }; + } + // Any initialisation after the DOM is ready $(function () { $('input[type=checkbox]:not(.noshiftselect)').enableCheckboxShiftClick(); @@ -88,6 +94,29 @@ return wgServer + wgArticlePath.replace('$1', this.wikiUrlencode(str)); }, + /** + * Check is a variable is empty. Support for strings, booleans, arrays and objects. + * String "0" is considered empty. String containing only whitespace (ie. " ") is considered not empty. + * + * @param Mixed v the variable to check for empty ness + */ + 'isEmpty' : function (v) { + var key; + if (v === "" || v === 0 || v === "0" || v === null || v === false || typeof v === 'undefined') { + return true; + } + if (v.length === 0) { + return true; + } + if (typeof v === 'object') { + for (key in v) { + return false; + } + return true; + } + return false; + }, + /** * Grabs the url parameter value for the given parameter -- 2.20.1