diff --git a/.changeset/keep-no-break-spaces.md b/.changeset/keep-no-break-spaces.md
new file mode 100644
index 0000000..14ded52
--- /dev/null
+++ b/.changeset/keep-no-break-spaces.md
@@ -0,0 +1,5 @@
+---
+"@office-kit/docx": patch
+---
+
+Opening and saving a document no longer drops a no-break space (U+00A0), narrow no-break space (U+202F) or ideographic space (U+3000) that sits alone in its run. French text such as `Nom : Dupont` or `« Oui »` came back as `Nom: Dupont` and `«Oui»`, even in paragraphs that were never edited. Only XML white space (space, tab, carriage return, line feed) between elements is now treated as formatting (#35).
diff --git a/src/api/non-xml-whitespace-round-trip.test.ts b/src/api/non-xml-whitespace-round-trip.test.ts
new file mode 100644
index 0000000..b7d615d
--- /dev/null
+++ b/src/api/non-xml-whitespace-round-trip.test.ts
@@ -0,0 +1,39 @@
+import { strFromU8, strToU8, unzipSync, zipSync } from "fflate";
+import { describe, expect, it } from "vitest";
+import { createDocx, openDocx, paragraphs, paragraphText, toUint8Array } from "./index.js";
+
+const DOCUMENT_PART = "word/document.xml";
+const W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
+
+// French typography puts a (narrow) no-break space alone in its own run before
+// `:` and inside « », written by Word without xml:space="preserve". Such a run
+// must survive open → save untouched (#35).
+function withBody(bodyXml: string): Uint8Array {
+ const files = unzipSync(toUint8Array(createDocx()));
+ files[DOCUMENT_PART] = strToU8(
+ `` +
+ `${bodyXml}`,
+ );
+ return zipSync(files);
+}
+
+describe("non-XML white space in (#35)", () => {
+ for (const ch of [" ", " ", " "]) {
+ const label = `U+${ch.charCodeAt(0).toString(16).toUpperCase().padStart(4, "0")}`;
+
+ it(`keeps a run holding only ${label} on read and on save`, () => {
+ const doc = openDocx(
+ withBody(
+ `Nom${ch}: Dupont`,
+ ),
+ );
+ expect(paragraphText(paragraphs(doc)[0]!)).toBe(`Nom${ch}: Dupont`);
+
+ const saved = strFromU8(unzipSync(toUint8Array(doc))[DOCUMENT_PART]!);
+ expect(saved).toContain(`${ch}`);
+
+ const reopened = openDocx(toUint8Array(doc));
+ expect(paragraphText(paragraphs(reopened)[0]!)).toBe(`Nom${ch}: Dupont`);
+ });
+ }
+});
diff --git a/src/internal/wordprocessingml/parser.ts b/src/internal/wordprocessingml/parser.ts
index 097bee1..6a8913e 100644
--- a/src/internal/wordprocessingml/parser.ts
+++ b/src/internal/wordprocessingml/parser.ts
@@ -1,4 +1,10 @@
-import type { XmlAttr, XmlDocument, XmlElement, XmlNode } from "../xml/index.js";
+import {
+ isXmlWhitespace,
+ type XmlAttr,
+ type XmlDocument,
+ type XmlElement,
+ type XmlNode,
+} from "../xml/index.js";
import { WML_NS } from "./namespaces.js";
import type {
PassThrough,
@@ -132,7 +138,7 @@ function parseRevisionRuns(el: XmlElement): WmlRun[] | undefined {
if (!isWmlElement(el, "ins") && !isWmlElement(el, "del")) return undefined;
const runs: WmlRun[] = [];
for (const child of el.children) {
- if (child.kind === "text" && child.value.trim() === "") continue;
+ if (child.kind === "text" && isXmlWhitespace(child.value)) continue;
if (child.kind !== "element" || !isWmlElement(child, "r")) return undefined;
runs.push(parseRun(child));
}
diff --git a/src/internal/wordprocessingml/round-trip.test.ts b/src/internal/wordprocessingml/round-trip.test.ts
index 5f58beb..f067272 100644
--- a/src/internal/wordprocessingml/round-trip.test.ts
+++ b/src/internal/wordprocessingml/round-trip.test.ts
@@ -114,6 +114,20 @@ describe("WML round-trip", () => {
expect(out).toContain("X");
});
+ it("keeps a no-break space that sits between the runs of a revision", () => {
+ const xml = [
+ '',
+ '',
+ "",
+ '',
+ "a b",
+ "",
+ "",
+ "",
+ ].join("");
+ expect(fullCycle(xml)).toContain(" ");
+ });
+
it("returns AST-equivalent output (re-parse equals first parse)", () => {
const xml = [
'',
diff --git a/src/internal/xml/index.ts b/src/internal/xml/index.ts
index 82bb23f..865b3ad 100644
--- a/src/internal/xml/index.ts
+++ b/src/internal/xml/index.ts
@@ -5,7 +5,7 @@
*/
export { decodeEntities, encodeAttrValue, encodeText } from "./entities.js";
-export { type ParseOptions, parseXml, XmlParseError } from "./parser.js";
+export { isXmlWhitespace, type ParseOptions, parseXml, XmlParseError } from "./parser.js";
export { type SerializeOptions, serializeXml } from "./serializer.js";
export {
type QName,
diff --git a/src/internal/xml/parser.test.ts b/src/internal/xml/parser.test.ts
index 9df2871..ff29d14 100644
--- a/src/internal/xml/parser.test.ts
+++ b/src/internal/xml/parser.test.ts
@@ -60,6 +60,21 @@ describe("parseXml", () => {
expect(kinds).toEqual(["element", "element"]);
});
+ // XML white space (production [3] `S`) is only U+0020, U+0009, U+000D and
+ // U+000A. `String.prototype.trim` also strips no-break and ideographic
+ // spaces, which are ordinary character data in XML.
+ it("keeps a text node made only of non-XML white space (#35)", () => {
+ for (const ch of [" ", " ", " ", " ", ""]) {
+ const doc = parseXml(`${ch}`);
+ expect(doc.root.children).toEqual([{ kind: "text", value: ch }]);
+ }
+ });
+
+ it("keeps a no-break space that mixes with XML white space between elements", () => {
+ const doc = parseXml("\n \n \n");
+ expect(doc.root.children).toContainEqual({ kind: "text", value: " \n " });
+ });
+
it("keeps interior whitespace when mixed with text", () => {
const doc = parseXml("hello world");
const kinds = doc.root.children.map((c) => c.kind);
diff --git a/src/internal/xml/parser.ts b/src/internal/xml/parser.ts
index 8df904a..6dd7b78 100644
--- a/src/internal/xml/parser.ts
+++ b/src/internal/xml/parser.ts
@@ -61,6 +61,18 @@ function cursorExpect(c: Cursor, expected: string): void {
c.index += expected.length;
}
+const XML_WHITESPACE_ONLY = /^[ \t\r\n]*$/;
+
+/**
+ * Whether `text` holds only XML white space (production [3] `S`: space, tab,
+ * carriage return, line feed). Not `String.prototype.trim`, which also strips
+ * no-break, narrow no-break and ideographic spaces — ordinary character data
+ * in XML that Word writes alone in a run (French `Nom\u202f:`).
+ */
+export function isXmlWhitespace(text: string): boolean {
+ return XML_WHITESPACE_ONLY.test(text);
+}
+
/** Skip XML whitespace (space, tab, newline, carriage return). */
function cursorSkipWhitespace(c: Cursor): void {
while (c.index < c.source.length) {
@@ -315,7 +327,7 @@ function parseElement(
const rawText = cursorTakeUntil(c, "<");
if (rawText.length === 0) continue;
const value = decodeEntities(rawText);
- if (xmlSpace === "preserve" || value.trim().length > 0 || hasNonElementSiblings(children)) {
+ if (xmlSpace === "preserve" || !isXmlWhitespace(value) || hasNonElementSiblings(children)) {
children.push({ kind: "text", value });
}
}