Parsing splits one text run into several Text nodes at each character reference (`<p>a &amp; b</p>` gives 3 child nodes)

未关闭 适合新手
#353 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

@johnhldiver-beep 已经在做这个了。

开始于 2026年10月30日。

  • #361 来自 @johnhldiver-beep —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
html, javascript
领域
backend, web-dev

调研方向

从 esm/shared/parse-from-string.js 中的 ontext 处理程序开始,issue 指出它会为每次解析器回调追加一个 Text 节点。将其行为与 htmlparser2 的 DomHandler 进行比较,然后为包含字符引用的标记添加回归测试。完成标准是:解析示例后生成一个数据为 "a & b" 的 Text 节点,且现有覆盖率保持在 100%。

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

描述

Parsing <p>a &amp; b</p> gives the <p> three Text nodes ("a ", "&", " b"). The HTML parsing algorithm appends each character to the previous Text node, and jsdom gives one Text node, "a & b". Code that walks childNodes and treats each Text node as one run sees extra runs wherever the source has an entity. normalize() merges them afterwards, but every consumer of parsed output has to know to call it on every subtree, and a spec parser never produces the split in the first place ("insert a character": https://html.spec.whatwg.org/multipage/parsing.html#insert-a-character).

Asks:

  1. Should the parser merge text into the previous Text node when the previous sibling under the same parent is a Text node, as the HTML parsing algorithm does?
  2. If yes, I can open a PR that changes esm/ only, with a test, keeping coverage at 100%.

Environment: linkedom 0.18.14 (npm, latest) and main at e2ee208, htmlparser2 10.1.0, Node 24.14.1, macOS.

Test case:

import { DOMParser } from 'linkedom'

const doc = new DOMParser().parseFromString('<!doctype html><html><body><p>a &amp; b</p></body></html>', 'text/html')
const p = doc.querySelector('p')
console.log('textContent:', JSON.stringify(p.textContent))
console.log('childNodes.length:', p.childNodes.length)
console.log('childNodes data:', JSON.stringify([...p.childNodes].map((n) => n.data)))

Output:

textContent: "a & b"
childNodes.length: 3
childNodes data: ["a ","&"," b"]

Expected: childNodes.length: 1, data ["a & b"] (what jsdom 29.1.1 returns for the same markup).

Input linkedom Text nodes jsdom Text nodes
<p>a and b</p> 1 1
<p>a &amp; b</p> 3 1
<p>x &lt; y &gt; z &#169;</p> 6 1

parseHTML gives the same result as DOMParser (3 nodes for <p>a &amp; b</p>).

Raw output (npm 0.18.14, main e2ee208, variants, htmlparser2)
$ node repro.mjs   # linkedom 0.18.14 from npm
textContent: "a & b"
childNodes.length: 3
childNodes data: ["a ","&"," b"]
$ node repro-main.mjs   # main e2ee208 esm/
textContent: "a & b"
childNodes.length: 3
childNodes data: ["a ","&"," b"]
$ node variants.mjs
no entity:       1 ["a and b"]
&amp;:           3 ["a ","&"," b"]
&lt; &gt; &#169;: 6 ["x ","<"," y ",">"," z ","©"]
parseHTML:       3
$ node htmlparser2-ontext.mjs   # htmlparser2 10.1.0, decodeEntities: true
htmlparser2 ontext calls: ["a ","&"," b"]
$ node jsdom-control.mjs
jsdom childNodes.length: 1 ["a & b"]

Where it comes from: htmlparser2 with decodeEntities: true calls ontext once per piece around each character reference. The ontext handler in parseFromString appends a new Text node for every call:

https://github.com/WebReflection/linkedom/blob/e2ee20813e4b3ae946d7ba41621ddf3698431989/esm/shared/parse-from-string.js#L92-L98

htmlparser2's own DomHandler merges each ontext piece into the previous Text node (domhandler 5.0.3, lib/index.js, ontext), so with the same callbacks it yields one node for <p>a &amp; b</p>.

No existing issue found for this; the closest are #62 and #71, which are about serializing entities, not the node split.

主要语言
HTML
星标
2.1k
派生
117
平均合并
4 天 13 小时
30 天内合并 PR
2

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

WebReflection/linkedom 的其他 Issue

查看 WebReflection/linkedom 的全部 Issue

相似的 Issue

更多 Backend & API Design Issue

把新 issue 发到你的邮箱

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