Shim for JavaScript .escape函数

时间:2014-05-04 02:31:44

标签: javascript escaping shim

根据此mdn doc全球.escape()功能:' ...已从网络中删除。虽然有些浏览器可能仍然支持它,但它正在被删除...' ,正如弃用通知所说。我有一些工作代码严重依赖于可用的功能。看看es5 escape(string) docs我把这段代码放在一起,希望能保持这个功能的活着,而不是重写我的工作代码。 问题是,当然,这个版本做的事情很快就会被删除.escape()功能&#39 ;?这是我提出的垫片:

//
// these 69 characters are left as is by native implementation
var safe69chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";

// equivalent hash map for speeding up character lookup
var chars69  = {0:1,1:1,2:1,3:1,4:1,5:1,6:1,7:1,8:1,9:1,A:1,B:1,C:1,D:1,E:1,F:1,G:1,H:1,I:1,J:1,K:1,L:1,M:1,N:1,O:1,P:1,Q:1,R:1,S:1,T:1,U:1,V:1,W:1,X:1,Y:1,Z:1,a:1,b:1,c:1,d:1,e:1,f:1,g:1,h:1,i:1,j:1,k:1,l:1,m:1,n:1,o:1,p:1,q:1,r:1,s:1,t:1,u:1,v:1,w:1,x:1,y:1,z:1,"@":1,"*":1,_:1,"+":1,"-":1,".":1,"/":1};

// these 'percent escapes' map to lower Array<int> list
// and their coresponding (funky) characters are processed by ecape function
var percesc  = "%00%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20%21%22%23%24%25%26%27%28%29%2C%3A%3B%3C%3D%3E%3F%5B%5C%5D%5E%60%7B%7C%7D%7E%7F%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF";
var ordesc   = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 44, 58, 59, 60, 61, 62, 63, 91, 92, 93, 94, 96, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255];

var isfunc = function (node) {
  return "function" == typeof node;
};

var pad02;
var pad04;


// here check's if it has global .escape, 
// if leaves 'safe69chars' characters unescaped, 
// if correctly escapes other characters, 
// and (re)defines the function if asserts don't pass
if (

  !("escape" in window)  || 
  !isfunc(window.escape) || 
  (safe69chars !== window.escape(safe69chars)) ||
  (percesc !== window.escape(ordesc.map(function(c) {
    return String.fromCharCode(c);
  }).join("")))

) {

  pad02 = function(c) {
    //return Array(3).splice(c.length).join("0") + c;
    return (Array(3).splice(c.length).join("0") + c).toUpperCase();
  };

  pad04 = function(c) {
    //return Array(5).splice(c.length).join("0") + c;
    return (Array(5).splice(c.length).join("0") + c).toUpperCase();
  };

  window.escape = function escape (str) {

    str += '';

    // loops each character 
    // escaping it if needed
    for (
      var 
        chr, chrcode, hexcode, i = -1,
        len = str.length, escaped = "";
        ++i < len;
    ) {

      chr = str.charAt(i);

      // use hash lookup to speed up search a bit
      if (chars69.hasOwnProperty(chr)) {

        // if it's in chars69 append it
        escaped += chr;

      } else {

        // get the character's code and it's hex value
        chrcode = chr.charCodeAt(0);
        hexcode = chrcode.toString(16);

        if (chrcode < 256) {

          // pad with '0', length 2 if less than 256 and append
          escaped += ('%' + pad02(hexcode));

        } else {

          // pad with '0', length 4 otherwise and append
          escaped += ('%u' + pad04(hexcode));

        }

      }

    }

    return escaped;
  };

}
//?
//

1 个答案:

答案 0 :(得分:0)

@ jfriend00 ,你说得对,我没办法进行测试。它构建一个2 ** 16(= 65536)字符串,并在其上运行两个版本,比较(===)输出。原生.escape()实际上是超级六角字母(错过了上面的代码片段)。在这里:

//
//
var safe69chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./";

var pad02 = function(c) {
  return (Array(3).splice(c.length).join("0") + c).toUpperCase();
};

var pad04 = function(c) {
  return (Array(5).splice(c.length).join("0") + c).toUpperCase();
};


function esc (str) {

  str += '';

  for (
    var chr, chrcode, hexcode, i = -1,
      len = str.length, escaped = "";
    ++i < len;
  ) {

    chr = str.charAt(i);

    if (-1 != safe69chars.indexOf(chr)) {
      escaped += chr;
    } else {

      chrcode = chr.charCodeAt(0);
      hexcode = chrcode.toString(16);

      if (chrcode < 256) {
        escaped += ('%' + pad02(hexcode));
      } else {
        escaped += ('%u' + pad04(hexcode));
      }
    }
  }

  return escaped;
}


var mkrange = function (len) {
  return Array.prototype.map.call(
    Array(len+1).join("1"), 
    function (one, i) {return i;}
  )
};

var rng = mkrange(Math.pow(2, 16));

// builds 65536 character string
var str = rng.map(function (c) {return String.fromCharCode(c)}).join("");

// runs the test:
esc(str) === escape(str);
// true
//