Skip to content

fix: merge consecutive ontext pieces into one Text node - #361

Open
johnhldiver-beep wants to merge 1 commit into
WebReflection:mainfrom
johnhldiver-beep:fix/353-text-node-merge
Open

johnhldiver-beep wants to merge 1 commit into
WebReflection:mainfrom
johnhldiver-beep:fix/353-text-node-merge

Conversation

@johnhldiver-beep

Copy link
Copy Markdown

Fixes #353.

Parsing <p>a &amp; b</p> produced 3 Text nodes ("a ", "&", " b") instead of 1 ("a & b"), because htmlparser2 with decodeEntities: true calls ontext once per piece around each character reference and the handler appended a new Text node per call. The HTML parsing algorithm appends each character to the previous Text node ("insert a character"), and jsdom returns one node here.

Fix (in esm/shared/parse-from-string.js plus the identical logic in the committed cjs/ mirror): merge an ontext piece into the previous sibling when it is a Text node (prev[VALUE] += text), otherwise appending a new node as before. CDATA handling is untouched; nothing about serialization, escaping, or normalize() changed.

Tests: regression tests in test/html/document.js covering entity merge, multi-entity merge, plain text unchanged, and no merging across element or comment boundaries. New tests failed before the fix ("Expected: 1, Got instead: 3") and pass after; test/html/document.js and the full node test/index.js suite both exit 0 with no failures. Honest limits: this suite prints no numeric assertion totals, so counts are reported as exit codes + failure-grep counts; eslint/tsc/rollup were not run (devDeps deliberately skipped for disk).

Note: the issue reporter offered in the issue body ("If yes, I can open a PR") but has filed nothing as of this PR — no PR by number or concept covers this fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant