From 50ee4e6d62f1f1a4c0da0168b0e48b3b7fc3a6ef Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Sun, 18 Oct 2026 01:05:20 +0900 Subject: [PATCH] Keep TextRun#text in sync after text= on a run without w:t (#190) When a run had no w:t, text= created one but did not add it to @text_nodes, so #text/#to_html kept returning "" and every further assignment created yet another w:t. Track the new node. Co-Authored-By: Claude Opus 5.5 --- lib/docx/containers/text_run.rb | 1 + spec/docx/containers/text_run_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/docx/containers/text_run.rb b/lib/docx/containers/text_run.rb index 18b83b1..1e3c58a 100755 --- a/lib/docx/containers/text_run.rb +++ b/lib/docx/containers/text_run.rb @@ -40,6 +40,7 @@ def text=(content) elsif @text_nodes.empty? new_t = Elements::Text.create_within(self) new_t.content = content + @text_nodes << new_t end reset_text end diff --git a/spec/docx/containers/text_run_spec.rb b/spec/docx/containers/text_run_spec.rb index 3131b68..8092c9d 100644 --- a/spec/docx/containers/text_run_spec.rb +++ b/spec/docx/containers/text_run_spec.rb @@ -48,6 +48,27 @@ def run(xml, document_properties = {}) expect(node.xpath('w:t').map(&:text)).to eq(['new']) end + + # Regression tests for #190 + it 'reflects the new text when the run had no w:t' do + text_run = described_class.new(w_node('')) + + text_run.text = 'new' + + expect(text_run.text).to eq('new') + expect(text_run.to_html).to eq('new') + end + + it 'keeps updating the same w:t on later assignments' do + node = w_node('') + text_run = described_class.new(node) + + text_run.text = 'first' + text_run.text = 'second' + + expect(text_run.text).to eq('second') + expect(node.xpath('w:t').map(&:text)).to eq(['second']) + end end describe '#substitute' do