#300 ✓wont_fix
Phred

Fix String#replace to support functions in Safari 2

Reported by Phred | August 22nd, 2008 @ 11:59 AM

Birthed from #297

Safari 2.x does not support functions as the 2nd param of String#replace. The following code attempts to remedy this:


if ('a'.replace(/a/, function() {return 'b'}) != 'b')
(function(replace){
  String.prototype.replace = function(pattern, replacement) {
    // replacement is not function, use built-in
    if (typeof replacement != 'function')
      return replace.apply(this, arguments);

    var str = '' + this;
    // pattern string is not RegExp
    if (!(pattern instanceof RegExp)) {
      var idx = str.indexOf(pattern);
      return (idx == -1 ?
        str :
        replace.apply(str, [pattern, replacement(pattern, idx, str)]);
      );
    }

    var reg = pattern, result = [], lastidx = reg.lastIndex, re;
    while ((re = reg.exec(str)) != null) {
      var idx  = re.index, args = re.concat(idx, str);
      result.push(str.slice(lastidx, idx), replacement.apply(null, args).toString());
      if (!reg.global) {
        lastidx += RegExp.lastMatch.length;
        break;
      }
      else {
        lastidx = reg.lastIndex;
      }
    }
    result.push(str.slice(lastidx));
    return result.join('');
  }
})(String.prototype.replace);

The exchange is performance (failing quickly with the built-in method) for functionality (working as spec'd).

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel August 22nd, 2008 @ 11:58 AM

    • State changed from “new” to “enhancement”
  • Tobie Langel

    Tobie Langel August 22nd, 2008 @ 12:20 PM

    • Tag changed from blocker297, enhancement, needs_benchmarks, needs_patch, needs_tests, safari2, string to needs_docs, needs_patch, needs_tests, safari2, string

    This actually might be only a Safari 1.2 bug.

  • Juriy Zaytsev

    Juriy Zaytsev August 22nd, 2008 @ 02:00 PM

    
    
    javascript:alert('a'.replace(/a/,function(){return 'b'}));
    
    // Safari 2.0
    function(){return 'b'};
    
    // Safari 2.0.2
    function(){return 'b'};
    
    // Safari 2.0.3
    'b'
    
    
  • Tobie Langel

    Tobie Langel July 23rd, 2009 @ 04:10 AM

    • State changed from “enhancement” to “wont_fix”

    Closing as wont_fix since we're dropping support for Safari < 2.0.4

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:25 AM

    • Tag changed from needs_docs, needs_patch, needs_tests, safari2, string to missing:documentation, needs_patch, needs_tests, safari2

    [not-tagged:"needs_docs" tagged:"missing:documentation" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from missing:documentation, needs_patch, needs_tests, safari2 to missing:documentation, missing:tests, needs_patch, safari2

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:28 AM

    • Tag changed from missing:documentation, missing:tests, needs_patch, safari2 to missing:documentation, missing:patch, missing:tests, safari2

    [not-tagged:"needs_patch" tagged:"missing:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:39 AM

    • Tag changed from missing:documentation, missing:patch, missing:tests, safari2 to missing:documentation, missing:patch, missing:tests
  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from missing:documentation, missing:patch, missing:tests to missing:documentation, missing:patch, needs:tests

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:37 AM

    • Tag changed from missing:documentation, missing:patch, needs:tests to missing:documentation, needs:patch, needs:tests

    [not-tagged:"missing:patch" tagged:"needs:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:38 AM

    • Tag changed from missing:documentation, needs:patch, needs:tests to needs:doc, needs:patch, needs:tests

    [not-tagged:"missing:documentation" tagged:"needs:doc" bulk edit command]

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

People watching this ticket

Referenced by

Pages