-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatch.html
358 lines (328 loc) · 37.7 KB
/
patch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html><html lang="en-US"><meta charset="utf-8">
<meta content="width=device-width,initial-scale=1,shrink-to-fit=no" name="viewport">
<meta content="#3c790a" name="theme-color">
<title>Changes to the Encoding Standard to support the Stream Standard</title>
<link href="https://resources.whatwg.org/standard.css" rel="stylesheet">
<link href="https://resources.whatwg.org/bikeshed.css" rel="stylesheet">
<link href="https://resources.whatwg.org/logo-encoding.svg" rel="icon">
<script defer id="head" src="https://resources.whatwg.org/dfn.js"></script>
<style>/* style-syntax-highlighting */
pre.idl.highlight { color: #708090; }
.highlight:not(.idl) { background: hsl(24, 20%, 95%); }
code.highlight { padding: .1em; border-radius: .3em; }
pre.highlight, pre > code.highlight { display: block; padding: 1em; margin: .5em 0; overflow: auto; border-radius: 0; }
.highlight .c { color: #708090 } /* Comment */
.highlight .k { color: #990055 } /* Keyword */
.highlight .l { color: #000000 } /* Literal */
.highlight .n { color: #0077aa } /* Name */
.highlight .o { color: #999999 } /* Operator */
.highlight .p { color: #999999 } /* Punctuation */
.highlight .cm { color: #708090 } /* Comment.Multiline */
.highlight .cp { color: #708090 } /* Comment.Preproc */
.highlight .c1 { color: #708090 } /* Comment.Single */
.highlight .cs { color: #708090 } /* Comment.Special */
.highlight .kc { color: #990055 } /* Keyword.Constant */
.highlight .kd { color: #990055 } /* Keyword.Declaration */
.highlight .kn { color: #990055 } /* Keyword.Namespace */
.highlight .kp { color: #990055 } /* Keyword.Pseudo */
.highlight .kr { color: #990055 } /* Keyword.Reserved */
.highlight .kt { color: #990055 } /* Keyword.Type */
.highlight .ld { color: #000000 } /* Literal.Date */
.highlight .m { color: #000000 } /* Literal.Number */
.highlight .s { color: #a67f59 } /* Literal.String */
.highlight .na { color: #0077aa } /* Name.Attribute */
.highlight .nc { color: #0077aa } /* Name.Class */
.highlight .no { color: #0077aa } /* Name.Constant */
.highlight .nd { color: #0077aa } /* Name.Decorator */
.highlight .ni { color: #0077aa } /* Name.Entity */
.highlight .ne { color: #0077aa } /* Name.Exception */
.highlight .nf { color: #0077aa } /* Name.Function */
.highlight .nl { color: #0077aa } /* Name.Label */
.highlight .nn { color: #0077aa } /* Name.Namespace */
.highlight .py { color: #0077aa } /* Name.Property */
.highlight .nt { color: #669900 } /* Name.Tag */
.highlight .nv { color: #222222 } /* Name.Variable */
.highlight .ow { color: #999999 } /* Operator.Word */
.highlight .mb { color: #000000 } /* Literal.Number.Bin */
.highlight .mf { color: #000000 } /* Literal.Number.Float */
.highlight .mh { color: #000000 } /* Literal.Number.Hex */
.highlight .mi { color: #000000 } /* Literal.Number.Integer */
.highlight .mo { color: #000000 } /* Literal.Number.Oct */
.highlight .sb { color: #a67f59 } /* Literal.String.Backtick */
.highlight .sc { color: #a67f59 } /* Literal.String.Char */
.highlight .sd { color: #a67f59 } /* Literal.String.Doc */
.highlight .s2 { color: #a67f59 } /* Literal.String.Double */
.highlight .se { color: #a67f59 } /* Literal.String.Escape */
.highlight .sh { color: #a67f59 } /* Literal.String.Heredoc */
.highlight .si { color: #a67f59 } /* Literal.String.Interpol */
.highlight .sx { color: #a67f59 } /* Literal.String.Other */
.highlight .sr { color: #a67f59 } /* Literal.String.Regex */
.highlight .s1 { color: #a67f59 } /* Literal.String.Single */
.highlight .ss { color: #a67f59 } /* Literal.String.Symbol */
.highlight .vc { color: #0077aa } /* Name.Variable.Class */
.highlight .vg { color: #0077aa } /* Name.Variable.Global */
.highlight .vi { color: #0077aa } /* Name.Variable.Instance */
.highlight .il { color: #000000 } /* Literal.Number.Integer.Long */
</style>
<style>
.addition {
font-weight: bold;
color: #00bb00;
margin-left: -0.5em;
}
</style>
<div class="head">
<h1>Changes to the <a href="https://encoding.spec.whatwg.org">Encoding
Standard</a> to support the <a href="https://streams.spec.whatwg.org/">Stream
Standard</a></h1>
<dl>
<dt>On Github:
<dd><a href="https://github.com/ricea/encoding-streams">Repository</a>
(<a href="https://github.com/ricea/encoding-streams/issues">open issues</a>)
</dl>
</div>
<h3>In 8.1, Interface <code title="">TextDecoder</code>:</h3>
<p class="addition">In the IDL, add
<p><pre class="idl highlight def">
[<a class="nv idl-code" data-link-type="extended-attribute" href="https://heycam.github.io/webidl/#SameObject" id="ref-for-SameObject">SameObject</a>] <span class="kt">readonly</span> <span class="kt">attribute</span> <a class="n" data-link-type="idl-name" href="https://streams.spec.whatwg.org/#rs-class" id="ref-for-rs-class">ReadableStream</a> <a class="nv idl-code" data-link-type="attribute" data-readonly="" data-type="ReadableStream" href="#dom-textdecoder-readable" id="ref-for-dom-textdecoder-readable">readable</a>;
[<a class="nv idl-code" data-link-type="extended-attribute" href="https://heycam.github.io/webidl/#SameObject" id="ref-for-SameObject①">SameObject</a>] <span class="kt">readonly</span> <span class="kt">attribute</span> <a class="n" data-link-type="idl-name" href="https://streams.spec.whatwg.org/#ws-class" id="ref-for-ws-class">WritableStream</a> <a class="nv idl-code" data-link-type="attribute" data-readonly="" data-type="WritableStream" href="#dom-textdecoder-writable" id="ref-for-dom-textdecoder-writable">writable</a>;
</pre>
<p class="addition">to the TextDecoder interface.
<p class="addition">In the paragraph following the IDL, insert ", and <dfn data-dfn-for="TextDecoder" data-dfn-type="dfn" data-noexport="" id="textdecoder-transform">transform<a class="self-link" href="#textdecoder-transform"></a></dfn>." at the end of the sentence.
<p class="addition">To the DOM intro note, add
<p><dl class="domintro">
<dt><code><var>decoder</var> . <a class="idl-code" data-link-type="attribute" href="#dom-textdecoder-readable">readable</a></code>
<dd>
<p>Returns a <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#readable-stream" id="ref-for-readable-stream">readable stream</a> whose <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#chunk" id="ref-for-chunk">chunks</a> are strings resulting from running <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding②">encoding</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#decoder" id="ref-for-decoder①①">decoder</a> on the chunks written to <code class="idl"><a data-link-type="idl" href="#dom-textdecoder-writable" id="ref-for-dom-textdecoder-writable①">writable</a></code>. </p>
<dt><code><var>decoder</var> . <a class="idl-code" data-link-type="attribute" href="#dom-textdecoder-writable">writable</a></code>
<dd>
<p>Returns a <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#writable-stream" id="ref-for-writable-stream">writable stream</a> which accepts <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#BufferSource" id="ref-for-BufferSource①">BufferSource</a></code> chunks and runs them through <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding③">encoding</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#decoder" id="ref-for-decoder①②">decoder</a> before making them available to <code class="idl"><a data-link-type="idl" href="#dom-textdecoder-readable" id="ref-for-dom-textdecoder-readable②">readable</a></code>. </p>
<p>Typically this will be used via the <code class="idl"><a data-link-type="idl" href="https://streams.spec.whatwg.org/#rs-pipe-through" id="ref-for-rs-pipe-through">pipeThrough()</a></code> method on a <code class="idl"><a data-link-type="idl" href="https://streams.spec.whatwg.org/#rs-class" id="ref-for-rs-class①">ReadableStream</a></code> source. </p>
<pre class="example" id="example-textdecode-writable"><a class="self-link" href="#example-textdecode-writable"></a><code class="lang-javascript highlight"><span class="kd">var</span> decoder <span class="o">=</span> <span class="k">new</span> TextDecoder<span class="p">(</span>encoding<span class="p">);</span>
byteReadable
<span class="p">.</span>pipeThrough<span class="p">(</span>decoder<span class="p">)</span>
<span class="p">.</span>pipeTo<span class="p">(</span>textWritable<span class="p">);</span></code></pre>
<p>If the <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode①">error mode</a> is "<code>fatal</code>" and <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding④">encoding</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#decoder" id="ref-for-decoder①③">decoder</a> returns <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#error" id="ref-for-error④">error</a>, both <code class="idl"><a data-link-type="idl" href="#dom-textdecoder-readable" id="ref-for-dom-textdecoder-readable③">readable</a></code> and <code class="idl"><a data-link-type="idl" href="#dom-textdecoder-writable" id="ref-for-dom-textdecoder-writable③">writable</a></code> will be errored with a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#exceptiondef-typeerror" id="ref-for-exceptiondef-typeerror">TypeError</a></code>. </p>
</dl>
<p class="addition">To the <dfn class="idl-code" data-dfn-for="TextDecoder" data-dfn-type="constructor" data-export="" id="dom-textdecoder"><code>TextDecoder(<var>label</var>, <var>options</var>)</code><a class="self-link" href="#dom-textdecoder"></a></dfn> constructor steps, before the final step, add:
<ol start="7">
<li>
<p>Let <var>decForTransform</var> be a new <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textdecoder" id="ref-for-textdecoder⑤">TextDecoder</a></code> object. </p>
<li>
<p>Set <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding⑧">encoding</a> to <var>encoding</var>. </p>
<li>
<p>Set <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode④">error mode</a> to <var>dec</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode⑤">error mode</a>. </p>
<li>
<p>Set <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-ignore-bom-flag" id="ref-for-textdecoder-ignore-bom-flag③">ignore BOM flag</a> to <var>dec</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-ignore-bom-flag" id="ref-for-textdecoder-ignore-bom-flag④">ignore BOM flag</a>. </p>
<li>
<p>Set <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-decoder" id="ref-for-textdecoder-decoder">decoder</a> to a new <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#decoder" id="ref-for-decoder①⑥">decoder</a> for <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding⑨">encoding</a>, and set <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-stream" id="ref-for-textdecoder-stream">stream</a> to a new <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream" id="ref-for-concept-stream①④">stream</a>. </p>
<p class="note" role="note">For simplicity, <var>dec</var> and <var>decForTransform</var> have redundant
members. However, the <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-bom-seen-flag" id="ref-for-textdecoder-bom-seen-flag③">BOM seen flag</a>, <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-do-not-flush-flag" id="ref-for-textdecoder-do-not-flush-flag">do not flush
flag</a>, and <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform">transform</a> are unused in <var>decForTransform</var>, and <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-encoding" id="ref-for-textdecoder-encoding①⓪">encoding</a>, <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-ignore-bom-flag" id="ref-for-textdecoder-ignore-bom-flag⑤">ignore BOM flag</a> and <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode⑥">error mode</a> are identical to <var>dec</var>. It is not necessary for
implementations to duplicate these member fields. </p>
<li>
<p>Let <var>startAlgorithm</var> be an algorithm that takes no arguments and returns nothing. </p>
<li>
<p>Let <var>transformAlgorithm</var> be an algorithm which takes a <var>chunk</var> argument
and runs the <a data-link-type="dfn" href="#concept-td-decode-and-enqueue" id="ref-for-concept-td-decode-and-enqueue">decode and enqueue a chunk</a> algorithm with <var>decForTransform</var> and <var>chunk</var>. </p>
<li>
<p>Let <var>flushAlgorithm</var> be an algorithm which takes no arguments and runs the <a data-link-type="dfn" href="#concept-td-flush-and-enqueue" id="ref-for-concept-td-flush-and-enqueue">flush
and enqueue</a> algorithm with <var>decForTransform</var>. </p>
<li>
<p>Let <var>transform</var> be the result of calling <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#create-transform-stream" id="ref-for-create-transform-stream">CreateTransformStream</a>(<var>startAlgorithm</var>, <var>transformAlgorithm</var>, <var>flushAlgorithm</var>). </p>
<li>
<p>Set <var>dec</var>’s <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform①">transform</a> to <var>transform</var>. </p>
</ol>
<p class="addition">After the description of the ignoreBOM attribute, add:
<p>The <dfn class="idl-code" data-dfn-for="TextDecoder" data-dfn-type="attribute" data-export="" id="dom-textdecoder-readable"><code>readable</code><a class="self-link" href="#dom-textdecoder-readable"></a></dfn> attribute’s getter must return <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform①">transform</a>.[[readable]]. </p>
<p>The <dfn class="idl-code" data-dfn-for="TextDecoder" data-dfn-type="attribute" data-export="" id="dom-textdecoder-writable"><code>writable</code><a class="self-link" href="#dom-textdecoder-writable"></a></dfn> attribute’s getter must return <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform②">transform</a>.[[writable]]. </p>
<p class="addition">After the definition of the decode() method, add:
<p>The <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport="" id="concept-td-decode-and-enqueue">decode and enqueue a chunk</dfn> algorithm, given a <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textdecoder" id="ref-for-textdecoder⑥">TextDecoder</a></code> <var>decForTransform</var> and a <var>chunk</var>, runs these steps: </p>
<ol>
<li>
<p>If <a data-link-type="dfn" href="https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values" id="ref-for-sec-ecmascript-data-types-and-values">Type</a>(<var>chunk</var>) is not Object, or <var>chunk</var> does not have an
[[ArrayBufferData]] internal slot, or <a data-link-type="abstract-op" href="https://tc39.github.io/ecma262/#sec-isdetachedbuffer" id="ref-for-sec-isdetachedbuffer">IsDetachedBuffer</a>(<var>chunk</var>) is
true, or <a data-link-type="abstract-op" href="https://tc39.github.io/ecma262/#sec-issharedarraybuffer" id="ref-for-sec-issharedarraybuffer">IsSharedArrayBuffer</a>(<var>chunk</var>) is true, then return a new
promise rejected with a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#exceptiondef-typeerror" id="ref-for-exceptiondef-typeerror③">TypeError</a></code> exception. </p>
<li>
<p><a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream-push" id="ref-for-concept-stream-push③">Push</a> a <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-get-buffer-source-copy" id="ref-for-dfn-get-buffer-source-copy①">copy of</a> <var>chunk</var> to <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-stream" id="ref-for-textdecoder-stream⑤">stream</a>. </p>
<li>
<p>Let <var>controller</var> be <var>decForTransform</var>’s <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform④">transform</a>.[[transformStreamController]]. </p>
<li>
<p>Let <var>output</var> be a new <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream" id="ref-for-concept-stream①⑦">stream</a>. </p>
<li>
<p>While true, run these steps: </p>
<ol>
<li>
<p>Let <var>token</var> be the result of <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream-read" id="ref-for-concept-stream-read⑥">reading</a> from <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-stream" id="ref-for-textdecoder-stream⑥">stream</a>. </p>
<li>
<p>If <var>token</var> is <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#end-of-stream" id="ref-for-end-of-stream⑥">end-of-stream</a>, then run these steps: </p>
<ol>
<li>
<p>Let <var>outputChunk</var> be <var>output</var>, <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-td-serialize" id="ref-for-concept-td-serialize②">serialized</a>. </p>
<li>
<p>Call <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#transform-stream-default-controller-enqueue" id="ref-for-transform-stream-default-controller-enqueue">TransformStreamDefaultControllerEnqueue</a>(<var>controller</var>, <var>outputChunk</var>). </p>
<li>
<p>Return a new promise resolved with undefined. </p>
</ol>
<li>
<p>Let <var>result</var> be the result of <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-encoding-process" id="ref-for-concept-encoding-process②">processing</a> <var>token</var> for <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-decoder" id="ref-for-textdecoder-decoder④">decoder</a>, <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-stream" id="ref-for-textdecoder-stream⑦">stream</a>, <var>output</var>, and <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode⑨">error mode</a>. </p>
<li>
<p>If <var>result</var> is <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#error" id="ref-for-error⑦">error</a>, then return a new promise rejected with a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#exceptiondef-typeerror" id="ref-for-exceptiondef-typeerror④">TypeError</a></code> exception. </p>
</ol>
</ol>
<p>The <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport="" id="concept-td-flush-and-enqueue">flush and enqueue</dfn> algorithm, which handles the end
of data from the input <code class="idl"><a data-link-type="idl" href="https://streams.spec.whatwg.org/#rs-class" id="ref-for-rs-class②">ReadableStream</a></code>, given a <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textdecoder" id="ref-for-textdecoder⑦">TextDecoder</a></code> <var>decForTransform</var>, runs
these steps: </p>
<ol>
<li>
<p>Let <var>output</var> be a new <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream" id="ref-for-concept-stream①⑧">stream</a>. </p>
<li>
<p>Let <var>result</var> be the result of <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-encoding-process" id="ref-for-concept-encoding-process③">processing</a> <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#end-of-stream" id="ref-for-end-of-stream⑦">end-of-stream</a> for <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-decoder" id="ref-for-textdecoder-decoder⑤">decoder</a> and <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-stream" id="ref-for-textdecoder-stream⑧">stream</a>, <var>output</var>, and <var>decForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textdecoder-error-mode" id="ref-for-textdecoder-error-mode①⓪">error mode</a>. </p>
<li>
<p>If <var>result</var> is <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#finished" id="ref-for-finished②">finished</a>, then run these steps: </p>
<ol>
<li>
<p>Let <var>outputChunk</var> be <var>output</var>, <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-td-serialize" id="ref-for-concept-td-serialize③">serialized</a>. </p>
<li>
<p>Let <var>controller</var> be <var>decForTransform</var>’s <a data-link-type="dfn" href="#textdecoder-transform" id="ref-for-textdecoder-transform⑤">transform</a>.[[transformStreamController]]. </p>
<li>
<p>Call <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#transform-stream-default-controller-enqueue" id="ref-for-transform-stream-default-controller-enqueue①">TransformStreamDefaultControllerEnqueue</a>(<var>controller</var>, <var>outputChunk</var>). </p>
<li>
<p>Return a new promise resolved with undefined. </p>
</ol>
<li>
<p>Otherwise, return a new promise rejected with a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#exceptiondef-typeerror" id="ref-for-exceptiondef-typeerror⑤">TypeError</a></code> exception. </p>
</ol>
<h3>In 8.2 Interface <code title="">TextEncoder</code>, add:</h3>
<p class="addition">In the IDL, add
<p><pre class="idl highlight def">
[<a class="nv idl-code" data-link-type="extended-attribute" href="https://heycam.github.io/webidl/#SameObject" id="ref-for-SameObject②">SameObject</a>] <span class="kt">readonly</span> <span class="kt">attribute</span> <a class="n" data-link-type="idl-name" href="https://streams.spec.whatwg.org/#rs-class" id="ref-for-rs-class③">ReadableStream</a> <a class="nv idl-code" data-link-type="attribute" data-readonly="" data-type="ReadableStream" href="#dom-textencoder-readable" id="ref-for-dom-textencoder-readable">readable</a>;
[<a class="nv idl-code" data-link-type="extended-attribute" href="https://heycam.github.io/webidl/#SameObject" id="ref-for-SameObject③">SameObject</a>] <span class="kt">readonly</span> <span class="kt">attribute</span> <a class="n" data-link-type="idl-name" href="https://streams.spec.whatwg.org/#ws-class" id="ref-for-ws-class①">WritableStream</a> <a class="nv idl-code" data-link-type="attribute" data-readonly="" data-type="WritableStream" href="#dom-textencoder-writable" id="ref-for-dom-textencoder-writable">writable</a>;
</pre>
<p class="addition">to the TextEncoder interface.
<p class="addition">In the paragraph following the IDL, insert " and <dfn data-dfn-for="TextEncoder" data-dfn-type="dfn" data-noexport="" id="textencoder-transform">transform<a class="self-link" href="#textencoder-transform"></a></dfn> and <dfn data-dfn-for="TextEncoder" data-dfn-type="dfn" data-lt="pending high surrogate" data-noexport="" id="textencoder-pending-high-surrogate">pending high
surrogate<a class="self-link" href="#textencoder-pending-high-surrogate"></a></dfn> (initially null)" at the end of the sentence.
<p class="addition">To the DOM intro note, add
<p><dl class="domintro">
<dt><code><var>encoder</var> . <a class="idl-code" data-link-type="attribute" href="#dom-textencoder-readable">readable</a></code>
<dd>
<p>Returns a <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#readable-stream" id="ref-for-readable-stream①">readable stream</a> whose <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#chunk" id="ref-for-chunk①">chunks</a> are <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-Uint8Array" id="ref-for-idl-Uint8Array②">Uint8Array</a></code>s resulting from running <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#utf-8" id="ref-for-utf-8①⑤">UTF-8</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#encoder" id="ref-for-encoder⑨">encoder</a> on the chunks written to <code class="idl"><a data-link-type="idl" href="#dom-textencoder-writable" id="ref-for-dom-textencoder-writable①">writable</a></code>. </p>
<dt><code><var>encoder</var> . <a class="idl-code" data-link-type="attribute" href="#dom-textencoder-writable" id="ref-for-dom-textencoder-writable②">writable</a></code>
<dd>
<p>Returns a <a data-link-type="dfn" href="https://streams.spec.whatwg.org/#writable-stream" id="ref-for-writable-stream①">writable stream</a> which accepts string chunks and runs them through <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#utf-8" id="ref-for-utf-8①⑥">UTF-8</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#encoder" id="ref-for-encoder①⓪">encoder</a> before making them available to <code class="idl"><a data-link-type="idl" href="#dom-textencoder-readable" id="ref-for-dom-textencoder-readable②">readable</a></code>. </p>
<p>Typically this will be used via the <code class="idl"><a data-link-type="idl" href="https://streams.spec.whatwg.org/#rs-pipe-through" id="ref-for-rs-pipe-through①">pipeThrough()</a></code> method on a <code class="idl"><a data-link-type="idl" href="https://streams.spec.whatwg.org/#rs-class" id="ref-for-rs-class④">ReadableStream</a></code> source. </p>
<pre class="example" id="example-textencode-writable"><a class="self-link" href="#example-textencode-writable"></a><code class="lang-javascript highlight">textReadable
<span class="p">.</span>pipeThrough<span class="p">(</span><span class="k">new</span> TextEncoder<span class="p">())</span>
<span class="p">.</span>pipeTo<span class="p">(</span>byteWritable<span class="p">);</span></code></pre>
</dl>
<p class="addition">To the <dfn class="idl-code" data-dfn-for="TextEncoder" data-dfn-type="constructor" data-export="" id="dom-textencoder"><code>TextEncoder()</code><a class="self-link" href="#dom-textencoder"></a></dfn> constructor steps, before the final step, add:
<ol start="3">
<li>
<p>Let <var>encForTransform</var> be a new <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textencoder" id="ref-for-textencoder⑥">TextEncoder</a></code> object. </p>
<li>
<p>Set <var>encForTransform</var>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textencoder-encoder" id="ref-for-textencoder-encoder①">encoder</a> to <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#utf-8" id="ref-for-utf-8①⑨">UTF-8</a>’s <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#encoder" id="ref-for-encoder①③">encoder</a>. </p>
<p class="note" role="note">For simplicity, <var>enc</var> and <var>encForTransform</var> have the same
members. However, <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform">transform</a> is not used by <var>encForTransform</var> and <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate">pending high surrogate</a> is not used by <var>enc</var>. It is not necessary
for implementations to store these unused member fields. </p>
<li>
<p>Let <var>startAlgorithm</var> be an algorithm that takes no arguments and returns nothing. </p>
<li>
<p>Let <var>transformAlgorithm</var> be an algorithm which takes a <var>chunk</var> argument
and runs the <a data-link-type="dfn" href="#concept-te-encode-and-enqueue" id="ref-for-concept-te-encode-and-enqueue">encode and enqueue a chunk</a> algorithm with <var>encForTransform</var> and <var>chunk</var>. </p>
<li>
<p>Let <var>flushAlgorithm</var> be an algorithm which runs the <a data-link-type="dfn" href="#concept-te-encode-and-flush" id="ref-for-concept-te-encode-and-flush">encode and flush</a> algorithm with <var>encForTransform</var>. </p>
<li>
<p>Let <var>transform</var> be the result of calling <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#create-transform-stream" id="ref-for-create-transform-stream①">CreateTransformStream</a>(<var>startAlgorithm</var>, <var>transformAlgorithm</var>, <var>flushAlgorithm</var>). </p>
<li>
<p>Set <var>enc</var>’s <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform①">transform</a> to <var>transform</var>. </p>
</ol>
<p class="addition">After the description of the encoding attribute, add:
<p>The <dfn class="idl-code" data-dfn-for="TextEncoder" data-dfn-type="attribute" data-export="" id="dom-textencoder-readable"><code>readable</code><a class="self-link" href="#dom-textencoder-readable"></a></dfn> attribute’s getter must return <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform①">transform</a>.[[readable]]. </p>
<p>The <dfn class="idl-code" data-dfn-for="TextEncoder" data-dfn-type="attribute" data-export="" id="dom-textencoder-writable"><code>writable</code><a class="self-link" href="#dom-textencoder-writable"></a></dfn> attribute’s getter must return <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform②">transform</a>.[[writable]]. </p>
<p class="addition">After the definition of the encode() method, add:
<p>The <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport="" id="concept-te-encode-and-enqueue">encode and enqueue a chunk</dfn> algorithm, given a <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textencoder" id="ref-for-textencoder⑦">TextEncoder</a></code> <var>encForTransform</var> and <var>chunk</var>, runs these steps: </p>
<ol>
<li>
<p>Let <var>input</var> be the result of <a data-link-type="dfn" href="https://heycam.github.io/webidl/#dfn-convert-ecmascript-to-idl-value" id="ref-for-dfn-convert-ecmascript-to-idl-value">converting</a> <var>chunk</var> to a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-DOMString" id="ref-for-idl-DOMString③">DOMString</a></code>. If this throws an exception, then return a promise rejected
with that exception. </p>
<li>
<p>Convert <var>input</var> to a <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream" id="ref-for-concept-stream②①">stream</a>. </p>
<li>
<p>Let <var>output</var> be a new <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream" id="ref-for-concept-stream②②">stream</a>. </p>
<li>
<p>Let <var>controller</var> be <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform④">transform</a>.[[transformStreamController]]. </p>
<li>
<p>While true, run these steps: </p>
<ol>
<li>
<p>Let <var>token</var> be the result of <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream-read" id="ref-for-concept-stream-read⑧">reading</a> from <var>input</var>. </p>
<li>
<p>If <var>token</var> is <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#end-of-stream" id="ref-for-end-of-stream⑧">end-of-stream</a>, then run these steps: </p>
<ol>
<li>
<p>Convert <var>output</var> into a byte sequence. </p>
<li>
<p>Let <var>chunk</var> be a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-Uint8Array" id="ref-for-idl-Uint8Array④">Uint8Array</a></code> object wrapping an <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-ArrayBuffer" id="ref-for-idl-ArrayBuffer③">ArrayBuffer</a></code> containing <var>output</var>. </p>
<li>
<p>Call <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#transform-stream-default-controller-enqueue" id="ref-for-transform-stream-default-controller-enqueue②">TransformStreamDefaultControllerEnqueue</a>(<var>controller</var>, <var>chunk</var>). </p>
<li>
<p>Return a new promise resolved with undefined. </p>
</ol>
<li>
<p>Let <var>result</var> be the result of executing the <a data-link-type="dfn" href="#concept-te-convert-code-unit-to-scalar-value" id="ref-for-concept-te-convert-code-unit-to-scalar-value">convert code unit to scalar
value</a> algorithm with <var>encForTransform</var>, <var>token</var> and <var>input</var>. </p>
<li>
<p>If <var>result</var> is not <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#continue" id="ref-for-continue③">continue</a>, then <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-encoding-process" id="ref-for-concept-encoding-process⑤">process</a> <var>result</var> for <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#textencoder-encoder" id="ref-for-textencoder-encoder③">encoder</a>, <var>input</var>, <var>output</var>. </p>
</ol>
</ol>
<p>The <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport="" id="concept-te-convert-code-unit-to-scalar-value">convert code unit to scalar value</dfn> algorithm, given a <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textencoder" id="ref-for-textencoder⑧">TextEncoder</a></code> <var>encForTransform</var>, <var>token</var> and <var>input</var> stream, runs these steps: </p>
<ol>
<li>
<p>If <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate①">pending high surrogate</a> is non-null, then run these
steps: </p>
<ol>
<li>
<p>Let <var>high surrogate</var> be <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate②">pending high
surrogate</a>. </p>
<li>
<p>Set <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate③">pending high surrogate</a> to null. </p>
<li>
<p>If <var>token</var> is in the range U+DC00 to U+DFFF, inclusive, then return a code point
whose value is 0x10000 + ((<var>high surrogate</var> − 0xD800) << 10) +
(<var>token</var> − 0xDC00). </p>
<li>
<p><a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#concept-stream-prepend" id="ref-for-concept-stream-prepend④">Prepend</a> <var>token</var> to <var>input</var>. </p>
<li>
<p>Return U+FFFD. </p>
</ol>
<li>
<p>If <var>token</var> is in the range U+D800 to U+DBFF, inclusive, then set <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate④">pending high
surrogate</a> to <var>token</var> and return <a data-link-type="dfn" href="https://encoding.spec.whatwg.org/#continue" id="ref-for-continue④">continue</a>. </p>
<li>
<p>If <var>token</var> is in the range U+DC00 to U+DFFF, inclusive, then return U+FFFD. </p>
<li>
<p>Return <var>token</var>. </p>
</ol>
<p class="note" role="note">This is equivalent to the "<a data-link-type="dfn" href="https://infra.spec.whatwg.org/#javascript-string-convert" id="ref-for-javascript-string-convert">convert</a> a <a data-link-type="dfn" href="https://infra.spec.whatwg.org/#javascript-string" id="ref-for-javascript-string">JavaScript string</a> into a <a data-link-type="dfn" href="https://infra.spec.whatwg.org/#scalar-value-string" id="ref-for-scalar-value-string">scalar
value string</a>" algorithm from the Infra Standard, but allows for surrogate pairs that are split
between strings. </p>
<p>The <dfn class="dfn-paneled" data-dfn-type="dfn" data-noexport="" id="concept-te-encode-and-flush">encode and flush</dfn> algorithm, given a <code class="idl"><a data-link-type="idl" href="https://encoding.spec.whatwg.org/#textencoder" id="ref-for-textencoder⑨">TextEncoder</a></code> <var>encForTransform</var>, runs these steps: </p>
<ol>
<li>
<p>If <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-pending-high-surrogate" id="ref-for-textencoder-pending-high-surrogate⑤">pending high surrogate</a> is non-null, then run these
steps: </p>
<ol>
<li>
<p>Let <var>controller</var> be <var>encForTransform</var>’s <a data-link-type="dfn" href="#textencoder-transform" id="ref-for-textencoder-transform⑤">transform</a>.[[transformStreamController]]. </p>
<li>
<p>Let <var>output</var> be the byte sequence 0xEF 0xBF 0xBD. </p>
<p class="note" role="note">This is the replacement character U+FFFD encoded as UTF-8. </p>
<li>
<p>Let <var>chunk</var> be a <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-Uint8Array" id="ref-for-idl-Uint8Array⑤">Uint8Array</a></code> object wrapping an <code class="idl"><a data-link-type="idl" href="https://heycam.github.io/webidl/#idl-ArrayBuffer" id="ref-for-idl-ArrayBuffer④">ArrayBuffer</a></code> containing <var>output</var>. </p>
<li>
<p>Call <a data-link-type="abstract-op" href="https://streams.spec.whatwg.org/#transform-stream-default-controller-enqueue" id="ref-for-transform-stream-default-controller-enqueue③">TransformStreamDefaultControllerEnqueue</a>(<var>controller</var>, <var>chunk</var>). </p>
</ol>
<li>
<p>Return a new promise resolved with undefined. </p>
</ol>