Skip to content

fix: no-break spaces disappeared when a document was opened and saved (#35) - #36

Merged
baseballyama merged 1 commit into
mainfrom
fix/issue-35-non-xml-whitespace
Nov 9, 2026
Merged

baseballyama merged 1 commit into
mainfrom
fix/issue-35-non-xml-whitespace

Conversation

@baseballyama

@baseballyama baseballyama commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

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 : Dupont came back as Nom: Dupont, even in paragraphs that were never edited.

Motivation

parseXml dropped text between elements when value.trim() was empty. String.prototype.trim strips 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 same trim() 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

  • parseXml keeps every text node that contains anything other than XML white space (new internal helper isXmlWhitespace, used in src/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.
  • Side effect, deliberate: character data directly under <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.
  • Not changed: the writers' /^\s|\s$/ checks that decide whether to add xml:space="preserve". \s is a superset of XML S, so at worst they add an unneeded preserve to 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.
  • Each new test fails on main for 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.ts

Breaking changes

None

Checklist

  • I have read CLAUDE.md and followed the project's conventions.
  • I have added or updated tests for the change.
  • I have added or updated documentation where user-visible behavior changed.
  • If this is a breaking change, I have added a changeset / CHANGELOG entry and
    flagged it above.
  • I have re-read my own diff and removed dead code, debug prints, and stale
    comments.
  • If I used an LLM to draft this PR, I have verified each change myself, this
    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

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
Copilot AI balanced review requested due to automatic review settings November 9, 2026 09:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@baseballyama
baseballyama merged commit 5cfb5cf into main Nov 9, 2026
7 checks passed
@baseballyama
baseballyama deleted the fix/issue-35-non-xml-whitespace branch November 9, 2026 09:19
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.

bug: non-breaking spaces disappear when a document is opened and saved

2 participants