[Web] Leading, trailing, and consecutive spaces in <p> and <codeblock> are stripped by setContent on mount / setValue

未关闭
#886 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

@pcbeingused333 已经在做这个了。

开始于 2026年11月27日。

  • #887 来自 @pcbeingused333 —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
react, react-native, typescript
领域
frontend, web-dev

调研方向

Start with src/web/EnrichedTextInput.tsx and inspect the setContent calls in onCreate and setValue, then read src/web/formats/EnrichedCodeBlock.ts and the htmlNormalizer.ts behavior described in the issue. Reproduce with the supplied defaultValue example and compare the resulting getHTML() output after mount and setValue. Done means the reported spaces survive both paths without turning layout newlines between blocks into extra content; pull request #887 is already open.

由索引模型根据 Issue 内容生成。

描述

Description

On web, <EnrichedTextInput> renders .ProseMirror with white-space: pre-wrap; white-space: break-spaces, so leading, trailing, and consecutive spaces typed by the user are stored in ProseMirror and serialized into HTML by getHTML() / onChangeHtml (e.g. <codeblock><p> const x = 1;</p></codeblock> or <p> hello</p>).

However, when that same HTML is passed back via defaultValue (initial mount / reload) or ref.current.setValue(html), leading and trailing spaces are stripped and consecutive spaces are collapsed to a single space.

Minimal Reproduction
<EnrichedTextInput
  defaultValue='<html><codeblock><p>  const x = 1;</p></codeblock><p>  hello   world </p></html>'
/>

Or via ref.current.setValue(...):

  1. Type leading spaces in a <codeblock> line (indentation) and at the beginning of a regular <p> paragraph.
  2. Read await ref.current.getHTML() → returns <html><codeblock><p> const x = 1;</p></codeblock><p> hello world</p></html>.
  3. Pass that HTML to another <EnrichedTextInput defaultValue={...} /> or call ref.current.setValue(html).

Expected:
Leading indentation in <codeblock>, leading/trailing spaces in <p>, and consecutive spaces are preserved ( const x = 1; and hello world).

Actual:
ProseMirror strips leading and trailing spaces and collapses consecutive spaces into a single space (const x = 1; and hello world), even inside <codeblock>. Additionally, any subsequent edit silently overwrites the persisted HTML without those spaces.

Root Cause (in [email protected])
  1. Both onCreate (e.commands.setContent(tiptapContent ?? '')) and setValue (e.commands.setContent(prepareHtmlForTiptap(...))) in src/web/EnrichedTextInput.tsx call TipTap's setContent without parseOptions: { preserveWhitespace: 'full' } (or true).
  2. Because ProseMirror's DOMParser defaults to preserveWhitespace: false:
    • addTextNode (prosemirror-model) collapses [ \t\r\n\u000c]+ to ' ' and slices off the leading whitespace at the start of every block (!nodeBefore),
    • NodeContext.finish() strips trailing whitespace at the end of every block.
  3. Even <codeblock> is affected because EnrichedCodeBlock (src/web/formats/EnrichedCodeBlock.ts) extends Blockquote (content: '(paragraph)+' rendered as <codeblock><p>...</p></codeblock>) without whitespace: 'pre', so its child <p> nodes use the same default HTML whitespace collapsing rules.

(Note: when useHtmlNormalizer is true, htmlNormalizer.ts already strips inter-block layout whitespace before setContent, whereas passing preserveWhitespace when useHtmlNormalizer is false would need care not to turn pretty-printed \n between blocks from native <html>\n<p>...</p>\n</html> into extra text/paragraphs. At minimum, <codeblock> and setContent handling should preserve indentation and user-entered spaces consistently with <EnrichedText> .et-view which uses white-space: pre-wrap.)

Environment
  • react-native-enriched-html: 1.2.0
  • Platform: Web (Chrome, TipTap 3.31.3)
主要语言
C
星标
1.4k
派生
80
平均合并
4 天 19 小时
30 天内合并 PR
21

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

software-mansion/react-native-enriched-html 的其他 Issue

查看 software-mansion/react-native-enriched-html 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。