Skip to content

Fix NBSP-only text being dropped and entities being decoded twice - #233

Merged
leonelsanchesdasilva merged 1 commit into
DesignLiquido:mainfrom
nachocases:fix/xml-whitespace-and-entity-double-decode
Nov 8, 2026
Merged

leonelsanchesdasilva merged 1 commit into
DesignLiquido:mainfrom
nachocases:fix/xml-whitespace-and-entity-double-decode

Conversation

@nachocases

Copy link
Copy Markdown
Contributor

Summary

Two bugs make the output of a transform differ from a spec-compliant processor such as Saxon:

  1. Text containing only non-breaking spaces (U+00A0) is dropped from the output.
  2. %, A, < and > are decoded twice.

These matter for Mexico's SAT CFDI invoices. Their "cadena original" is produced by XSLT and verified byte for byte against a digital signature, so either bug makes a valid invoice fail verification. This follows DesignLiquido/xpath#2, which fixed normalize-space() for NBSP.

Input Output before Expected and output now
<r a="&#160;"/> with value-of select="r/@a" empty U+00A0
<t>17&amp;#37;</t> 17% 17&#37;
<t>&amp;lt;x&amp;gt;</t> <x> &lt;x&gt;

Root causes

NBSP-only text. xmlTransformedTextRecursive and xmlProducesNoOutput skip blank text nodes using nodeValue.trim() !== ''. JavaScript's trim() strips U+00A0, but XML whitespace is only #x9, #xA, #xD and #x20.

Double decoding has two separate causes:

  • htmlEntityDecode runs separate passes for named, decimal and hex references. &amp;#37; becomes &#37; in the first pass and % in the next.
  • xmlTransformedTextRecursive calls xmlUnescapeText on unescaped output, converting a literal &lt; that the parser had already decoded from &amp;lt;.

Changes

  • src/dom/xml-functions.ts: add isXmlWhitespaceOnly and use it for both blank-text checks.
  • src/dom/html-entity-decoder.ts: decode all references in one regex pass, so output is never re-read as input.
  • src/dom/xml-functions.ts: stop unescaping text nodes on output, and remove xmlUnescapeText, which is no longer used.

Whitespace-only text is still skipped, and real references such as &#37;, &lt; and &amp; still decode normally.

Behaviour change to review: xmlUnescapeText was exported from src/dom/xml-functions.ts. It has no other callers in src or tests, but tell me if you consider it public API and I'll keep it. Any caller relying on the extra decode will now get the correct literal text.

Testing

  • Added tests/entities-and-nbsp-output.test.tsx. 8 of its 15 cases fail without the source changes; the other 7 guard existing behaviour.
  • The full suite passes: 696 tests, up from 681.
  • I compared this build against 5.1.2 with our application-level workarounds on 88 real CFDI 3.3/4.0 documents. Both the invoice cadena and the TFD cadena were byte-identical on every document, using the official SAT stylesheets.

Known related issue, not changed here

xmlEscapeText collapses &amp;amp; back to &amp;, so a literal &amp; value isn't re-escaped correctly on XML output. It looked like a separate change, so I left it out; happy to follow up.


Drafted with Claude's assistance (investigation, patch and tests); verified as described above.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@leonelsanchesdasilva leonelsanchesdasilva left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@leonelsanchesdasilva
leonelsanchesdasilva merged commit 61a0309 into DesignLiquido:main Nov 8, 2026
1 check failed
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.

2 participants