From 911dd8ee6d170fff77cc5aff1fc95d9eb459df9b Mon Sep 17 00:00:00 2001 From: A1lo Date: Wed, 19 Jan 2022 01:04:35 +0800 Subject: Fix percent encoding and minor improvements (#3632) * fix: use uppercase hexadecimal digits rfc3986 specific that the percent encoding should use uppercase. Referenced Issues: #3032 * minor improvements Highlight the javascript code. Replace unicode space `U+00a0` with ASCII space. Replace `Specifications` and `Compat`. --- .../global_objects/encodeuricomponent/index.html | 58 ++++++---------------- 1 file changed, 15 insertions(+), 43 deletions(-) (limited to 'files/zh-cn/web/javascript/reference') diff --git a/files/zh-cn/web/javascript/reference/global_objects/encodeuricomponent/index.html b/files/zh-cn/web/javascript/reference/global_objects/encodeuricomponent/index.html index dc3f9a00a2..9ae0061beb 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/encodeuricomponent/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/encodeuricomponent/index.html @@ -9,7 +9,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/encodeURIComponent ---
{{jsSidebar("Objects")}}
-

encodeURIComponent()函数通过将一个,两个,三个或四个表示字符的UTF-8编码的转义序列替换某些字符的每个实例来编码 URI (对于由两个“代理”字符组成的字符而言,将仅是四个转义序列) 。

+

encodeURIComponent() 函数通过将一个,两个,三个或四个表示字符的 UTF-8 编码的转义序列替换某些字符的每个实例来编码 {{glossary("URI")}}(对于由两个“代理”字符组成的字符而言,将仅是四个转义序列)。

{{EmbedInteractiveExample("pages/js/globalprops-encodeuricomponent.html","shorter")}}

@@ -26,18 +26,18 @@ translation_of: Web/JavaScript/Reference/Global_Objects/encodeURIComponent

返回值

-

原字串作为URI组成部分被被编码后的新字符串。

+

原字串作为 URI 组成部分被被编码后的新字符串。

描述

-

encodeURIComponent 转义除了如下所示外的所有字符:

+

encodeURIComponent 转义除了如下所示外的所有字符:

不转义的字符:
     A-Z a-z 0-9 - _ . ! ~ * ' ( )
-

encodeURIComponent() 和 encodeURI 有以下几个不同点:

+

encodeURIComponent()encodeURI 有以下几个不同点:

-
var set1 = ";,/?:@&=+$";  // 保留字符
+
var set1 = ";,/?:@&=+$";  // 保留字符
 var set2 = "-_.!~*'()";   // 不转义字符
 var set3 = "#";           // 数字标志
 var set4 = "ABC abc 123"; // 字母数字字符和空格
@@ -65,13 +65,13 @@ alert(encodeURIComponent('\uDFFF')); 

为了避免服务器收到不可预知的请求,对任何用户输入的作为URI部分的内容你都需要用encodeURIComponent进行转义。比如,一个用户可能会输入"Thyme &time=again"作为comment变量的一部分。如果不使用encodeURIComponent对此内容进行转义,服务器得到的将是comment=Thyme%20&time=again。请注意,"&"符号和"="符号产生了一个新的键值对,所以服务器得到两个键值对(一个键值对是comment=Thyme,另一个则是time=again),而不是一个键值对。

-

对于 application/x-www-form-urlencoded (POST) 这种数据方式,空格需要被替换成 '+',所以通常使用 encodeURIComponent 的时候还会把 "%20" 替换为 "+"。

+

对于 application/x-www-form-urlencoded (POST) 这种数据方式,空格需要被替换成 '+',所以通常使用 encodeURIComponent 的时候还会把 "%20" 替换为 "+"。

-

为了更严格的遵循 RFC 3986(它保留 !, ', (, ), 和 *),即使这些字符并没有正式划定 URI 的用途,下面这种方式是比较安全的:

+

为了更严格的遵循 {{rfc("3986")}}(它保留 !, ', (, ), 和 *),即使这些字符并没有正式划定 URI 的用途,下面这种方式是比较安全的:

function fixedEncodeURIComponent (str) {
   return encodeURIComponent(str).replace(/[!'()*]/g, function(c) {
-    return '%' + c.charCodeAt(0).toString(16);
+    return '%' + c.charCodeAt(0).toString(16).toUpperCase();
   });
 }
@@ -88,54 +88,26 @@ console.log(header); function encodeRFC5987ValueChars (str) { -    return encodeURIComponent(str). + return encodeURIComponent(str). // 注意,尽管 RFC3986 保留 "!",但 RFC5987 并没有 // 所以我们并不需要过滤它 -        replace(/['()]/g, escape). // i.e., %27 %28 %29 -        replace(/\*/g, '%2A'). + replace(/['()]/g, escape). // i.e., %27 %28 %29 + replace(/\*/g, '%2A'). // 下面的并不是 RFC5987 中 URI 编码必须的 // 所以对于 |`^ 这3个字符我们可以稍稍提高一点可读性 -            replace(/%(?:7C|60|5E)/g, unescape); + replace(/%(?:7C|60|5E)/g, unescape); }

规范

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
规范状态备注
{{SpecName('ES3')}}{{Spec2('ES3')}}初始定义
{{SpecName('ES5.1', '#sec-15.1.3.4', 'encodeURIComponent')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-encodeuricomponent-uricomponent', 'encodeURIComponent')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-encodeuricomponent-uricomponent', 'encodeURIComponent')}}{{Spec2('ESDraft')}}
+
{{Specifications}}

浏览器兼容性

-

{{Compat("javascript.builtins.encodeURIComponent")}}

+

{{Compat}}

-

相关链接

+

参见