Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static methods for TextEncoder and TextDecoder #284

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

lucacasonato
Copy link
Member

@lucacasonato lucacasonato commented Apr 22, 2022

NOTE: this PR is currently blocked on a technicality (speced/bikeshed#2270).

This PR adds support for static TextDecoder.decode, TextEncoder.encode and
TextEncoder.encodeInto methods. These do not add new functionality, rather
just acting as useful helpers for "one time" text encoding/decoding operations.

Closes #267.

  • At least two implementers are interested (and none opposed):
    • Deno
  • Tests are written and can be reviewed and commented upon at:
  • Implementation bugs are filed:
    • Chrome: …
    • Firefox: …
    • Safari: …
    • Deno: …
    • Node.js: …

Preview | Diff

@@ -1333,6 +1333,10 @@ dictionary TextDecoderOptions {
boolean ignoreBOM = false;
};

dictionary TextDecoderOptionsWithLabel : TextDecoderOptions {
DOMString label = "utf-8";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can get away with just supporting UTF-8 here. If you need more, you get the streaming API. Perhaps not even support fatal or ignoreBOM. Also makes it easier to implement without a lot of IDL bridging.

@@ -1351,13 +1357,17 @@ TextDecoder includes TextDecoderCommon;
initially false.

<dl class=domintro>
<dt>{{TextDecoder}} . <a method for=TextDecoder lt=decode()>decode([<var>input</var> [, <var>options</var>]])</a></code>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<dt>{{TextDecoder}} . <a method for=TextDecoder lt=decode()>decode([<var>input</var> [, <var>options</var>]])</a></code>
<dt><code><a>TextDecoder</a> . <a method for=TextDecoder lt=decode()>decode([<var>input</var> [, <var>options</var>]])</a></code>

@@ -1351,13 +1357,17 @@ TextDecoder includes TextDecoderCommon;
initially false.

<dl class=domintro>
<dt>{{TextDecoder}} . <a method for=TextDecoder lt=decode()>decode([<var>input</var> [, <var>options</var>]])</a></code>
<dd>
<p>Returns the result of running <a for=TextDecoderCommon>encoding</a>'s <a for=/>decoder</a>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below this should probably be prefixed with decoder's and here it needs some rewording as there is no instance to speak of.

constructor steps are to do nothing.

<p>The <dfn method for=TextEncoder><code>encode(<var>input</var>)</code></dfn> method steps are:
<p>To <a>encode an input</a> given a <a>USVString</a> <var>input</var>, run the following steps:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs <dfn>.

<p>The
<dfn method for=TextEncoder><code>encodeInto(<var>source</var>, <var>destination</var>)</code></dfn>
method steps are:
<p>To <a>encode a source into a destination</a> given a <a>USVString</a> <var>source</var>, and a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs <dfn>.

@jasnell
Copy link

jasnell commented Apr 7, 2024

@lucacasonato ... are you still wanting to push this forward. I'm interested in implementing this for both workers and node.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements topic: api
Development

Successfully merging this pull request may close these issues.

Add a static decode and encode method to TextEncoder and TextDecoder
3 participants