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

オープン
#200 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
frontend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.
主要言語
JavaScript
スター
30
フォーク
5
平均マージ
1時間 57分
マージ済み PR(30日)
10

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

webtides/element-js のほかの issue

webtides/element-js の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。