From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/textencoder/encode/index.html | 74 ++++++ .../zh-cn/web/api/textencoder/encoding/index.html | 49 ++++ files/zh-cn/web/api/textencoder/index.html | 150 +++++++++++ .../web/api/textencoder/textencoder/index.html | 294 +++++++++++++++++++++ 4 files changed, 567 insertions(+) create mode 100644 files/zh-cn/web/api/textencoder/encode/index.html create mode 100644 files/zh-cn/web/api/textencoder/encoding/index.html create mode 100644 files/zh-cn/web/api/textencoder/index.html create mode 100644 files/zh-cn/web/api/textencoder/textencoder/index.html (limited to 'files/zh-cn/web/api/textencoder') diff --git a/files/zh-cn/web/api/textencoder/encode/index.html b/files/zh-cn/web/api/textencoder/encode/index.html new file mode 100644 index 0000000000..96c91994bd --- /dev/null +++ b/files/zh-cn/web/api/textencoder/encode/index.html @@ -0,0 +1,74 @@ +--- +title: TextEncoder.prototype.encode() +slug: Web/API/TextEncoder/encode +tags: + - 文本编码 + - 编码 +translation_of: Web/API/TextEncoder/encode +--- +
{{APIRef("Encoding API")}}
+ +

TextEncoder.prototype.encode() 方法接受一个 {{domxref("USVString")}} 作为参数,返回一个以给定的文本(字符串)参数,通过 TextEncoder 中指定的方法(默认 UTF-8)编码后的 {{jsxref("Global_Objects/Uint8Array", "Uint8Array")}} 类型的值。

+ +

语法

+ +
b1 = encoder.encode(string);
+
+ +

参数

+ +
+
string
+
一个包含了将要编码的文本的 {{DOMxRef("USVString")}}。
+
+ +

返回值

+ +

一个 {{domxref("Uint8Array")}} 对象。

+ +

示例

+ +
<p class="source">This is a sample paragraph.</p>
+<p class="result">Encoded result: </p>
+ +
const sourcePara = document.querySelector('.source');
+const resultPara = document.querySelector('.result');
+const string = sourcePara.textContent;
+
+const textEncoder = new TextEncoder();
+
+let encoded = textEncoder.encode(string);
+resultPara.textContent += encoded;
+ +

{{EmbedLiveSample('Examples')}}

+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("Encoding", "#dom-textencoder-encode", "TextEncoder.prototype.encode()")}}{{Spec2("Encoding")}}Initial definition.
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.TextEncoder.encode")}}

+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/textencoder/encoding/index.html b/files/zh-cn/web/api/textencoder/encoding/index.html new file mode 100644 index 0000000000..602a16c5ff --- /dev/null +++ b/files/zh-cn/web/api/textencoder/encoding/index.html @@ -0,0 +1,49 @@ +--- +title: TextEncoder.encoding +slug: Web/API/TextEncoder/encoding +translation_of: Web/API/TextEncoder/encoding +--- +

{{APIRef("DOM")}}

+ +

{{ SeeCompatTable }}

+ +

TextEncoder.encoding 只读属性返回一个{{domxref("DOMString")}} ,其中包含特定编码器使用的编码算法的名称。

+ +

现在只会返回“utf-8”。

+ +

语法

+ +
b = encoder.encoding;
+ +

标准

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Encoding', '#dom-textencoder-encoding', 'TextEncoder.encoding')}}{{Spec2('Encoding')}}Initial definition.
+ +

浏览器支持

+ +
+
+ + +

{{Compat("api.TextEncoder.encoding")}}

+
+
+ +

另见

+ + diff --git a/files/zh-cn/web/api/textencoder/index.html b/files/zh-cn/web/api/textencoder/index.html new file mode 100644 index 0000000000..223674f74b --- /dev/null +++ b/files/zh-cn/web/api/textencoder/index.html @@ -0,0 +1,150 @@ +--- +title: TextEncoder +slug: Web/API/TextEncoder +tags: + - API + - 参考 + - 接口 + - 编码 +translation_of: Web/API/TextEncoder +--- +
{{APIRef("Encoding API")}}
+ +

