Skip to content

Preserve leading/trailing spaces written to w:t (reported by @Andy9822 in #187) - #198

Merged
satoryu merged 1 commit into
masterfrom
fix-187-preserve-space
Oct 17, 2026
Merged

satoryu merged 1 commit into
masterfrom
fix-187-preserve-space

Conversation

@satoryu

@satoryu satoryu commented Oct 17, 2026

Copy link
Copy Markdown
Member

Bug

Part of #187 (the "Janeend" row). Elements::Text#content= never set xml:space="preserve". When a write left a w:t with a leading or trailing space, for example substitute turning {{na into Jane , Word stripped the space on open and displayed Janeend. The gem keeps the space when reading, so paragraph.text looked correct, which made this hard to notice.

Fix

Text#content= now sets xml:space="preserve" whenever the new content starts or ends with whitespace. It doesn't add the attribute otherwise, and it leaves an existing one in place. I checked that after a serialize/reparse round trip the attribute sits in the XML namespace, not as a literal attribute name.

All writes go through this method (TextRun#text=, TextRun#substitute, Paragraph#substitute, bookmarks), so they all benefit.

Tests

New cases in spec/docx/elements/text_spec.rb: trailing space and leading space get the attribute, no surrounding whitespace leaves it off, and an existing attribute is kept.

Refs #187. The remaining rows of that issue are fixed in a follow-up PR that makes Paragraph#substitute work on w:t nodes.

🤖 Generated with Claude Code

Elements::Text#content= never set xml:space="preserve", so when a write
left a w:t starting or ending with a space (e.g. substitute producing
"Jane "), Word dropped the space and showed "Janeend". Set the attribute
whenever the new content has surrounding whitespace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@satoryu
satoryu merged commit 9413d1e into master Oct 17, 2026
6 checks passed
@satoryu
satoryu deleted the fix-187-preserve-space branch October 17, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant