Skip to content

Commit e4039d2

Browse files
saphidclaude
andcommitted
fix(server): keep Word fallback text when no choice has any
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 18d9b24 commit e4039d2

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎apps/server/src/provider/documentTextWorker.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
4848
});
4949
parser.on("closetag", () => {
5050
// Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
51-
if (stack.pop()!.name === "Fallback") stack.at(-1)!.children.pop();
51+
// Keep one when no Choice carries text, such as a chart with a VML caption.
52+
if (stack.pop()!.name !== "Fallback") return;
53+
const parent = stack.at(-1)!;
54+
if (children(parent).some((child) => child.name === "Choice" && hasText(child))) {
55+
parent.children.pop();
56+
}
5257
});
5358
const text = (value: string) => {
5459
stack.at(-1)!.children.push(value);
@@ -61,6 +66,8 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
6166

6267
const children = (node: XmlNode) =>
6368
node.children.filter((child): child is XmlNode => typeof child !== "string");
69+
const hasText = (node: XmlNode): boolean =>
70+
node.children.some((child) => (typeof child === "string" ? child.trim() !== "" : hasText(child)));
6471
function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNode, void> {
6572
for (const child of children(node)) {
6673
if (typeof name === "string" ? child.name === name : name.includes(child.name)) yield child;

‎apps/server/src/provider/userInputAttachments.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,15 @@ describe("question answer paths", () => {
564564
expected: ["Body\nCallout box"],
565565
occurrences: { text: "Callout box", count: 1 },
566566
},
567+
{
568+
ext: "docx",
569+
bounded: true,
570+
parts: {
571+
"word/document.xml":
572+
'<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body><w:p><w:r><mc:AlternateContent><mc:Choice Requires="cx"><w:drawing><cx:chart/></w:drawing></mc:Choice><mc:Fallback><w:pict><v:shape><v:textbox><w:txbxContent><w:p><w:r><w:t>Chart caption</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></mc:Fallback></mc:AlternateContent></w:r></w:p></w:body></w:document>',
573+
},
574+
expected: ["Chart caption"],
575+
},
567576
{
568577
ext: "docx",
569578
bounded: true,

0 commit comments

Comments
 (0)