From eacfdad6f08212753754a7759a97fe6eb27ad8a9 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Fri, 22 Apr 2022 23:53:52 +0200 Subject: [PATCH] Static methods for TextEncoder and TextDecoder --- encoding.bs | 166 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 128 insertions(+), 38 deletions(-) diff --git a/encoding.bs b/encoding.bs index a91e225..a4d8a02 100644 --- a/encoding.bs +++ b/encoding.bs @@ -1333,6 +1333,10 @@ dictionary TextDecoderOptions { boolean ignoreBOM = false; }; +dictionary TextDecoderOptionsWithLabel : TextDecoderOptions { + DOMString label = "utf-8"; +}; + dictionary TextDecodeOptions { boolean stream = false; }; @@ -1341,6 +1345,8 @@ dictionary TextDecodeOptions { interface TextDecoder { constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options = {}); + static USVString decode(optional [AllowShared] BufferSource input, optional TextDecoderOptionsWithLabel options = {}); + USVString decode(optional [AllowShared] BufferSource input, optional TextDecodeOptions options = {}); }; TextDecoder includes TextDecoderCommon; @@ -1351,13 +1357,17 @@ TextDecoder includes TextDecoderCommon; initially false.
+
{{TextDecoder}} . decode([input [, options]]) +
+

Returns the result of running encoding's decoder. +

If options["{{TextDecoderOptionsWithLabel/label}}"] is either not a label or + is a label for replacement, throws a {{RangeError}}. +

decoder = new TextDecoder([label = "utf-8" [, options]])

Returns a new {{TextDecoder}} object. -

If label is either not a label or is a - label for replacement, - throws a - {{RangeError}}. +

If label is either not a label or is a label for replacement, + throws a {{RangeError}}.

decoder . encoding

Returns encoding's name, lowercased. @@ -1389,40 +1399,41 @@ string += decoder.decode(); // end-of-queue throws a {{TypeError}}.

-

The -new TextDecoder(label, options) -constructor steps are: +

To initialize a {{TextDecoder}} decoder, +DOMString label, an optional fatal flag, and an optional +ignoreBOM flag, run these steps:

  1. Let encoding be the result of getting an encoding from label.

  2. If encoding is failure or replacement, then throw a {{RangeError}}. -

  3. Set this's encoding to encoding. +

  4. Set decoder's encoding to encoding. -

  5. If options["{{TextDecoderOptions/fatal}}"] is true, then set this's +

  6. If fatal is set, then set decoder's error mode to "fatal". -

  7. Set this's ignore BOM to - options["{{TextDecoderOptions/ignoreBOM}}"]. +

  8. Set decoder's ignore BOM to + true if ignoreBOM if is set, false otherwise.

-

The decode(input, options) -method steps are: +

To decode an input, given a {{TextDecoder}} decoderObject, a +BufferSource input, and an optional stream flag, run these steps:

    -
  1. If this's do not flush is false, then set this's - decoder to a new instance of this's - encoding's decoder, this's - I/O queue to the I/O queue of bytes - « end-of-queue », and this's BOM seen to false. +

  2. If decoderObject's do not flush is false, then set + decoderObject's decoder to a new instance of + decoderObject's encoding's decoder, + decoderObject's I/O queue to the I/O queue of + bytes « end-of-queue », and decoderObject's BOM seen + to false. -

  3. Set this's do not flush to - options["{{TextDecodeOptions/stream}}"]. +

  4. Set decoderObject's do not flush to true if + stream if is set, false otherwise.

  5. If input is given, then push a - copy of input to this's + copy of input to decoder's I/O queue.

    Implementations are strongly encouraged to use an implementation strategy that @@ -1441,36 +1452,72 @@ method steps are:

    While true:

      -
    1. Let item be the result of reading from this's +

    2. Let item be the result of reading from decoderObject's I/O queue.

    3. -

      If item is end-of-queue and this's +

      If item is end-of-queue and decoderObject's do not flush is true, then return the result of running - serialize I/O queue with this and output. + serialize I/O queue with decoderObject and output.

      The way streaming works is to not handle end-of-queue here when - this's do not flush is true and to not set it to false. That way - in a subsequent invocation this's decoder is not set anew in - the first step of the algorithm and its state is preserved. + decoderObject's do not flush is true and to not set it to + false. That way in a subsequent invocation decoderObject's + decoder is not set anew in the first step of the algorithm and its + state is preserved.

    4. Otherwise:

      1. Let result be the result of processing an item with item, - this's decoder, this's - I/O queue, output, and this's + decoderObject's decoder, decoderObject's + I/O queue, output, and decoderObject's error mode.

      2. If result is finished, then return the result of running - serialize I/O queue with this and output. + serialize I/O queue with decoderObject and output.

      3. Otherwise, if result is error, throw a {{TypeError}}.

