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

Đang mở Phù hợp với người mới
#353 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 5 ngày

@johnhldiver-beep đang làm issue này rồi.

Từ ngày 30/10/2026.

  • #361 của @johnhldiver-beep — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
html, javascript
Lĩnh vực
backend, web-dev

Hướng nghiên cứu

Bắt đầu với trình xử lý ontext trong esm/shared/parse-from-string.js, nơi issue xác định là thêm một nút Text cho mỗi callback của parser. So sánh hành vi của nó với DomHandler của htmlparser2, sau đó thêm một kiểm thử hồi quy cho markup có chứa tham chiếu ký tự. Hoàn tất khi phân tích ví dụ tạo ra một nút Text có dữ liệu "a & b" và độ bao phủ hiện có vẫn ở mức 100%.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
HTML
Star
2.1k
Fork
117
Merge trung bình
4 ngày 13 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của WebReflection/linkedom

Tất cả issue của WebReflection/linkedom

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.