[RENDERER]: a line break at the start of a <textarea> differs between toString() and a client render

Aperta
#200 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
frontend

Direzione di ricerca

Start with parseRawText() in src/dom-parts/parseTemplate.js and the text writer in src/dom-parts/stringify.js, as named in the proposal. Reproduce both textarea cases from the issue and compare toString(), client render, and hydration values. Done means both cases preserve the specified value across all three paths, while style, script, and title output remains unchanged.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Goal

A <textarea> with interpolated content has the same value in markup from toString(), after a client render and after hydration, also when a line break stands at the start of its content.

Context

The HTML parser drops one line break that directly follows a <textarea> start tag, also when it is written as &#10;. textContent, which the client render writes, drops nothing. The renderer treats both the same, so the two sides disagree in two cases. Measured in Chromium with 1.3.1 and with #189 at 802e47a.

Case 1, the value starts with a line break:

const template = html`<textarea>${'\nMilk'}</textarea>`;
template.toString(); // …<textarea>\nMilk</textarea>…, the parser makes it "Milk"
template.renderInto(node); // "\nMilk"

Case 2, the literal has a line break after the start tag:

html`<textarea
    id="input-${this.name}"
    name="${this.name}"
    rows="${this.rows}"
>
${this.value}</textarea>`;

textarea.value for each case (⏎ is the line break in the literal):

Version Literal Value Parsed from toString() Client render After hydration
1.3.1 and #189 <textarea>${v}</textarea> "\nMilk" "Milk" "\nMilk" "\nMilk"
#189 <textarea>⏎${v}</textarea> "Milk" "Milk" "\nMilk" "\nMilk"
#189 <textarea>⏎${v}</textarea> "\nMilk" "\nMilk" "\n\nMilk" "\n\nMilk"

In case 1 the server-rendered field has lost the first line break until the element hydrates, and for good without JavaScript: saving the form writes the text back without it.

In case 2 the server-rendered field is right, and hydration then puts an empty line in front of the text. Prettier (3.9.10) and Oxfmt both write exactly this literal once a <textarea> has enough attributes to wrap: the line break after the start tag is the one place where it does not change the content. In 1.3.1 that literal does not render at all (#185: the client throws ParseTemplateError, toString() writes the part markers into the textarea as text), so case 2 only shows with #189.

Seen in @webtides/element-library: el-textarea-field renders its value as the content of the textarea since webtides/element-library#93. A stored text that starts with an empty line loses it in server-rendered markup. Its template is kept away from the formatter with // oxfmt-ignore because of #185. With #189 that could go, but case 2 would take its place.

<pre> is not affected. The parser drops a leading line break there too, but the content is a node part that starts with a comment marker: html`<pre>${'\nMilk'}</pre>` gives "\nMilk" on the server, on the client and after hydration, in both versions.

Acceptance criteria

  • For html`<textarea>${value}</textarea>` and a value that starts with a line break, the textarea parsed from toString() has exactly that value, the same as after a client render.
  • One line break in the literal directly after the <textarea …> start tag is not content: a literal written that way gives value on the server, on the client and after hydration.
  • Hydrating either markup leaves the value of the textarea as it is.
  • <style>, <script> and <title> are written as before. The parser drops nothing there.

Proposal (non-binding)

One rule for textarea, mirroring the parser on both sides:

  • When the literal is parsed, drop one line break (\n or \r\n) at the start of the first static string of a textarea's content.
  • When markup is written, put a line break after the start tag if the joined content starts with one. The parser then drops that one and leaves the content alone.

In #189 that is parseRawText() in src/dom-parts/parseTemplate.js and the text writer in src/dom-parts/stringify.js.

Out of scope

  • 1.3.1 drops static text next to a single interpolation on the client: html`<textarea>Dear ${'Ann'}</textarea>` renders Ann, while toString() writes Dear Ann. #189 fixes it, the client renders Dear Ann there.
  • A textarea without interpolations. Both sides parse the same markup, so they agree.
  • Working around it in element-library by adding the line break in the component. The component cannot tell a server render from a client render, so the value would be wrong on one of the two.
Lingua principale
JavaScript
Stelle
30
Fork
5
Merge medio
1h 57m
PR unite (30g)
10

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di webtides/element-js

Tutte le issue di webtides/element-js

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.