Repository navigation
Description
Summary
parseXml drops a text node whose content is only a no-break space (U+00A0) or a narrow no-break space (U+202F). A <w:t> holding just that character is therefore emptied when a document is opened, and the space is missing from the saved file, even in paragraphs the caller never touched.
French typography puts a no-break or narrow no-break space before :, ;, !, ? and inside « ». When the text language is French (France), Word inserts one as you type, or converts the ordinary space typed before the sign, as I checked in my own Word, and writers also type them by hand (Ctrl+Shift+Space in Word, AltGr+Space or AltGr+Shift+Space on the French AZERTY keyboard). A phrase like Nom : Dupont comes out as Nom: Dupont, as the reproduction below shows, and nothing reports the loss.
The same happens to any such space that sits alone in its run, for example:
Durée : 18 mois→Durée: 18 mois« Oui »→«Oui»10 000 €→10000€
I found it by checking that a document opened and saved with no edit comes back unchanged. Two French documents saved by Word did not:
word/document.xmllost 5 and 11 characters respectively;- every lost character was a no-break or narrow no-break space sitting alone in its own run, with no
xml:space="preserve"; - all the other parts came back byte-identical.
The cause is the test that drops white-space-only text between elements, in src/internal/xml/parser.ts. It keeps a text node only when value.trim().length > 0, or under xml:space="preserve", or in mixed content. String.prototype.trim treats U+00A0 and U+202F as white space; XML does not (details below).
Reproduction
import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
import { openDocx, paragraphs, paragraphText, toUint8Array } from "@office-kit/docx";
const show = (s) => s.replace(/[\u00a0\u202f]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
const xml = (t) =>
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p>` +
`<w:r><w:t>Nom</w:t></w:r><w:r><w:t>${t}</w:t></w:r><w:r><w:t>: Dupont</w:t></w:r></w:p></w:body></w:document>`;
for (const ch of ["\u202f", "\u00a0"]) {
const bytes = zipSync({
"[Content_Types].xml": strToU8(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>`),
"_rels/.rels": strToU8(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>`),
"word/document.xml": strToU8(xml(ch)),
});
const doc = openDocx(bytes);
console.log("read: ", show(paragraphText(paragraphs(doc)[0])));
const saved = strFromU8(unzipSync(toUint8Array(doc))["word/document.xml"]);
console.log("saved:", show(saved.match(/<w:t[^>]*>[^<]*<\/w:t>|<w:t\/>/g).join("")));
}Expected behavior
The character is kept, as for any other character data. Output with the condition changed as proposed below:
read: Nom\u202f: Dupont
saved: <w:t>Nom</w:t><w:t>\u202f</w:t><w:t>: Dupont</w:t>
read: Nom\u00a0: Dupont
saved: <w:t>Nom</w:t><w:t>\u00a0</w:t><w:t>: Dupont</w:t>
Actual behavior
The character is dropped on read, and the saved part holds an empty <w:t>:
read: Nom: Dupont
saved: <w:t>Nom</w:t><w:t></w:t><w:t>: Dupont</w:t>
read: Nom: Dupont
saved: <w:t>Nom</w:t><w:t></w:t><w:t>: Dupont</w:t>
Environment
- Package version: @office-kit/docx 0.2.0 (the same line is on
mainat a5fe1b8) - Runtime: Node 24.17.0 on Ubuntu 26.04 x86_64
- Other relevant details: none, the reproduction uses only the published package and fflate
Additional context
The condition is value.trim().length > 0 in src/internal/xml/parser.ts (line 318 on main). String.prototype.trim removes ECMAScript white space and line terminators: U+0009–U+000D, U+0020, U+00A0, U+1680, U+2000–U+200A, U+2028, U+2029, U+202F, U+205F, U+3000, U+FEFF. XML white space (S, production [3] of XML 1.0) is only U+0020, U+0009, U+000D and U+000A, the set cursorSkipWhitespace already uses. So U+3000 (ideographic space) is affected as well.
Testing only XML white space fixes it:
if (xmlSpace === "preserve" || /[^ \t\r\n]/.test(value) || hasNonElementSiblings(children)) {With this change applied to the 0.2.0 build, both documents round-trip byte-identical (all 40 and 32 parts). A regression test along these lines:
it("keeps a text node made only of non-XML white space", () => {
for (const ch of ["\u00a0", "\u202f", "\u3000"]) {
const doc = parseXml(`<w:t xmlns:w="urn:test">${ch}</w:t>`);
expect(doc.root.children).toEqual([{ kind: "text", value: ch }]);
}
});Confirmation
- I have searched existing issues and confirmed this is not a duplicate.
- I am on the latest released version, or I have explained above why an older version is relevant.
- I have included a minimal reproduction that I have actually run myself.
- If I used an LLM to draft this issue, I have read and verified every claim and am willing to defend them in follow-up.
Activity
fix: no-break spaces disappeared when a document was opened and saved (…