From fc91a3e456ef26d0fe0296e4959fadfa2d081dd0 Mon Sep 17 00:00:00 2001 From: Thomas Fuchs Date: Tue, 27 Mar 2007 17:43:30 +0000 Subject: [PATCH] Fix an issue with String.prototype.endsWith. Closes #7822. [altblue] --- CHANGELOG | 4 ++++ src/string.js | 5 +++-- test/unit/string.html | 2 ++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index f3ff21c..cbf2a5b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +*SVN* + +* Fix an issue with String.prototype.endsWith. Closes #7822. [altblue] + *1.5.1_rc2* (March 12, 2007) * Add a tab character via innerHTML to the selector whitespace test. [Christophe Porteneuve] diff --git a/src/string.js b/src/string.js index 266607b..df20371 100644 --- a/src/string.js +++ b/src/string.js @@ -176,11 +176,12 @@ Object.extend(String.prototype, { }, startsWith: function(pattern) { - return this.indexOf(pattern) == 0; + return this.indexOf(pattern) === 0; }, endsWith: function(pattern) { - return this.lastIndexOf(pattern) == (this.length - pattern.length); + var d = this.length - pattern.length; + return d >= 0 && this.lastIndexOf(pattern) === d; }, empty: function() { diff --git a/test/unit/string.html b/test/unit/string.html index a92e55a..90bacdd 100644 --- a/test/unit/string.html +++ b/test/unit/string.html @@ -342,6 +342,7 @@ assert('hello world'.startsWith('hello')); assert(!'hello world'.startsWith('bye')); assert(!''.startsWith('bye')); + assert(!'hell'.startsWith('hello')); }}, testEndsWith: function() {with(this) { @@ -350,6 +351,7 @@ assert(!'hello world'.endsWith('planet')); assert(!''.endsWith('planet')); assert('hello world world'.endsWith(' world')); + assert(!'z'.endsWith('az')); }}, testBlank: function() { with(this) {