Skip to content

Commit eacfdad

Browse files
committed
Static methods for TextEncoder and TextDecoder
1 parent f09648a commit eacfdad

File tree

1 file changed

+128
-38
lines changed

1 file changed

+128
-38
lines changed

encoding.bs

+128-38
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,10 @@ dictionary TextDecoderOptions {
13331333
boolean ignoreBOM = false;
13341334
};
13351335

1336+
dictionary TextDecoderOptionsWithLabel : TextDecoderOptions {
1337+
DOMString label = "utf-8";
1338+
};
1339+
13361340
dictionary TextDecodeOptions {
13371341
boolean stream = false;
13381342
};
@@ -1341,6 +1345,8 @@ dictionary TextDecodeOptions {
13411345
interface TextDecoder {
13421346
constructor(optional DOMString label = "utf-8", optional TextDecoderOptions options = {});
13431347

1348+
static USVString decode(optional [AllowShared] BufferSource input, optional TextDecoderOptionsWithLabel options = {});
1349+
13441350
USVString decode(optional [AllowShared] BufferSource input, optional TextDecodeOptions options = {});
13451351
};
13461352
TextDecoder includes TextDecoderCommon;
@@ -1351,13 +1357,17 @@ TextDecoder includes TextDecoderCommon;
13511357
initially false.
13521358

13531359
<dl class=domintro>
1360+
<dt>{{TextDecoder}} . <a method for=TextDecoder lt=decode()>decode([<var>input</var> [, <var>options</var>]])</a></code>
1361+
<dd>
1362+
<p>Returns the result of running <a for=TextDecoderCommon>encoding</a>'s <a for=/>decoder</a>.
1363+
<p>If <var>options</var>["{{TextDecoderOptionsWithLabel/label}}"] is either not a <a>label</a> or
1364+
is a <a>label</a> for <a>replacement</a>, <a>throws</a> a {{RangeError}}.
1365+
13541366
<dt><code><var>decoder</var> = new <a constructor for=TextDecoder lt=TextDecoder()>TextDecoder([<var>label</var> = "utf-8" [, <var>options</var>]])</a></code>
13551367
<dd>
13561368
<p>Returns a new {{TextDecoder}} object.
1357-
<p>If <var>label</var> is either not a <a>label</a> or is a
1358-
<a>label</a> for <a>replacement</a>,
1359-
<a>throws</a> a
1360-
{{RangeError}}.
1369+
<p>If <var>label</var> is either not a <a>label</a> or is a <a>label</a> for <a>replacement</a>,
1370+
<a>throws</a> a {{RangeError}}.
13611371

13621372
<dt><code><var>decoder</var> . <a attribute for=TextDecoderCommon>encoding</a></code>
13631373
<dd><p>Returns <a for=TextDecoderCommon>encoding</a>'s <a>name</a>, lowercased.
@@ -1389,40 +1399,41 @@ string += decoder.decode(); // end-of-queue</code></pre>
13891399
<a>throws</a> a {{TypeError}}.
13901400
</dl>
13911401

1392-
<p>The
1393-
<dfn constructor for=TextDecoder lt="TextDecoder(label, options)" id=dom-textdecoder><code>new TextDecoder(<var>label</var>, <var>options</var>)</code></dfn>
1394-
constructor steps are:
1402+
<p>To <dfn>initialize</dfn> a {{TextDecoder}} <var>decoder</var>,
1403+
<a>DOMString</a> <var>label</var>, an optional <var>fatal</var> flag, and an optional
1404+
<var>ignoreBOM</var> flag, run these steps:
13951405

13961406
<ol>
13971407
<li><p>Let <var>encoding</var> be the result of <a>getting an encoding</a> from <var>label</var>.
13981408

13991409
<li><p>If <var>encoding</var> is failure or <a>replacement</a>, then <a>throw</a> a {{RangeError}}.
14001410

1401-
<li><p>Set <a>this</a>'s <a for=TextDecoderCommon>encoding</a> to <var>encoding</var>.
1411+
<li><p>Set <a>decoder</a>'s <a for=TextDecoderCommon>encoding</a> to <var>encoding</var>.
14021412

1403-
<li><p>If <var>options</var>["{{TextDecoderOptions/fatal}}"] is true, then set <a>this</a>'s
1413+
<li><p>If <var>fatal</var> is set, then set <a>decoder</a>'s
14041414
<a for=TextDecoderCommon>error mode</a> to "<code>fatal</code>".
14051415

1406-
<li><p>Set <a>this</a>'s <a for=TextDecoderCommon>ignore BOM</a> to
1407-
<var>options</var>["{{TextDecoderOptions/ignoreBOM}}"].
1416+
<li><p>Set <a>decoder</a>'s <a for=TextDecoderCommon>ignore BOM</a> to
1417+
<code>true</code> if <var>ignoreBOM</var> if is set, <code>false</code> otherwise.
14081418
</ol>
14091419

1410-
<p>The <dfn method for=TextDecoder><code>decode(<var>input</var>, <var>options</var>)</code></dfn>
1411-
method steps are:
1420+
<p>To <dfn>decode an input</dfn>, given a {{TextDecoder}} <var>decoderObject</var>, a
1421+
<a>BufferSource</a> <var>input</var>, and an optional <var>stream</var> flag, run these steps:
14121422

14131423
<ol>
1414-
<li><p>If <a>this</a>'s <a for=TextDecoder>do not flush</a> is false, then set <a>this</a>'s
1415-
<a for=TextDecoderCommon>decoder</a> to a new instance of <a>this</a>'s
1416-
<a for=TextDecoderCommon>encoding</a>'s <a for=/>decoder</a>, <a>this</a>'s
1417-
<a for=TextDecoderCommon>I/O queue</a> to the <a for=/>I/O queue</a> of bytes
1418-
« <a>end-of-queue</a> », and <a>this</a>'s <a for=TextDecoderCommon>BOM seen</a> to false.
1424+
<li><p>If <var>decoderObject</var>'s <a for=TextDecoder>do not flush</a> is false, then set
1425+
<var>decoderObject</var>'s <a for=TextDecoderCommon>decoder</a> to a new instance of
1426+
<var>decoderObject</var>'s <a for=TextDecoderCommon>encoding</a>'s <a for=/>decoder</a>,
1427+
<var>decoderObject</var>'s <a for=TextDecoderCommon>I/O queue</a> to the <a for=/>I/O queue</a> of
1428+
bytes « <a>end-of-queue</a> », and <var>decoderObject</var>'s <a for=TextDecoderCommon>BOM seen</a>
1429+
to false.
14191430

1420-
<li><p>Set <a>this</a>'s <a for=TextDecoder>do not flush</a> to
1421-
<var>options</var>["{{TextDecodeOptions/stream}}"].
1431+
<li><p>Set <var>decoderObject</var>'s <a for=TextDecoder>do not flush</a> to <code>true</code> if
1432+
<var>stream</var> if is set, <code>false</code> otherwise.
14221433

14231434
<li>
14241435
<p>If <var>input</var> is given, then <a>push</a> a
1425-
<a lt="get a copy of the buffer source">copy of</a> <var>input</var> to <a>this</a>'s
1436+
<a lt="get a copy of the buffer source">copy of</a> <var>input</var> to <a>decoder</a>'s
14261437
<a for=TextDecoderCommon>I/O queue</a>.
14271438

14281439
<p class=note>Implementations are strongly encouraged to use an implementation strategy that
@@ -1441,36 +1452,72 @@ method steps are:
14411452
<p>While true:
14421453

14431454
<ol>
1444-
<li><p>Let <var>item</var> be the result of <a>reading</a> from <a>this</a>'s
1455+
<li><p>Let <var>item</var> be the result of <a>reading</a> from <var>decoderObject</var>'s
14451456
<a for=TextDecoderCommon>I/O queue</a>.
14461457

14471458
<li>
1448-
<p>If <var>item</var> is <a>end-of-queue</a> and <a>this</a>'s
1459+
<p>If <var>item</var> is <a>end-of-queue</a> and <var>decoderObject</var>'s
14491460
<a for=TextDecoder>do not flush</a> is true, then return the result of running
1450-
<a>serialize I/O queue</a> with <a>this</a> and <var>output</var>.
1461+
<a>serialize I/O queue</a> with <var>decoderObject</var> and <var>output</var>.
14511462

14521463
<p class=note>The way streaming works is to not handle <a>end-of-queue</a> here when
1453-
<a>this</a>'s <a for=TextDecoder>do not flush</a> is true and to not set it to false. That way
1454-
in a subsequent invocation <a>this</a>'s <a for=TextDecoderCommon>decoder</a> is not set anew in
1455-
the first step of the algorithm and its state is preserved.
1464+
<var>decoderObject</var>'s <a for=TextDecoder>do not flush</a> is true and to not set it to
1465+
false. That way in a subsequent invocation <var>decoderObject</var>'s
1466+
<a for=TextDecoderCommon>decoder</a> is not set anew in the first step of the algorithm and its
1467+
state is preserved.
14561468

14571469
<li>
14581470
<p>Otherwise:
14591471

14601472
<ol>
14611473
<li><p>Let <var>result</var> be the result of <a>processing an item</a> with <var>item</var>,
1462-
<a>this</a>'s <a for=TextDecoderCommon>decoder</a>, <a>this</a>'s
1463-
<a for=TextDecoderCommon>I/O queue</a>, <var>output</var>, and <a>this</a>'s
1474+
<var>decoderObject</var>'s <a for=TextDecoderCommon>decoder</a>, <var>decoderObject</var>'s
1475+
<a for=TextDecoderCommon>I/O queue</a>, <var>output</var>, and <var>decoderObject</var>'s
14641476
<a for=TextDecoderCommon>error mode</a>.
14651477

14661478
<li><p>If <var>result</var> is <a>finished</a>, then return the result of running
1467-
<a>serialize I/O queue</a> with <a>this</a> and <var>output</var>.
1479+
<a>serialize I/O queue</a> with <var>decoderObject</var> and <var>output</var>.
14681480

14691481
<li><p>Otherwise, if <var>result</var> is <a>error</a>, <a>throw</a> a {{TypeError}}.
14701482
</ol>
14711483
</ol>
14721484
</ol>
14731485

1486+
<p>The
1487+
<dfn constructor for=TextDecoder lt="TextDecoder(label, options)" id=dom-textdecoder><code>new TextDecoder(<var>label</var>, <var>options</var>)</code></dfn>
1488+
constructor steps are:
1489+
1490+
<ol>
1491+
<li><p>Run the <a>initialize a TextDecoder</a> steps on <a>this</a> with <var>label</var>, the
1492+
fatal flag set if <var>options</var>["{{TextDecoderOptions/fatal}}"] is <code>true</code>, and the
1493+
ignoreBOM flag set if <var>options</var>["{{TextDecoderOptions/ignoreBOM}}"] is <code>true</code>.
1494+
</ol>
1495+
1496+
<p>The static
1497+
<dfn method for=TextDecoder><code>decode(<var>input</var>, <var>options</var>)</code></dfn> method
1498+
steps are:
1499+
1500+
<ol>
1501+
<li><p>Let <var>decoderObject</var> be a new <a>TextDecoder</a> object.
1502+
1503+
<li><p>Run the <a>initialize a TextDecoder</a> steps on <var>decoderObject</var> with
1504+
<var>options</var>["{{TextDecoderOptionsWithLabel/label}}"], the fatal flag set if
1505+
<var>options</var>["{{TextDecoderOptions/fatal}}"] is <code>true</code>, and the ignoreBOM flag set
1506+
if <var>options</var>["{{TextDecoderOptions/ignoreBOM}}"] is <code>true</code>.
1507+
1508+
<li><p>Return the result of running the <a>decode an input</a> steps with <a>decoderObject</a>,
1509+
<var>input</var>.
1510+
</ol>
1511+
1512+
<p>The <dfn method for=TextDecoder><code>decode(<var>input</var>, <var>options</var>)</code></dfn>
1513+
method steps are:
1514+
1515+
<ol>
1516+
<li><p>Return the result of running the <a>decode an input</a> steps with <a>this</a>,
1517+
<var>input</var>, and the <code>stream</code> flag set if
1518+
<var>options</var>["{{TextDecoderOptions/stream}}"] is <code>true</code>.
1519+
</ol>
1520+
14741521
<h3 id=interface-mixin-textencodercommon>Interface mixin {{TextEncoderCommon}}</h3>
14751522

14761523
<pre class=idl>
@@ -1498,6 +1545,9 @@ dictionary TextEncoderEncodeIntoResult {
14981545
interface TextEncoder {
14991546
constructor();
15001547

1548+
[NewObject] static Uint8Array encode(optional USVString input = "");
1549+
static TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
1550+
15011551
[NewObject] Uint8Array encode(optional USVString input = "");
15021552
TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination);
15031553
};
@@ -1511,6 +1561,16 @@ requires buffering of scalar values.
15111561
<hr>
15121562

15131563
<dl class=domintro>
1564+
<dt>{{TextEncoder}} . <a method for=TextEncoder lt=encode()>encode([<var>input</var> = ""])</a></code>
1565+
<dd><p>Returns the result of running <a>UTF-8</a>'s <a for=/>encoder</a> on input.
1566+
1567+
<dt>{{TextEncoder}} . <a method=for=TextEncoder lt="encodeInto(source, destination)">encodeInto(<var>source</var>, <var>destination</var>)</a></code>
1568+
<dd><p>Runs the <a>UTF-8 encoder</a> on <var>source</var>, stores the result of that operation into
1569+
<var>destination</var>, and returns the progress made as an object wherein
1570+
{{TextEncoderEncodeIntoResult/read}} is the number of converted <a>code units</a> of
1571+
<var>source</var> and {{TextEncoderEncodeIntoResult/written}} is the number of bytes modified in
1572+
<var>destination</var>.
1573+
15141574
<dt><code><var>encoder</var> = new <a constructor for=TextEncoder>TextEncoder()</a></code>
15151575
<dd><p>Returns a new {{TextEncoder}} object.
15161576

@@ -1528,11 +1588,7 @@ requires buffering of scalar values.
15281588
<var>destination</var>.
15291589
</dl>
15301590

1531-
<p>The
1532-
<dfn constructor for=TextEncoder lt=TextEncoder() id=dom-textencoder><code>new TextEncoder()</code></dfn>
1533-
constructor steps are to do nothing.
1534-
1535-
<p>The <dfn method for=TextEncoder><code>encode(<var>input</var>)</code></dfn> method steps are:
1591+
<p>To <a>encode an input</a> given a <a>USVString</a> <var>input</var>, run the following steps:
15361592

15371593
<ol>
15381594
<li><p><a for="to I/O queue">Convert</a> <var>input</var> to an <a for=/>I/O queue</a> of scalar
@@ -1563,9 +1619,8 @@ constructor steps are to do nothing.
15631619
</ol>
15641620
</ol>
15651621

1566-
<p>The
1567-
<dfn method for=TextEncoder><code>encodeInto(<var>source</var>, <var>destination</var>)</code></dfn>
1568-
method steps are:
1622+
<p>To <a>encode a source into a destination</a> given a <a>USVString</a> <var>source</var>, and a
1623+
<a>BufferSource</a> <var>destination</var>, run the following steps:
15691624

15701625
<ol>
15711626
<li><p>Let <var>read</var> be 0.
@@ -1627,6 +1682,41 @@ method steps are:
16271682
"{{TextEncoderEncodeIntoResult/written}}" → <var>written</var> ]».
16281683
</ol>
16291684

