Repository navigation
Preserve leading/trailing spaces written to w:t (reported by @Andy9822 in #187) - #198
Merged
Merged
Conversation
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>
This was referenced Oct 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
Part of #187 (the "
Janeend" row).Elements::Text#content=never setxml:space="preserve". When a write left aw:twith a leading or trailing space, for examplesubstituteturning{{naintoJane, Word stripped the space on open and displayedJaneend. The gem keeps the space when reading, soparagraph.textlooked correct, which made this hard to notice.Fix
Text#content=now setsxml: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#substitutework onw:tnodes.🤖 Generated with Claude Code