Nos bénévoles n'ont pas encore traduit cet article en Français. Inscrivez-vous et aidez-nous à réaliser cette tâche !
Vous pouvez également lire cet article 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 (ticket), 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.encoding Read 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 supported, unfourtunately ☹️). It is designed to work in IE5 provided that you also have a TypedArray polyfill (not included here). Although, it will work in IE10+ without the TypedArray polyfill.

if (typeof TextEncoder === "undefined") {
    TextEncoder=function TextEncoder(){};
    TextEncoder.prototype.encode = function encode(str) {
        "use strict";
        var Len = str.length, resPos = -1, resArr = 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*/);
            }
        }
        resArr = new Uint8Array(resArr.buffer.slice(0, resPos+1));
        return resArr;
    };
    TextEncoder.prototype.toString = function(){return "[object TextEncoder]"};
    if (Object.defineProperty) {
      Object.defineProperty(TextEncoder.prototype,"encoding",{get:function(){if(Object.getPrototypeOf
      (this)!==TextEncoder.prototype)throw TypeError("Illegal invocation");else return"utf-8"}});
    } else {
      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

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Basic support38 ?

19

181

No25 No
Available in Web Workers38 ?20 No25 No
TextEncoder() constructor

532

38 — 533

?

484

38 — 485

19 — 386

181

No25 No
encoding38 ?

19

181

No25 No
encode38 ?

19

181

No25 No
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Basic support3838 ?

19

181

? No ?
Available in Web Workers3838 ?20 ? No ?
TextEncoder() constructor3838 ?

484

38 — 485

19 — 386

181

? No ?
encoding3838 ?

19

181

? No ?
encode3838 ?

19

181

? No ?

1. Firefox 18 implemented an earlier and slightly different version of the specification.

2. Does not accept parameters. Supports only utf-8 encoding.

3. Throws RangeError exception for unknown encoding types.

4. The constructor accepts an encoding type label argument, but the value is ignored. Only utf-8 encoding is supported.

5. If the encoding type label argument is invalid, then a RangeError exception is thrown.

6. If the encoding type label argument is invalid, then a TypeError exception is thrown.

See also

Étiquettes et contributeurs liés au document

Dernière mise à jour par : anonyco,