Repository navigation
Make Paragraph#substitute work on w:t nodes (approach by @Andy9822 in #187) - #199
Merged
Merged
Conversation
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>
This was referenced Oct 17, 2026
Closed
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.
Bug (#187)
Paragraph#substitutejoined the paragraph'sTextRuns, wrote the merged result into the first run of the match, and emptied the other runs throughTextRun#text=. It never checked that those writes happened.TextRun#text=does nothing on a run with more than onew:t(#197). That kind of run is common:textutilwritetext<w:br/>textortext<w:tab/>textin one run.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:tnodes that#textreads (w:r/w:t,w:hyperlink/w:r/w:t, in document order):w:tholding the first matched character, so that run's formatting is used, as before.w:t. Text after the match stays in its own node and keeps its own formatting.w:br/w:tab/w:crinside the match are dropped. Those outside it stay where they are.Elements::Text#content=, so surrounding spaces getxml: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 letsubstitutematch text that#textdoesn't show.The public behavior otherwise stays the same: String or Regexp patterns,
String#subreplacement semantics, every occurrence replaced, no re-matching inside a replacement, and the method returnsself. The new constants areprivate_constant.Tests
Unit specs in
spec/docx/containers/paragraph_spec.rbbuilt from XML fragments:w:t/w:br/w:tab)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.docxintegration spec still pass.Thanks to @Andy9822 for the detailed report and for suggesting the
w:t-level approach.Closes #187
🤖 Generated with Claude Code