+

The +new TextDecoder(label, options) +constructor steps are: + +

    +
  1. Run the initialize a TextDecoder steps on this with label, the + fatal flag set if options["{{TextDecoderOptions/fatal}}"] is true, and the + ignoreBOM flag set if options["{{TextDecoderOptions/ignoreBOM}}"] is true. +

+ +

The static +decode(input, options) method +steps are: + +

    +
  1. Let decoderObject be a new TextDecoder object. + +

  2. Run the initialize a TextDecoder steps on decoderObject with + options["{{TextDecoderOptionsWithLabel/label}}"], the fatal flag set if + options["{{TextDecoderOptions/fatal}}"] is true, and the ignoreBOM flag set + if options["{{TextDecoderOptions/ignoreBOM}}"] is true. + +

  3. Return the result of running the decode an input steps with decoderObject, + input. +

+ +

The decode(input, options) +method steps are: + +

    +
  1. Return the result of running the decode an input steps with this, + input, and the stream flag set if + options["{{TextDecoderOptions/stream}}"] is true. +

+

Interface mixin {{TextEncoderCommon}}

@@ -1498,6 +1545,9 @@ dictionary TextEncoderEncodeIntoResult {
 interface TextEncoder {
   constructor();
 
+  [NewObject] static Uint8Array encode(optional USVString input = "");
+  static TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
+
   [NewObject] Uint8Array encode(optional USVString input = "");
   TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
 };
@@ -1511,6 +1561,16 @@ requires buffering of scalar values.
 
+
{{TextEncoder}} . encode([input = ""]) +

Returns the result of running UTF-8's encoder on input. + +

{{TextEncoder}} . encodeInto(source, destination) +

Runs the UTF-8 encoder on source, stores the result of that operation into + destination, and returns the progress made as an object wherein + {{TextEncoderEncodeIntoResult/read}} is the number of converted code units of + source and {{TextEncoderEncodeIntoResult/written}} is the number of bytes modified in + destination. +

encoder = new TextEncoder()

Returns a new {{TextEncoder}} object. @@ -1528,11 +1588,7 @@ requires buffering of scalar values. destination.

-

The -new TextEncoder() -constructor steps are to do nothing. - -

The encode(input) method steps are: +

To encode an input given a USVString input, run the following steps:

  1. Convert input to an I/O queue of scalar @@ -1563,9 +1619,8 @@ constructor steps are to do nothing.

-

The -encodeInto(source, destination) -method steps are: +

To encode a source into a destination given a USVString source, and a +BufferSource destination, run the following steps:

  1. Let read be 0. @@ -1627,6 +1682,41 @@ method steps are: "{{TextEncoderEncodeIntoResult/written}}" → written ]».

+

The +new TextEncoder() +constructor steps are to do nothing. + +

The static encode(input) method steps +are: + +

    +
  1. Return the result of running the encode an input steps with input. +

+ +

The encode(input) method steps are: + +

    +
  1. Return the result of running the encode an input steps with input. +

+ +

The static +encodeInto(source, destination) +method steps are: + +

    +
  1. Return the result of running the encode a source into a destination steps with + source and destination. +

+ +

The +encodeInto(source, destination) +method steps are: + +

    +
  1. Return the result of running the encode a source into a destination steps with + source and destination. +

+

The encodeInto() method can be used to encode a string into an existing {{ArrayBuffer}} object. Various details below are left