nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] decodeHtmlEntities corrupts numeric entities above U+FFFF and decodes escaped entities twice

Open inahus99 opened this issue 1 week ago • 2 comments

Description

decodeHtmlEntities, which runs on every assistant message, gives the wrong text in two cases:

  1. Numeric entities above U+FFFF are decoded to a different character. 😀 and 😀 (the grinning face emoji) come out as U+F600, a private-use character that shows as nothing or a box.
  2. An escaped entity is decoded twice. &amp;lt; is the literal text &lt;, but it is shown as <. Likewise &amp;#169; becomes the copyright sign instead of &#169;.

Environment

  • OS: Windows 11
  • Node version: v24.11.0
  • Nanocoder version: 1.32.0 (main at bfd8b8e7)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Ask any model to reply with exactly: done &#128512; and &amp;lt;
  2. Look at the rendered message.

The same can be seen by calling decodeHtmlEntities with that text directly.

Expected Behavior

done 😀 and &lt;

Actual Behavior

done followed by a blank or box glyph, then and <.

The second case matters when a model explains HTML escaping in prose ("write &amp;lt; to show &lt;"): the terminal shows a sentence that says something else.

Logs/Screenshots

decodeHtmlEntities('done &#128512;')      expected 'done 😀'              actual 'done '
decodeHtmlEntities('done &#x1F600;')      expected 'done 😀'              actual 'done '
decodeHtmlEntities('write &amp;lt; for <') expected 'write &lt; for <'    actual 'write < for <'
decodeHtmlEntities('&amp;#169;')          expected '&#169;'              actual '©'

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder

Cause, in source/markdown-parser/html-entities.ts:

  • Numeric entities use String.fromCharCode, which keeps only the low 16 bits of the code point.
  • Named entities are replaced in one pass per entity with &amp; first, and the numeric passes run after all of them, so text produced by one replacement is decoded again by the next.

Suggested fix: decode in a single pass over the text, and use String.fromCodePoint with a guard for values above U+10FFFF (it throws a RangeError for those, so they should be left as written). This is separate from #1591, which was about decoding inside fenced code blocks.

I have this working locally with specs for each case, and I would like to work on it if you can assign it to me.

inahus99 avatar Nov 22 '26 14:11 inahus99

I'd like to take this. Both cases come from how the function works now: named entities are replaced one by one, then numeric ones, so the &lt; that comes out of &amp;lt; gets decoded again; and String.fromCharCode truncates code points above U+FFFF. My plan is a single regex pass over named, decimal and hex entities, using String.fromCodePoint and leaving out-of-range or unknown entities as they are. Tests for both cases plus a changeset.

Kidkender avatar Nov 22 '26 15:11 Kidkender

Hi, I have already done the implementation in my local for this , just waiting for mod approval

inahus99 avatar Nov 22 '26 17:11 inahus99