From 359c66ed52b75b14c81a89c93706ff78647119e9 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Tue, 11 Sep 2007 11:16:51 +0000 Subject: [PATCH] Pull in upstream fix from Prototype, patch by fredj (thanks fredj!) to fix String.prototype.trim. (Closes #961) Note that we should also be more careful not to clobber other library prototypes. (See #962) I'm going to check this in to fix the bug for 2.5, and we'll work on the latter in 2.6. git-svn-id: http://svn.openlayers.org/trunk/openlayers@4213 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes.js | 13 +------------ tests/test_BaseTypes.html | 4 +++- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/lib/OpenLayers/BaseTypes.js b/lib/OpenLayers/BaseTypes.js index c4c2976bac..4537e6a99c 100644 --- a/lib/OpenLayers/BaseTypes.js +++ b/lib/OpenLayers/BaseTypes.js @@ -51,18 +51,7 @@ String.prototype.contains = function(str) { * trailing spaces removed */ String.prototype.trim = function() { - - var b = 0; - while(this.substr(b,1) == " ") { - b++; - } - - var e = this.length - 1; - while(this.substr(e,1) == " ") { - e--; - } - - return this.substring(b, e+1); + return this.replace(/^\s+/, '').replace(/\s+$/, ''); }; /** diff --git a/tests/test_BaseTypes.html b/tests/test_BaseTypes.html index e633229aaf..7a6879cd11 100644 --- a/tests/test_BaseTypes.html +++ b/tests/test_BaseTypes.html @@ -28,7 +28,7 @@ } function test_03_String_trim(t) { - t.plan(4); + t.plan(5); var str = "chickenHead"; t.eq(str.trim(), "chickenHead", "string with no extra whitespace is left alone"); @@ -42,6 +42,8 @@ str = " chickenHead "; t.eq(str.trim(), "chickenHead", "string with extra whitespace at beginning and end is trimmed correctly"); + str = " "; + t.eq(str.trim(), "", "whitespace string is trimmed correctly"); } function test_04_String_indexOf(t) {