Fix double-encoded entity references flattened during XML round-trip#663
Merged
Fix double-encoded entity references flattened during XML round-trip#663
Conversation
When XML contains double-encoded entities (e.g. &lt; to represent literal text <), the round-trip through model serialization flattened them by one level, producing semantically different or invalid XML. Root cause: add_text_with_entities treated ALL entity-like patterns in text content as EntityReference nodes, including standard XML entities (lt, gt, amp, apos, quot) and numeric character references. This caused &lt; to become < (i.e. <) after round-trip. Three fixes in add_text_with_entities: - Exclude standard XML entities from EntityReference creation; treat as text nodes so the serializer handles proper escaping - Use #match instead of #match? for the entity name check (match? does not populate in Ruby, silently breaking the exclusion check) - Tighten entity name regex to require letter-initial names per the XML spec, preventing invalid entity references like &1; from shell syntax Non-standard entities (copy, nbsp, mdash, etc.) continue to be preserved as EntityReference nodes as before. Fixes: rfc8792, rfc8846, rfc9052, rfc9095, rfc9108, rfc9338, rfc9683, rfc9700, rfc9788, rfc9953 round-trip failures fix: Oga adapter CDATA serialization in plan-based path The build_moxml_node method always created text nodes, ignoring the cdata flag on XmlElement. This caused 5 pre-existing OgaAdapter test failures in cdata_spec.rb on GHA. fix: Oga adapter CDATA in mixed content and xmlns deduplication - Check xml_element.cdata flag when creating String child nodes in build_moxml_node, creating CDATA sections instead of text nodes when cdata is true (matching Nokogiri adapter behavior) - Filter xmlns attributes in regular attribute iteration to prevent duplicate namespace declarations, matching Nokogiri's logic - Fixes pre-existing CI failures with Canon 0.2.x
189b3bc to
945dc34
Compare
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.
When XML contains double-encoded entities (e.g. < to represent
literal text <), the round-trip through model serialization flattened
them by one level, producing semantically different or invalid XML.
Root cause: add_text_with_entities treated ALL entity-like patterns in
text content as EntityReference nodes, including standard XML entities
(lt, gt, amp, apos, quot) and numeric character references. This caused
< to become < (i.e. <) after round-trip.
Three fixes in add_text_with_entities:
text nodes so the serializer handles proper escaping
not populate in Ruby, silently breaking the exclusion check)
spec, preventing invalid entity references like &1; from shell syntax
Non-standard entities (copy, nbsp, mdash, etc.) continue to be preserved
as EntityReference nodes as before.
Fixes: rfc8792, rfc8846, rfc9052, rfc9095, rfc9108, rfc9338, rfc9683,
rfc9700, rfc9788, rfc9953 round-trip failures
fix: Oga adapter CDATA serialization in plan-based path
The build_moxml_node method always created text nodes, ignoring the
cdata flag on XmlElement. This caused 5 pre-existing OgaAdapter test
failures in cdata_spec.rb on GHA.
fix: Oga adapter CDATA in mixed content and xmlns deduplication
build_moxml_node, creating CDATA sections instead of text nodes
when cdata is true (matching Nokogiri adapter behavior)
duplicate namespace declarations, matching Nokogiri's logic