1685+
<p>The
1686+
<dfn constructor for=TextEncoder lt=TextEncoder() id=dom-textencoder><code>new TextEncoder()</code></dfn>
1687+
constructor steps are to do nothing.
1688+
1689+
<p>The static <dfn method for=TextEncoder><code>encode(<var>input</var>)</code></dfn> method steps
1690+
are:
1691+
1692+
<ol>
1693+
<li><p>Return the result of running the <a>encode an input</a> steps with <var>input</var>.
1694+
</ol>
1695+
1696+
<p>The <dfn method for=TextEncoder><code>encode(<var>input</var>)</code></dfn> method steps are:
1697+
1698+
<ol>
1699+
<li><p>Return the result of running the <a>encode an input</a> steps with <var>input</var>.
1700+
</ol>
1701+
1702+
<p>The static
1703+
<dfn method for=TextEncoder><code>encodeInto(<var>source</var>, <var>destination</var>)</code></dfn>
1704+
method steps are:
1705+
1706+
<ol>
1707+
<li><p>Return the result of running the <a>encode a source into a destination</a> steps with
1708+
<var>source</var> and <var>destination</var>.
1709+
</ol>
1710+
1711+
<p>The
1712+
<dfn method for=TextEncoder><code>encodeInto(<var>source</var>, <var>destination</var>)</code></dfn>
1713+
method steps are:
1714+
1715+
<ol>
1716+
<li><p>Return the result of running the <a>encode a source into a destination</a> steps with
1717+
<var>source</var> and <var>destination</var>.
1718+
</ol>
1719+
16301720
<div class=example id=example-textencoder-encodeinto>
16311721
<p>The <a method=for=TextEncoder lt="encodeInto(source, destination)">encodeInto()</a> method can
16321722
be used to encode a string into an existing {{ArrayBuffer}} object. Various details below are left

0 commit comments

Comments
 (0)