Fix character reference consumption for consecutive & in text and correct numeric NCR edge handling#172
Closed
Copilot wants to merge 2 commits into
Closed
Fix character reference consumption for consecutive & in text and correct numeric NCR edge handling#172Copilot wants to merge 2 commits into
& in text and correct numeric NCR edge handling#172Copilot wants to merge 2 commits into
Conversation
Agent-Logs-Url: https://github.com/HtmlUnit/htmlunit-neko/sessions/7ff3bc0d-a2b2-4ffc-a2bf-019d40132ce6 Co-authored-by: rbri <[email protected]>
|
Copilot
AI
changed the title
[WIP] Fix HTMLScanner character reference consumption and handling
Fix character reference consumption for consecutive May 7, 2026
& in text and correct numeric NCR edge handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This updates HTML character reference parsing to preserve literal ampersands in text when a later named reference exists, and fixes two numeric character reference edge cases in unicode entity resolution.
HTMLScanner: preserve
&runs in data statescanEntityRef, added an early&&guard so an invalid&start consumes/emits exactly one literal&and leaves following input untouched.&&&>and allows only the final valid>to resolve.HTMLUnicodeEntitiesParser: surrogate consumption correctness
setMatchFromCode, surrogate handling now setsmatchLength_ = consumedCount_.�now fully consumes as�).HTMLUnicodeEntitiesParser: Windows-1252 mapping correction
0x98fromU+20DCtoU+02DCper HTML/Windows-1252 remap table.Regression coverage
FOO&&&>BAR→FOO&&&>BARFOO�ZOO→FOO�ZOOFOO˜ZOO→FOO˜ZOO0x98mapping.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
checkstyle.org/opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-a1ec6fe11304a4f3/dbs/java/working/files-to-index15099172076840092536.list --sourceArchiveDir=/tmp/codeql-scratch-a1ec6fe11304a4f3/dbs/java/src --outputDir=/tmp/codeql-scratch-a1ec6fe11304a4f3/dbs/java/trap/java(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fix HTMLScanner character reference consumption so that in text content (data state) consecutive ampersands are not lost when followed by a named character reference.
Repro:
Input text content:
FOO&&&>BARCurrent output produced by parser:
FOO&&>;BARExpected output per HTML spec behavior:
FOO&&&>BAR(only the final>becomes>; the preceding&remain literal).Also fix numeric character reference handling in HTMLUnicodeEntitiesParser:
FOO�ZOOcurrently yieldsFOO�xDFFF;ZOO, expectedFOO�ZOO. Root cause: in HTMLUnicodeEntitiesParser.setMatchFromCode(), surrogate branch sets match_ but not matchLength_. Set matchLength_ = consumedCount_.FOO˜ZOOshould map 0x98 -> U+02DC (SMALL TILDE). Current code maps to U+20DC. Change case 0x98 to "\u02DC".Implementation notes:
Acceptance criteria:
FOO&&&>BARresults in a single text node with value exactlyFOO&&&>BAR.FOO�ZOOyields textFOO�ZOO.FOO˜ZOOyields textFOO˜ZOO(U+02DC).The following is the prior conversation context from the user's chat exploration (may be truncated):
User: why the html parser generates this dom tree
|
|
|
|
|
|
|
|
from
Assistant: Now I have a thorough understanding of the code. Let me trace through what happens with
<b><b><a><p></a>step by step.Why
<b><b><a><p></a>produces that DOM treeThe behaviour is driven entirely by
HTMLTagBalancer.java, specifically two mechanisms:Key mechanism 1 — "Save and re-open formatting elements" when a block arrives (lines 868–962)
Actually
<p>is a block element. When a block element is encountered, the parser:fElementStackfor consecutive formatting elements (INLINE | FORMATTINGflag).<b>is defined asElement.INLINE | Element.FORMATTINGand<a>asElement.INLINE(but not FORMATTING). The scan stops at<a>because it is not a formatting element.Key mechanism 2 —
<p>closes<p>inelement.closes<p>has acloseslist that includesPitself, so an open<p>is implicitly closed by a new<p>.Step-by-step parse of
<b><b><a><p></a><b><html><head></head><body>, then push<b>html > body > bhtml, head, body, b<b><b>(noclosesrule stops it)html > body > b > bb<a><a>html > body > b > b > aa<p><a>:<a>is INLINE but not FORMATTING → scan stops. Only the directly adjacent<b>(the second one) is FORMATTING. Save it tofFormattingStack, emit synthetic</b>. Stack is nowb > a. Then<p>is emitted and pushed. Then the saved<b>is re-opened inside<p>.…> b > a > p > b</b>(synth), <p>, <b>(synth)</a><a>in element stack. Found at depth 3 (abovepandb). ButgetElementDepthstops at a block boundary —<p>is a block element, so the search breaks early → depth = -1 (not found). The</a>is discarded.This pull request was created from Copilot chat.