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) {