Show
Ignore:
Timestamp:
06/14/08 02:45:51 (5 years ago)
Author:
dankogai
Message:

string is escaped more carefully (and Firefox Compliant)

Location:
lang/javascript/clone/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/clone/trunk/uneval.html

    r2060 r13839  
    11<head> 
     2<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"> 
    23<script src="uneval.js"></script> 
    34<script> 
     
    1314    'boolean'  :false, 
    1415    'number'   :1, 
    15     'string'   :'Can\'t you "uneval" this? \\', 
     16    'string'   :'Can\'t you\n\t"uneval" this? façade\\漢字', 
    1617    'null'     :null, 
    1718    'undefined':undefined, 
     
    5657</dd> 
    5758<dt>stdout:</dt> 
    58 <dd><div id="stdout"></div></dd> 
     59<dd><pre id="stdout"></pre></dd> 
    5960<dt>stderr:</dt> 
    6061<dd><div id="stderr"></div></dd> 
  • lang/javascript/clone/trunk/uneval.js

    r12158 r13839  
    33 */ 
    44 
    5 (function(){ 
    6     // if uneval is predefined, skip it 
    7     try{ eval('uneval'); return; }catch(e){}; 
    8  
     5try{  
     6    eval('uneval');  
     7}catch(e){ 
    98    var protos = []; 
     9    var char2esc = {'\t':'t','\n':'n','\v':'v','\f':'f','\r':'\r',     
     10                    '\'':'\'','\"':'\"','\\':'\\'}; 
     11    var escapeChar = function(c){ 
     12        if (c in char2esc) return '\\' + char2esc[c]; 
     13        var ord = c.charCodeAt(0); 
     14        return ord < 0x20   ? '\\x0' + ord.toString(16) 
     15            :  ord < 0x7F   ? '\\'   + c 
     16            :  ord < 0x100  ? '\\x'  + ord.toString(16) 
     17            :  ord < 0x1000 ? '\\u0' + ord.toString(16) 
     18                            : '\\u'  + ord.toString(16) 
     19    }; 
    1020    var uneval_asis = function(o){ return o.toString() }; 
    11  
    1221    /* predefine objects where typeof(o) != 'object' */ 
    1322    var name2uneval = { 
     
    1524        'number': uneval_asis, 
    1625        'string': function(o){ 
    17             return '\''  
    18             + o.toString().replace(/[\\\"\']/g, function(m0){ 
    19                 return '\\' + m0; 
    20             })  
    21             + '\''; 
     26            return '\'' 
     27               + o.toString().replace(/[\x00-\x1F\'\"\\\u007F-\uFFFF]/g, escapeChar) 
     28               + '\'' 
    2229        }, 
    2330        'undefined': function(o){ return 'undefined' }, 
     
    6875        return func(o, np); 
    6976    } 
    70 })(); 
     77} 
    7178 
    72 (function(){ 
    73     try{ eval('clone'); return; }catch(e){}; 
     79try{  
     80    eval('clone'); 
     81} 
     82catch(e){ 
    7483    clone = function(o){ 
    75         try{ 
    76             return eval(uneval(o)); 
    77         }catch(e){ 
    78             throw(e); 
    79         } 
     84        try{ 
     85            return eval(uneval(o)); 
     86        }catch(e){ 
     87            throw(e); 
     88        } 
    8089    }; 
    81 })(); 
     90}