Ticket #961 (closed bug: fixed)

Opened 6 years ago

Last modified 6 years ago

String.prototype.trim failed to trim a whitespace string

Reported by: fredj Owned by:
Priority: major Milestone: 2.5 Release
Component: general Version: SVN
Keywords: Cc:
State:

Description

As reported by John Cole on the dev mailing list:

I’ve run into an issue with OL 2.4 (branch HEAD) and Microsoft Ajax.Net.

In BaseTypes.js, the following function:
 
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);
};

Is failing because e is going negative and goes into an endless loop.  The
string being fed (from an Ajax.Net call) is simply two spaces (“  “).

It appears that Ajax.Net is also prototyping a trim function onto String,
and commenting out the one in BaseTypes.js gets everything working again.
How do you detect that a function has already been prototyped on to a class
so OL doesn’t have to do it?

Here is a test_String.html that confirms the issue.
<html>
  <head>
  <script src="../../lib/OpenLayers.js"></script>
  <script type="text/javascript"><!--
    function test_01_Trim (t) {
        t.plan( 1 );
        var s; 
        
        s = " test ".trim();
        t.eq( s, "test", "string.trim worked correctly");
    }
    function test_01a_Trim (t) {
        t.plan( 1 );
        var s;

        s = "  ".trim();
        t.eq( s, "", "string.trim worked correctly");
    }
  // -->
  </script>
  </head>
  <body></body>
</html>

Thanks,
John

Attachments

trim_whitespaces.patch Download (1.3 KB) - added by fredj 6 years ago.
bugfix and unit test, the trim code is from prototype.js
string_trim_24.patch Download (1.9 KB) - added by johnwebbcole 6 years ago.
Patch for OL 2.4 with test

Change History

Changed 6 years ago by fredj

bugfix and unit test, the trim code is from prototype.js

Changed 6 years ago by fredj

  • keywords review added

Changed 6 years ago by fredj

  • type changed from feature to bug
  • milestone set to 2.5 Release

Changed 6 years ago by crschmidt

  • keywords review removed
  • status changed from new to closed
  • resolution set to fixed

(In [4213]) 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.

Changed 6 years ago by johnwebbcole

Patch for OL 2.4 with test

Note: See TracTickets for help on using tickets.