Skip to content

Make Paragraph#substitute work on w:t nodes (approach by @Andy9822 in #187) - #199

Merged
satoryu merged 1 commit into
masterfrom
fix-187-substitute-text-nodes
Oct 17, 2026
Merged

satoryu merged 1 commit into
masterfrom
fix-187-substitute-text-nodes

Conversation

@satoryu

@satoryu satoryu commented Oct 17, 2026

Copy link
Copy Markdown
Member

Bug (#187)

Paragraph#substitute joined the paragraph's TextRuns, wrote the merged result into the first run of the match, and emptied the other runs through TextRun#text=. It never checked that those writes happened. TextRun#text= does nothing on a run with more than one w:t (#197). That kind of run is common:

  • LibreOffice and macOS textutil write text<w:br/>text or text<w:tab/>text in one run.
  • A hyperlink with several runs is read as a single TextRun.

Depending on which write was skipped, the placeholder stayed unfilled, the text after the match was duplicated (Name: JaneNext lineme}}Next line), stray characters were left behind (A Jane Bam), or the rest of the line was lost (See site {{na). Nothing raised.

There was a second problem even when every write succeeded: the text after the match moved into the first run, which put it before any line break in that range, and it lost its own run formatting.

Fix

Do the substitution on the w:t nodes that #text reads (w:r/w:t, w:hyperlink/w:r/w:t, in document order):

  • The replacement goes into the w:t holding the first matched character, so that run's formatting is used, as before.
  • The rest of the match is cut from the following w:t. Text after the match stays in its own node and keeps its own formatting.
  • w:br / w:tab / w:cr inside the match are dropped. Those outside it stay where they are.
  • All writes go through Elements::Text#content=, so surrounding spaces get xml:space="preserve" (Preserve leading/trailing spaces written to w:t (reported by @Andy9822 in #187) #198).

I deliberately did not use .//w:t, which would also pick up text from text boxes nested in a run (w:drawing//w:txbxContent), and would let substitute match text that #text doesn't show.

The public behavior otherwise stays the same: String or Regexp patterns, String#sub replacement semantics, every occurrence replaced, no re-matching inside a replacement, and the method returns self. The new constants are private_constant.

Tests

Unit specs in spec/docx/containers/paragraph_spec.rb built from XML fragments:

The reproduction script from the issue now prints the expected result for every row, including after save and reopen. The existing #147 specs and the split_placeholder.docx integration spec still pass.

Thanks to @Andy9822 for the detailed report and for suggesting the w:t-level approach.

Closes #187

🤖 Generated with Claude Code

substitute joined the paragraph's TextRuns, wrote the result into the
first run of the match and emptied the others through TextRun#text=,
without checking that the writes happened. TextRun#text= does nothing
on a run with several w:t (LibreOffice/textutil write "text<w:br/>text"
in one run, and a hyperlink's runs form one TextRun), so placeholders
were left unfilled, text after the match was duplicated, or the rest of
the line was lost. Even when the writes succeeded, text after the match
moved into the first run, past any line break.

Work on the w:t nodes that #text reads instead: put the replacement in
the w:t holding the first matched character, cut the rest of the match
from the following w:t, and drop line breaks and tabs inside the match.
Text, breaks and tabs outside the match stay where they are, and text
after the match keeps its own run formatting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@satoryu
satoryu merged commit 971e20e into master Oct 17, 2026
6 checks passed
@satoryu
satoryu deleted the fix-187-substitute-text-nodes branch October 17, 2026 16:40
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.

substitute leaves placeholders unfilled, or garbles the line, in files saved by LibreOffice

1 participant