Skip to content

Commit b758ca1

Browse files
committed
fix runs text not changed after update
1 parent bb78837 commit b758ca1

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

lib/docx/containers/text_run.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class TextRun
1212
bold: false,
1313
underline: false
1414
}
15-
15+
1616
def self.tag
1717
'r'
1818
end
1919

2020
attr_reader :text
2121
attr_reader :formatting
22-
22+
2323
def initialize(node, document_properties = {})
2424
@node = node
2525
@text_nodes = @node.xpath('w:t').map {|t_node| Elements::Text.new(t_node) }
@@ -40,6 +40,7 @@ def text=(content)
4040
new_t = Elements::Text.create_within(self)
4141
new_t.content = content
4242
end
43+
reset_text
4344
end
4445

4546
# Returns text contained within text run
@@ -52,6 +53,7 @@ def substitute(match, replacement)
5253
@text_nodes.each do |text_node|
5354
text_node.content = text_node.content.gsub(match, replacement)
5455
end
56+
reset_text
5557
end
5658

5759
def parse_formatting
@@ -74,7 +76,7 @@ def to_html
7476
styles = {}
7577
styles['text-decoration'] = 'underline' if underlined?
7678
# No need to be granular with font size down to the span level if it doesn't vary.
77-
styles['font-size'] = "#{font_size}pt" if font_size != @font_size
79+
styles['font-size'] = "#{font_size}pt" if font_size != @font_size
7880
html = html_tag(:span, content: html, styles: styles) unless styles.empty?
7981
html = html_tag(:a, content: html, attributes: {href: href, target: "_blank"}) if hyperlink?
8082
return html
@@ -83,11 +85,11 @@ def to_html
8385
def italicized?
8486
@formatting[:italic]
8587
end
86-
88+
8789
def bolded?
8890
@formatting[:bold]
8991
end
90-
92+
9193
def underlined?
9294
@formatting[:underline]
9395
end
@@ -102,12 +104,18 @@ def href
102104

103105
def hyperlink_id
104106
@node.attributes['id'].value
105-
end
107+
end
106108

107109
def font_size
108110
size_tag = @node.xpath('w:rPr//w:sz').first
109111
size_tag ? size_tag.attributes['val'].value.to_i / 2 : @font_size
110112
end
113+
114+
private
115+
116+
def reset_text
117+
@text = parse_text
118+
end
111119
end
112120
end
113121
end

spec/docx/document_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@
299299
expect(text_runs[3].font_size).to eq 11
300300
expect(text_runs[4].font_size).to eq 11
301301
end
302+
303+
it 'should return changed value for runs' do
304+
paragraph = @doc.paragraphs[10]
305+
text_runs = paragraph.text_runs
306+
307+
tr = text_runs[0]
308+
expect(tr.text).to eq 'This paragraph has a '
309+
310+
tr.text = 'This paragraph hasn\'t a'
311+
expect(tr.text).to eq 'This paragraph hasn\'t a'
312+
end
302313
end
303314

304315
describe 'saving' do

0 commit comments

Comments
 (0)