Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11524: 0012-patch-for-isJSON.patch

File 0012-patch-for-isJSON.patch, 1.3 kB (added by kangax, 3 months ago)
  • a/src/string.js

    old new  
    195195  unfilterJSON: function(filter) { 
    196196    return this.sub(filter || Prototype.JSONFilter, '#{1}'); 
    197197  }, 
    198  
    199   isJSON: function() { 
    200     var str = this; 
    201     if (str.blank()) return false; 
    202     str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); 
    203     return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); 
    204   }, 
     198   
     199  isJSON: (function() { 
     200    var escapes = /\\["\\\/bfnrtu]/g; 
     201    var values = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g; 
     202    var brackets = /(?:^|:|,)(?:\s*\[)+/g; 
     203    var invalid = /^[\],:{}\s]*$/; 
     204     
     205    return function() { 
     206      if (this.blank()) return false; 
     207      return invalid.test( 
     208        this.replace(escapes, '@').replace(values, ']').replace(brackets, '') 
     209      ) 
     210    } 
     211  })(), 
    205212   
    206213  evalJSON: function(sanitize) { 
    207214    var json = this.unfilterJSON();