Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/keep-no-break-spaces.md
Original file line number Diff line number Diff line change
@@ -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).
39 changes: 39 additions & 0 deletions src/api/non-xml-whitespace-round-trip.test.ts
Original file line number Diff line number Diff line change
@@ -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(
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>` +
`<w:document xmlns:w="${W_NS}"><w:body>${bodyXml}</w:body></w:document>`,
);
return zipSync(files);
}

describe("non-XML white space in <w:t> (#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(
`<w:p><w:r><w:t>Nom</w:t></w:r><w:r><w:t>${ch}</w:t></w:r><w:r><w:t>: Dupont</w:t></w:r></w:p>`,
),
);
expect(paragraphText(paragraphs(doc)[0]!)).toBe(`Nom${ch}: Dupont`);

const saved = strFromU8(unzipSync(toUint8Array(doc))[DOCUMENT_PART]!);
expect(saved).toContain(`<w:t>${ch}</w:t>`);

const reopened = openDocx(toUint8Array(doc));
expect(paragraphText(paragraphs(reopened)[0]!)).toBe(`Nom${ch}: Dupont`);
});
}
});
10 changes: 8 additions & 2 deletions src/internal/wordprocessingml/parser.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down
14 changes: 14 additions & 0 deletions src/internal/wordprocessingml/round-trip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ describe("WML round-trip", () => {
expect(out).toContain("<w:t>X</w:t>");
});

it("keeps a no-break space that sits between the runs of a revision", () => {
const xml = [
'<?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:ins w:id="1" w:author="A">',
"<w:r><w:t>a</w:t></w:r> <w:r><w:t>b</w:t></w:r>",
"</w:ins>",
"</w:p></w:body>",
"</w:document>",
].join("");
expect(fullCycle(xml)).toContain("</w:r> <w:r>");
});

it("returns AST-equivalent output (re-parse equals first parse)", () => {
const xml = [
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>',
Expand Down
2 changes: 1 addition & 1 deletion src/internal/xml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions src/internal/xml/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<w:t xmlns:w="urn:test">${ch}</w:t>`);
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("<root>\n <a/>  \n <b/>\n</root>");
expect(doc.root.children).toContainEqual({ kind: "text", value: "  \n " });
});

it("keeps interior whitespace when mixed with text", () => {
const doc = parseXml("<root>hello <b/> world</root>");
const kinds = doc.root.children.map((c) => c.kind);
Expand Down
14 changes: 13 additions & 1 deletion src/internal/xml/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 });
}
}
Expand Down
Loading