Nuestros voluntarios aún no han traducido este artículo al Español. ¡Únete a nosotros y ayúdanos a traducirlo!
También puedes leer el artículo en English (US).

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

TextEncoder takes a stream of code points as input and emits a stream of bytes. For a more scalable, non-native library, see StringView – a C-like representation of strings based on typed arrays.

Note: Firefox, Chrome and Opera used to have support for encoding types other than utf-8 (such as utf-16iso-8859-2, koi8, cp1261, and gbk). As of Firefox 48 (bug 1257877), Chrome 54 (ticket) and Opera 41, no other encoding types are available other than utf-8, in order to match the spec. In all cases, passing in an encoding type to the constructor will be ignored and a utf-8 TextEncoder will be created (the TextDecoder still allows for other decoding types).

Note: There is a polyfill implementation to support all the legacy encodings in GitHub.

Constructor

TextEncoder()
Returns a newly constructed TextEncoder that will generate a byte stream with utf-8 encoding.

Properties

The TextEncoder interface doesn't inherit any property.

TextEncoder.encodingRead only
Is a DOMString containing the name of the encoder, that is a string describing the method the TextEncoder will use.

Methods

The TextEncoder interface doesn't inherit any method.

TextEncoder.encode()
Returns a Uint8Array containing utf-8 encoded text.

Polyfill

The below polyfill will only furfill the specs demanded by the W3 (no character encodings other than UTF-8 are supported, unfortunately ☹️). 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 such circumstances as these with such memory inefficient slow browsers, this polyfill (or any polyfill for that matter) would be impractical for large strings in such old browsers. 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";
}

Specifications

Specification Status Comment
Encoding
The definition of 'TextEncoder' in that specification.
Living Standard Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS Full support 10.1Samsung Internet Android ?
Available in Web Workers
Experimental
Chrome Full support 38Edge ? Firefox Full support 20IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 20Opera Android ? Safari iOS Full support 10.1Samsung Internet Android ?
TextEncoder() constructor
Experimental
Chrome Full support 53
Notes
Full support 53
Notes
Notes Does not accept parameters. Supports only utf-8 encoding.
No support 38 — 53
Notes
Notes Throws RangeError exception for unknown encoding types.
Edge ? Firefox Full support 48
Notes
Full support 48
Notes
Notes The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
No support 38 — 48
Notes
Notes If the encoding type label argument is invalid, then a RangeError exception is thrown.
No support 19 — 38
Notes
Notes If the encoding type label argument is invalid, then a TypeError exception is thrown.
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 48
Notes
Full support 48
Notes
Notes The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.
No support 38 — 48
Notes
Notes If the encoding type label argument is invalid, then a RangeError exception is thrown.
No support 19 — 38
Notes
Notes If the encoding type label argument is invalid, then a TypeError exception is thrown.
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS Full support 10.1Samsung Internet Android ?
encoding
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS Full support 10.1Samsung Internet Android ?
encode
Experimental
Chrome Full support 38Edge ? Firefox Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
IE No support NoOpera Full support 25Safari Full support 10.1WebView Android Full support 38Chrome Android Full support 38Edge Mobile ? Firefox Android Full support 19
Full support 19
Full support 18
Notes
Notes Firefox 18 implemented an earlier and slightly different version of the specification.
Opera Android ? Safari iOS Full support 10.1Samsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

See also

Etiquetas y colaboradores del documento

Última actualización por: Peppesterest,