diff --git a/lib/docx/elements/text.rb b/lib/docx/elements/text.rb
index fb89401..dcac793 100644
--- a/lib/docx/elements/text.rb
+++ b/lib/docx/elements/text.rb
@@ -13,6 +13,8 @@ def content
def content=(args)
@node.content = args
+ # Without xml:space="preserve", Word drops leading and trailing spaces.
+ @node['xml:space'] = 'preserve' if args.to_s.match?(/\A\s|\s\z/)
end
def initialize(node)
diff --git a/spec/docx/elements/text_spec.rb b/spec/docx/elements/text_spec.rb
index c15e979..3f9fbc2 100644
--- a/spec/docx/elements/text_spec.rb
+++ b/spec/docx/elements/text_spec.rb
@@ -23,5 +23,33 @@
expect(node.content).to eq('world')
end
+
+ # Regression tests for #187: without xml:space="preserve", Word drops
+ # leading and trailing spaces of a w:t.
+ it 'marks the node xml:space="preserve" when the content has a trailing space' do
+ text.content = 'Jane '
+
+ expect(node.to_xml).to eq('Jane ')
+ end
+
+ it 'marks the node xml:space="preserve" when the content has a leading space' do
+ text.content = ' end'
+
+ expect(node.to_xml).to eq(' end')
+ end
+
+ it 'does not add xml:space when the content has no surrounding whitespace' do
+ text.content = 'Jane'
+
+ expect(node.to_xml).to eq('Jane')
+ end
+
+ it 'keeps an existing xml:space="preserve"' do
+ node = w_node(' a ')
+
+ described_class.new(node).content = 'b'
+
+ expect(node.to_xml).to eq('b')
+ end
end
end