Skip to content

Apply the WHATWG numeric character reference replacement rules - #289

Open
whoalin1 wants to merge 1 commit into
Masterminds:masterfrom
whoalin1:fix/numeric-character-reference-replacement
Open

whoalin1 wants to merge 1 commit into
Masterminds:masterfrom
whoalin1:fix/numeric-character-reference-replacement

Conversation

@whoalin1

@whoalin1 whoalin1 commented Dec 2, 2026

Copy link
Copy Markdown

Fixes the numeric reference replacement part of #286 (€ should give €). It is independent of #288 (named entity table). The semicolon-less reference and tag-name parts of #286 are not addressed.

Root cause: CharacterReference::lookupDecimal() passed the raw number to mb_decode_numericentity() with the mask [0x0, 0x2FFFF, 0, 0xFFFF]. It never applied the numeric character reference end state rules, and the mask range cut off valid code points. Behaviour on master with loadHTML('<body>…</body>'):

input before spec
&#128;, &#x80; U+0080 U+20AC €
&#x9F; U+009F U+0178 Ÿ
&#0; removed U+FFFD
&#xD800; ? U+FFFD
&#x30000; literal text &#196608; U+30000
&#x110000; literal text &#1114112; U+FFFD
&#xFFFFFFFFFFFFFFFFFFFF; \x01.2089258196146E+24; U+FFFD

Fix: in lookupDecimal():

  • 0, surrogates, values above U+10FFFF, and overflowed input (hexdec() returns a float) map to U+FFFD.
  • The 27 C1 code points in the spec table get their replacements. The values were taken from Python's html._invalid_charrefs, which implements the same table.
  • The mask now covers the whole Unicode range (0x10FFFF, mask 0x1FFFFF).

Other code points, including the rest of the C1 range (U+0081, U+008D, …) and noncharacters, are unchanged. The spec only flags those as parse errors.

Validation (PHP 8.5, composer:2 Docker image):

  • vendor/bin/phpunit: 176 tests OK. The new CharacterReferenceTest::testLookupNumericAppliesReplacementRules fails on master and passes with this change.
  • Running the loadHTML() inputs from the table above now gives the spec column for every row. &#x1F600; and &#x81; are unchanged.
  • I did not run php-cs-fixer 2.19 (the CS workflow downloads the phar). The new code follows the file's existing style.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant