Repository navigation
fix: no-break spaces disappeared when a document was opened and saved (#35) - #36
Merged
Merged
Conversation
The parser dropped text nodes between elements when `String.prototype.trim` found them empty. `trim` also strips U+00A0, U+202F, U+3000 and other characters that XML treats as ordinary character data, so a run holding only a (narrow) no-break space lost its text. Only XML white space (S) is now treated as formatting, here and between the runs of w:ins / w:del. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VtFrJBHR6yvdPisu3bM8dP
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
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.
Summary
Opening and saving a document dropped a no-break space (U+00A0), narrow no-break space (U+202F), ideographic space (U+3000) or any other non-XML white space that made up a whole text node, e.g. a
<w:t>holding only the U+202F Word inserts before:in French text.Nom : Dupontcame back asNom: Dupont, even in paragraphs that were never edited.Motivation
parseXmldropped text between elements whenvalue.trim()was empty.String.prototype.trimstrips ECMAScript white space, which includes U+00A0, U+202F, U+2000–U+200A, U+3000, U+FEFF …; XML white space (production [3]S) is only space, tab, CR and LF. The sametrim()test decided which text inside<w:ins>/<w:del>was formatting between runs, so a no-break space there was silently dropped too.Closes #35
Changes
parseXmlkeeps every text node that contains anything other than XML white space (new internal helperisXmlWhitespace, used insrc/internal/xml/parser.ts).parseRevisionRuns(src/internal/wordprocessingml/parser.ts) uses the same test, so a<w:ins>/<w:del>holding a stray no-break space between runs is kept verbatim as a raw inline instead of losing that character.<w:body>is schema-invalid but was already kept when it held visible text; it is now kept the same way when it holds only a no-break space. As before for visible text, that makes the trailing whitespace node the body's last child, so a following<w:sectPr>round-trips as a raw block rather than as the section properties. The saved body still holds the same elements and characters in the same order (checked with<w:body>\n<w:p/>\u00a0\n<w:sectPr>…</w:sectPr>\n</w:body>); only the parsed model differs for such (invalid) input./^\s|\s$/checks that decide whether to addxml:space="preserve".\sis a superset of XMLS, so at worst they add an unneededpreserveto NBSP-edged text, which never loses data.Testing
src/internal/xml/parser.test.ts: U+00A0, U+202F, U+3000, U+2009, U+FEFF alone in an element are kept; an NBSP mixed with XML white space between elements is kept.src/internal/wordprocessingml/round-trip.test.ts: an NBSP between the runs of a<w:ins>survives parse → write.src/api/non-xml-whitespace-round-trip.test.ts: the issue's reproduction through the public API —openDocx→paragraphText→toUint8Array→ reopen, for U+00A0, U+202F and U+3000.mainfor the reported symptom and passes with the fix. The full gate passes locally:pnpm format:check && pnpm lint && pnpm typecheck && pnpm test && pnpm build:packages && pnpm check:tree-shake(1311 passed, 5 skipped).pnpm exec vitest run src/internal/xml/parser.test.ts src/internal/wordprocessingml/round-trip.test.ts src/api/non-xml-whitespace-round-trip.test.tsBreaking changes
None
Checklist
flagged it above.
comments.
PR represents real work that warrants a maintainer's review, and I am
willing to defend each line in review.
🤖 Generated with Claude Code
https://claude.ai/code/session_01VtFrJBHR6yvdPisu3bM8dP