Skip to content

Stop substituting entities with an empty value - #370

Merged
kou merged 1 commit into
ruby:masterfrom
naitoh:stop-substituting-empty-entity
Sep 8, 2026
Merged

Stop substituting entities with an empty value#370
kou merged 1 commit into
ruby:masterfrom
naitoh:stop-substituting-empty-entity

Conversation

@naitoh

@naitoh naitoh commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

String#gsub("") matches at every position, so an entity declared with an empty value was inserted between every character when a text node or an attribute value was normalized.
That also kept the other entities from matching the text:

"abc aaa" -> "abc &a;"  # was "∅a∅b∅c∅ ..."

Substituting an empty entity is pointless -- a reference to it and its replacement text are equivalent -- so skipping it loses nothing.

`String#gsub("")` matches at every position, so an entity declared with
an empty value was inserted between every character when a text node or
an attribute value was normalized.  That also kept the other entities
from matching the text:

    "abc aaa" -> "abc &a;"  # was "∅a∅b∅c∅ ..."

Substituting an empty entity is pointless -- a reference to it and its
replacement text are equivalent -- so skipping it loses nothing.
Copilot AI lite review requested due to automatic review settings September 8, 2026 13:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The change is narrowly scoped, corrects a well-defined failure mode, and is covered by targeted regression tests for both text and attribute serialization.

Pull request overview

This PR fixes a normalization bug in REXML::Text.normalize where entities with an empty replacement value caused String#gsub("") to match at every position, resulting in entity references being inserted between every character and preventing other entities from matching.

Changes:

  • Skip doctype entities whose replacement value is nil or empty during normalization to avoid gsub("") behavior.
  • Add regression coverage for text-node serialization with an empty entity declared in the doctype.
  • Add regression coverage for attribute serialization with an empty entity declared in the doctype.
File summaries
File Description
lib/rexml/text.rb Skips entities with empty replacement values during normalization to prevent gsub("") inserting references everywhere.
test/test_text.rb Adds a regression test ensuring text normalization still replaces non-empty entities when an empty entity exists.
test/test_attribute.rb Adds a regression test ensuring attribute serialization still replaces non-empty entities when an empty entity exists.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@naitoh
naitoh requested a review from kou September 8, 2026 13:33
@kou
kou merged commit f3c7623 into ruby:master Sep 8, 2026
69 of 71 checks passed
@naitoh
naitoh deleted the stop-substituting-empty-entity branch September 8, 2026 21:01
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.

3 participants