TextEncoder 接受代码点流作为输入,并提供 UTF-8 字节流作为输出。

+ +
+

Note: There is a polyfill implementation to support non-UTF-8 text encodings on GitHub.

+
+ +

例子

+ +
const encoder = new TextEncoder()
+const view = encoder.encode('€')
+console.log(view); // Uint8Array(3) [226, 130, 172]
+
+ +

构造器

+ +
+
{{domxref("TextEncoder.TextEncoder", "TextEncoder()")}}
+
返回一个新构造的 TextEncoder,它默认使用 UTF-8 编码将代码点流转换成字节流。
+
+ +

属性

+ +

TextEncoder 接口不继承任何属性。

+ +
+
{{domxref("TextEncoder.encoding")}} {{readonlyInline}}
+
总是返回 "utf-8"。
+
+ +

方法

+ +

TextEncoder 接口不继承任何方法。

+ +
+
{{domxref("TextEncoder.encode()")}}
+
接受一个 {{domxref("USVString")}} 作为输入,返回一个包含文本的 {{jsxref("Uint8Array")}},其中的文本使用 UTF-8 编码。
+
{{DOMxRef("TextEncoder.prototype.encodeInto()")}}
+
接受一个 {{domxref("USVString")}} 作为输入、一个{{jsxref("Uint8Array")}} 作为输出目标,返回一个指示编码进度的目录(dictionary)对象。此方法的性能可能回比更早出现的 encode() 方法好一些。
+
+ +

Polyfill

+ +

The below polyfill is compliant with the standard and therefore only supports UTF-8. It is designed to work in IE5 "out of the box". However, in IE5-IE9, it will return a regular Array instead of a TypedArray. In those cases a polyfill might be impractical for large strings. Finally, note that you should run the below code through a minifier (especially closure compiler) to turn sequences like 0x1e << 3 into 0xf0. These sequences are not already precomputed because they serve to aesthetically illustrate how the polyfill works.

+ +
if (typeof TextEncoder === "undefined") {
+    TextEncoder=function TextEncoder(){};
+    TextEncoder.prototype.encode = function encode(str) {
+        "use strict";
+        var Len = str.length, resPos = -1;
+        // The Uint8Array's length must be at least 3x the length of the string because an invalid UTF-16
+        //  takes up the equivelent space of 3 UTF-8 characters to encode it properly. However, Array's
+        //  have an auto expanding length and 1.5x should be just the right balance for most uses.
+        var resArr = typeof Uint8Array === "undefined" ? new Array(Len * 1.5) : new Uint8Array(Len * 3);
+        for (var point=0, nextcode=0, i = 0; i !== Len; ) {
+            point = str.charCodeAt(i), i += 1;
+            if (point >= 0xD800 && point <= 0xDBFF) {
+                if (i === Len) {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; break;
+                }
+                // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
+                nextcode = str.charCodeAt(i);
+                if (nextcode >= 0xDC00 && nextcode <= 0xDFFF) {
+                    point = (point - 0xD800) * 0x400 + nextcode - 0xDC00 + 0x10000;
+                    i += 1;
+                    if (point > 0xffff) {
+                        resArr[resPos += 1] = (0x1e/*0b11110*/<<3) | (point>>>18);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>12)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | ((point>>>6)&0x3f/*0b00111111*/);
+                        resArr[resPos += 1] = (0x2/*0b10*/<<6) | (point&0x3f/*0b00111111*/);
+                        continue;
+                    }
+                } else {
+                    resArr[resPos += 1] = 0xef/*0b11101111*/; resArr[resPos += 1] = 0xbf/*0b10111111*/;
+                    resArr[resPos += 1] = 0xbd/*0b10111101*/; continue;
+                }
+            }
+            if (point <= 0x007f) {
+                resArr[resPos += 1] = (0x0/*0b0*/<<7) | point;
+            } else if (point <= 0x07ff) {
+                resArr[resPos += 1] = (0x6/*0b110*/<<5) | (point>>>6);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)  | (point&0x3f/*0b00111111*/);
+            } else {
+                resArr[resPos += 1] = (0xe/*0b1110*/<<4) | (point>>>12);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | ((point>>>6)&0x3f/*0b00111111*/);
+                resArr[resPos += 1] = (0x2/*0b10*/<<6)    | (point&0x3f/*0b00111111*/);
+            }
+        }
+        if (typeof Uint8Array !== "undefined") return resArr.subarray(0, resPos + 1);
+        // else // IE 6-9
+        resArr.length = resPos + 1; // trim off extra weight
+        return resArr;
+    };
+    TextEncoder.prototype.toString = function(){return "[object TextEncoder]"};
+    try { // Object.defineProperty only works on DOM prototypes in IE8
+        Object.defineProperty(TextEncoder.prototype,"encoding",{
+            get:function(){if(TextEncoder.prototype.isPrototypeOf(this)) return"utf-8";
+                           else throw TypeError("Illegal invocation");}
+        });
+    } catch(e) { /*IE6-8 fallback*/ TextEncoder.prototype.encoding = "utf-8"; }
+    if(typeof Symbol!=="undefined")TextEncoder.prototype[Symbol.toStringTag]="TextEncoder";
+}
+
+ +

