Skip to content
TypeScript

fix(server): document text skips tracked deletions and field codes (#29796)

Alex Southwellb8fb45eb4cca
diff --git a/apps/server/src/provider/documentTextWorker.ts b/apps/server/src/provider/documentTextWorker.ts
index 569493906d3..9138d951d20 100644
--- a/apps/server/src/provider/documentTextWorker.ts
+++ b/apps/server/src/provider/documentTextWorker.ts
@@ -14,6 +14,20 @@ const CELL_END = Symbol("cell-end");
type TextPart = string | typeof CELL_START | typeof CELL_END | { fallback: string };
const TRUNCATED = "\n\n[Extracted text truncated at document extraction limits.]";
+// Parts the document never displays: tracked deletions and moves, field codes, phonetic guides,
+// paragraph properties such as tab-stop definitions.
+const HIDDEN = new Set([
+ "script",
+ "style",
+ "binary-data",
+ "rPh",
+ "delText",
+ "moveFrom",
+ "instrText",
+ "delInstrText",
+ "tracked-changes",
+ "pPr",
+]);
class Truncation extends Error {}
@@ -67,7 +81,7 @@ function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNod
}
}
function* text(node: XmlNode): Generator<string, void> {
- if (["script", "style", "binary-data", "rPh"].includes(node.name)) return;
+ if (HIDDEN.has(node.name)) return;
if (["tab", "cell"].includes(node.name)) yield "\t";
else if (["br", "line-break", "cr"].includes(node.name)) yield "\n";
else if (node.spaceCount !== undefined) {
@@ -80,7 +94,7 @@ function* text(node: XmlNode): Generator<string, void> {
}
}
function* paragraphs(node: XmlNode): Generator<TextPart, void> {
- if (["script", "style", "binary-data"].includes(node.name)) return;
+ if (HIDDEN.has(node.name)) return;
for (const child of node.children) {
if (typeof child === "string") yield child;
else if (["br", "line-break", "cr"].includes(child.name)) yield "\n";
@@ -137,11 +151,7 @@ function odfValue(cell: XmlNode): string {
function hasCellText(cell: XmlNode): boolean {
if (odfValue(cell).trim()) return true;
// Space markers can have enormous repeat counts; emptiness never requires expanding them.
- if (
- ["script", "style", "binary-data", "rPh"].includes(cell.name) ||
- cell.spaceCount !== undefined
- )
- return false;
+ if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false;
return cell.children.some((child) =>
typeof child === "string" ? !!child.trim() : hasCellText(child),
);
diff --git a/apps/server/src/provider/userInputAttachments.test.ts b/apps/server/src/provider/userInputAttachments.test.ts
index 81c167c038d..887a2d5febb 100644
--- a/apps/server/src/provider/userInputAttachments.test.ts
+++ b/apps/server/src/provider/userInputAttachments.test.ts
@@ -572,6 +572,26 @@ describe("question answer paths", () => {
},
expected: ["First\nSecond\nThird"],
},
+ {
+ ext: "docx",
+ bounded: true,
+ parts: {
+ "word/document.xml":
+ '<w:document xmlns:w="urn:w"><w:body><w:p><w:r><w:t>Intro</w:t></w:r><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> PAGEREF _Toc1 \\h </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r><w:t> 3</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p><w:pPr><w:tabs><w:tab w:val="right" w:pos="9350"/></w:tabs></w:pPr><w:r><w:t xml:space="preserve">Fee </w:t></w:r><w:del><w:r><w:delText>$500</w:delText></w:r></w:del><w:ins><w:r><w:t>$700</w:t></w:r></w:ins><w:moveFrom><w:r><w:t>Moved</w:t></w:r></w:moveFrom><w:r><w:tab/><w:t>due</w:t></w:r></w:p></w:body></w:document>',
+ },
+ expected: ["Intro 3\nFee $700\tdue"],
+ absent: ["PAGEREF", "$500", "Moved"],
+ },
+ {
+ ext: "odt",
+ bounded: true,
+ parts: {
+ "content.xml":
+ "<document><text><tracked-changes><changed-region><deletion><change-info><creator>Reviewer</creator></change-info><p>$500</p></deletion></changed-region></tracked-changes><p>Fee <change/>$700</p></text></document>",
+ },
+ expected: ["Fee $700"],
+ absent: ["Reviewer", "$500"],
+ },
{
ext: "docx",
bounded: true,