encodeURIComponent算法源代码 [英] encodeURIComponent algorithm source code

查看:169
本文介绍了encodeURIComponent算法源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Javascript开发钛的应用程序。我需要一个开源实现的 encodeURIComponent 在Javascript中。



任何人可以指导我或向我展示一些实现? / p>

解决方案

V8实现可从 src / uri.js ,并根据BSD许可证分发。

 code> // ECMA-262  -  15.1.3.4 
函数URIEncodeComponent(component){
var unescapePredicate = function(cc){
if(isAlphaNumeric(cc))return true;
//!
if(cc == 33)return true;
//'()*
if(39< = cc&& cc< = 42)返回true;
// - 。
if(45< = cc&& cc< = 46)返回true;
// _
if(cc == 95)return true;
//〜
if(cc == 126)return true;

返回false;
};

var string = ToString(component);
return Encode(string,unescapePredicate);
}

没有调用 encodeURIComponent 那里,但同一个文件中的这个代码可以确定映射:

  InstallFunctions(全局,DONT_ENUM,$ Array(
escape,URIEscape,
unes​​cape,URIUnescape,
decodeURI,URIDecode,
decodeURIComponent,URIDecodeComponent,
encodeURI,URIEncode,
encodeURIComponent,URIEncodeComponent
));

此功能的规范位于 15.1.3.4


I am developing an application in titanium using Javascript. I need an open source implementation of encodeURIComponent in Javascript.

Can anybody guide me or show me some implementation?

解决方案

The V8 implementation is available at line 327 of src/uri.js and is distributed under the BSD license.

// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
  var unescapePredicate = function(cc) {
    if (isAlphaNumeric(cc)) return true;
    // !
    if (cc == 33) return true;
    // '()*
    if (39 <= cc && cc <= 42) return true;
    // -.
    if (45 <= cc && cc <= 46) return true;
    // _
    if (cc == 95) return true;
    // ~
    if (cc == 126) return true;

    return false;
  };

  var string = ToString(component);
  return Encode(string, unescapePredicate);
}

It's not called encodeURIComponent there, but this code in the same file, esablishes the mapping:

InstallFunctions(global, DONT_ENUM, $Array(
    "escape", URIEscape,
    "unescape", URIUnescape,
    "decodeURI", URIDecode,
    "decodeURIComponent", URIDecodeComponent,
    "encodeURI", URIEncode,
    "encodeURIComponent", URIEncodeComponent
  ));

The specification for this function is in 15.1.3.4.

这篇关于encodeURIComponent算法源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