Source: https://github.com/anonyco/FastestSmallestTextEncoderDecoder

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName('Encoding', '#interface-textencoder', 'TextEncoder')}}{{Spec2('Encoding')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.TextEncoder")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/textencoder/textencoder/index.html b/files/zh-cn/web/api/textencoder/textencoder/index.html new file mode 100644 index 0000000000..ce65b791c4 --- /dev/null +++ b/files/zh-cn/web/api/textencoder/textencoder/index.html @@ -0,0 +1,294 @@ +--- +title: TextEncoder() +slug: Web/API/TextEncoder/TextEncoder +tags: + - TextEncoder + - 接口 + - 编码 + - 编码器 +translation_of: Web/API/TextEncoder/TextEncoder +--- +

{{apiref("TextEncoder")}}{{seeCompatTable}}

+ +

The TextEncoder() 构造函数返回一个新创建的{{domxref("TextEncoder")}}对象。

+ +

语法

+ +
encoder = new TextEncoder();
+
+ +

参数

+ +
+
+
    +
  • TextEncoder() 从 Firefox 48 and Chrome 53 开始不再需要参数
  • +
+ +

Note: 在Firefox 48和Chrome 53之前,编码类型标签被接受为TextEncoder对象的参数,现在这两个浏览器已经删除了除utf-8之外的任何编码器类型的支持,以符合规范。 传入TextEncoder构造函数的任何类型标签现在都将被忽略,并且将创建一个utf-8 TextEncoder。

+
+
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Possible values of utfLabelEncoding
"unicode-1-1-utf-8", "utf-8", "utf8"'utf-8'
"866", "cp866", "csibm866", "ibm866"{{interwiki('wikipedia', 'Code_page_866', "'ibm866'")}}
"csisolatin2", "iso-8859-2", "iso-ir-101", "iso8859-2", "iso88592", "iso_8859-2", "iso_8859-2:1987", "l2", "latin2"{{interwiki('wikipedia', 'ISO/IEC_8859-2', "'iso-8859-2'")}}
"csisolatin3", "iso-8859-3", "iso-ir-109", "iso8859-3", "iso88593", "iso_8859-3", "iso_8859-3:1988", "l3", "latin3"{{interwiki('wikipedia', 'ISO/IEC_8859-3', "'iso-8859-3'")}}
"csisolatin4", "iso-8859-4", "iso-ir-110", "iso8859-4", "iso88594", "iso_8859-4", "iso_8859-4:1988", "l4", "latin4"{{interwiki('wikipedia', 'ISO/IEC_8859-4', "'iso-8859-4'")}}
"csisolatincyrillic", "cyrillic", "iso-8859-5", "iso-ir-144", "iso88595", "iso_8859-5", "iso_8859-5:1988"{{interwiki('wikipedia', 'ISO/IEC_8859-5', "'iso-8859-5'")}}
"arabic", "asmo-708", "csiso88596e", "csiso88596i", "csisolatinarabic", "ecma-114", "iso-8859-6", "iso-8859-6-e", "iso-8859-6-i", "iso-ir-127", "iso8859-6", "iso88596", "iso_8859-6", "iso_8859-6:1987"{{interwiki('wikipedia', 'ISO/IEC_8859-6', "'iso-8859-6'")}}
"csisolatingreek", "ecma-118", "elot_928", "greek", "greek8", "iso-8859-7", "iso-ir-126", "iso8859-7", "iso88597", "iso_8859-7", "iso_8859-7:1987", "sun_eu_greek"{{interwiki('wikipedia', 'ISO/IEC_8859-7', "'iso-8859-7'")}}
"csiso88598e", "csisolatinhebrew", "hebrew", "iso-8859-8", "iso-8859-8-e", "iso-ir-138", "iso8859-8", "iso88598", "iso_8859-8", "iso_8859-8:1988", "visual"{{interwiki('wikipedia', 'ISO/IEC_8859-8', "'iso-8859-8'")}}
"csiso88598i", "iso-8859-8-i", "logical"{{interwiki('wikipedia', 'ISO-8859-8-I', "'iso-8859-8i'")}}
"csisolatin6", "iso-8859-10", "iso-ir-157", "iso8859-10", "iso885910", "l6", "latin6"{{interwiki('wikipedia', 'ISO/IEC_8859-10', "'iso-8859-10'")}}
"iso-8859-13", "iso8859-13", "iso885913"{{interwiki('wikipedia', 'ISO/IEC_8859-13', "'iso-8859-13'")}}
"iso-8859-14", "iso8859-14", "iso885914"{{interwiki('wikipedia', 'ISO/IEC_8859-14', "'iso-8859-14'")}}
"csisolatin9", "iso-8859-15", "iso8859-15", "iso885915", "l9", "latin9"{{interwiki('wikipedia', 'ISO/IEC_8859-15', "'iso-8859-15'")}}
"iso-8859-16"{{interwiki('wikipedia', 'ISO/IEC_8859-16', "'iso-8859-16'")}}
"cskoi8r", "koi", "koi8", "koi8-r", "koi8_r"{{interwiki('wikipedia', 'KOI8-R', "'koi8-r'")}}
"koi8-u"{{interwiki('wikipedia', 'KOI8-U', "'koi8-u'")}}
"csmacintosh", "mac", "macintosh", "x-mac-roman"{{interwiki('wikipedia', 'Mac OS Roman', "'macintosh'")}}
"dos-874", "iso-8859-11", "iso8859-11", "iso885911", "tis-620", "windows-874"{{interwiki('wikipedia', 'Windows-874', "'windows-874'")}}
"cp1250", "windows-1250", "x-cp1250"{{interwiki('wikipedia', 'Windows-1250', "'windows-1250'")}}
"cp1251", "windows-1251", "x-cp1251"{{interwiki('wikipedia', 'Windows-1251', "'windows-1251'")}}
"ansi_x3.4-1968", "ascii", "cp1252", "cp819", "csisolatin1", "ibm819", "iso-8859-1", "iso-ir-100", "iso8859-1", "iso88591", "iso_8859-1", "iso_8859-1:1987", "l1", "latin1", "us-ascii", "windows-1252", "x-cp1252"{{interwiki('wikipedia', 'Windows-1252', "'windows-1252'")}}
"cp1253", "windows-1253", "x-cp1253"{{interwiki('wikipedia', 'Windows-1253', "'windows-1253'")}}
"cp1254", "csisolatin5", "iso-8859-9", "iso-ir-148", "iso8859-9", "iso88599", "iso_8859-9", "iso_8859-9:1989", "l5", "latin5", "windows-1254", "x-cp1254"{{interwiki('wikipedia', 'Windows-1254', "'windows-1254'")}}
"cp1255", "windows-1255", "x-cp1255"{{interwiki('wikipedia', 'Windows-1255', "'windows-1255'")}}
"cp1256", "windows-1256", "x-cp1256"{{interwiki('wikipedia', 'Windows-1256', "'windows-1256'")}}
"cp1257", "windows-1257", "x-cp1257"{{interwiki('wikipedia', 'Windows-1257', "'windows-1257'")}}
"cp1258", "windows-1258", "x-cp1258"{{interwiki('wikipedia', 'Windows-1258', "'windows-1258'")}}
"x-mac-cyrillic", "x-mac-ukrainian"{{interwiki('wikipedia', 'Macintosh Cyrillic encoding', "'x-mac-cyrillic'")}}
"chinese", "csgb2312", "csiso58gb231280", "gb2312", "gb_2312", "gb_2312-80", "gbk", "iso-ir-58", "x-gbk"{{interwiki('wikipedia', 'GBK', "'gbk'")}}
"gb18030"{{interwiki('wikipedia', 'GB_18030', "'gb18030'")}}
"hz-gb-2312"{{interwiki('wikipedia', 'HZ_(character_encoding)', "'hz-gb-2312'")}}
"big5", "big5-hkscs", "cn-big5", "csbig5", "x-x-big5"{{interwiki('wikipedia', 'Big5', "'big5'")}}
"cseucpkdfmtjapanese", "euc-jp", "x-euc-jp"{{interwiki('wikipedia', 'Extended_Unix_Code#EUC-JP', "'euc-jp'")}}
"csiso2022jp", "iso-2022-jp"{{interwiki('wikipedia', 'ISO/IEC_2022#ISO-2022-JP', "'iso-2022-jp'")}}
"csshiftjis", "ms_kanji", "shift-jis", "shift_jis", "sjis", "windows-31j", "x-sjis"{{interwiki('wikipedia', 'Shift JIS', "'shift-jis'")}}
"cseuckr", "csksc56011987", "euc-kr", "iso-ir-149", "korean", "ks_c_5601-1987", "ks_c_5601-1989", "ksc5601", "ksc_5601", "windows-949"{{interwiki('wikipedia', 'Extended_Unix_Code#EUC-KR', "'euc-kr'")}}
"csiso2022kr", "iso-2022-kr"{{interwiki('wikipedia', 'ISO/IEC_2022#ISO-2022-KR', "'iso-2022-kr'")}}
"utf-16be"{{interwiki('wikipedia', 'UTF-16#Byte_order_encoding_schemes', "'utf-16be'")}}
"utf-16", "utf-16le"{{interwiki('wikipedia', 'UTF-16#Byte_order_encoding_schemes', "'utf-16le'")}}
"x-user-defined"'x-user-defined'
"iso-2022-cn", "iso-2022-cn-ext"'replacement'
+
+
+ +

例子

+ +
var textEncoder = new TextEncoder("iso-8859-1");
+ +

标注

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Encoding', '#dom-textencoder', 'TextEncode()')}}{{Spec2('Encoding')}}Initial definition.
+ +

浏览器支持

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatNo}}{{CompatGeckoDesktop("19.0")}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE PhoneOpera MobileSafari Mobile
Basic support{{CompatNo}}{{CompatGeckoMobile("19.0")}}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +

另见

+ + -- cgit v1.2.3-54-g00ecf