Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/docx/elements/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions spec/docx/elements/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('<w:t xml:space="preserve">Jane </w:t>')
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('<w:t xml:space="preserve"> end</w:t>')
end

it 'does not add xml:space when the content has no surrounding whitespace' do
text.content = 'Jane'

expect(node.to_xml).to eq('<w:t>Jane</w:t>')
end

it 'keeps an existing xml:space="preserve"' do
node = w_node('<w:t xml:space="preserve"> a </w:t>')

described_class.new(node).content = 'b'

expect(node.to_xml).to eq('<w:t xml:space="preserve">b</w:t>')
end
end
end
Loading