The best way to control Claude Code, Codex and more from any computer or phone
fix(server): preserve positional tabs in DOCX attachment text (#38393)
Co-authored-by: Simone <185146821+Lucenx9@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Simone committed
Nov 18, 2026 at 03:03 UTC
08f324988ef17b04aa51e27f456678a7a2555b4d
2 files changed
+36
-10
apps/server/src/provider/documentTextWorker.ts
+25
-7
@@ -112,12 +112,7 @@ function xml(source: string, extension: string): XmlNode {
112
parent.children.pop();
113
return;
114
}
115
- // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
116
- // Keep one when no Choice carries text, such as a chart with a VML caption.
117
- if (node.name === "Fallback" && parent.hasTextChoice) {
118
- parent.children.pop();
119
- return;
120
- }
115
+ if (node.name === "Fallback" && !retainCompatibilityFallback(parent, node)) return;
116
if (HIDDEN.has(node.name) || (!node.hasText && node.name !== "noBreakHyphen")) return;
117
parent.hasText = true;
118
if (node.name === "Choice") parent.hasTextChoice = true;
@@ -135,6 +130,29 @@ function xml(source: string, extension: string): XmlNode {
130
131
const children = (node: XmlNode | undefined) =>
132
node?.children.filter((child): child is XmlNode => typeof child !== "string") ?? [];
133
+
134
+function retainCompatibilityFallback(parent: XmlNode, fallback: XmlNode): boolean {
135
+ // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
136
+ // Keep one when no Choice carries text, such as a chart with a VML caption.
137
+ if (parent.hasTextChoice) {
138
+ parent.children.pop();
139
+ return false;
140
+ }
141
+ if (fallback.hasText || hasInlineTab(fallback, ["tab", "ptab"])) {
142
+ parent.children = parent.children.filter(
143
+ (child) =>
144
+ typeof child === "string" || child.name !== "Choice" || !hasInlineTab(child, ["ptab"]),
145
+ );
146
+ }
147
+ return true;
148
+}
149
+
150
+function hasInlineTab(node: XmlNode, names: readonly string[]): boolean {
151
+ return (
152
+ !HIDDEN.has(node.name) &&
153
+ (names.includes(node.name) || children(node).some((child) => hasInlineTab(child, names)))
154
+ );
155
+}
156
function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNode, void> {
157
for (const child of children(node)) {
158
if (typeof name === "string" ? child.name === name : name.includes(child.name)) yield child;
@@ -143,7 +161,7 @@ function* all(node: XmlNode, name: string | readonly string[]): Generator<XmlNod
161
}
162
function* text(node: XmlNode): Generator<string, void> {
163
if (HIDDEN.has(node.name)) return;
146
- if (["tab", "cell"].includes(node.name)) yield "\t";
164
+ if (["tab", "ptab", "cell"].includes(node.name)) yield "\t";
165
else if (["br", "line-break", "cr"].includes(node.name)) yield "\n";
166
else if (node.name === "noBreakHyphen") yield "\u2011";
167
else if (node.spaceCount !== undefined) {
apps/server/src/provider/userInputAttachments.test.ts
+11
-3
@@ -749,10 +749,18 @@ describe("question answer paths", () => {
749
ext: "docx",
750
bounded: true,
751
parts: {
752
- "word/document.xml":
753
- '<w:document xmlns:w="urn:w"><w:body><w:p><w:r><w:t>First</w:t><w:cr/><w:t>Second</w:t><w:br/><w:t>Third</w:t><w:noBreakHyphen/><w:t>party</w:t><mc:AlternateContent xmlns:mc="urn:mc"><mc:Choice Requires="w"><w:noBreakHyphen/></mc:Choice><mc:Fallback><w:t>‑</w:t></mc:Fallback></mc:AlternateContent><w:t>End</w:t></w:r></w:p></w:body></w:document>',
752
+ "word/document.xml": [
753
+ '<w:document xmlns:w="urn:w" xmlns:mc="urn:mc"><w:body>',
754
+ '<w:p><w:r><w:t>First</w:t><w:cr/><w:t>Second</w:t><w:br/><w:t>Third</w:t><w:noBreakHyphen/><w:t>party</w:t><mc:AlternateContent><mc:Choice Requires="w"><w:noBreakHyphen/></mc:Choice><mc:Fallback><w:t>‑</w:t></mc:Fallback></mc:AlternateContent><w:t>End</w:t></w:r></w:p>',
755
+ '<w:p><w:r><w:t>Invoice 104</w:t><w:ptab w:alignment="right" w:relativeTo="margin" w:leader="none"/><w:t>$700.00</w:t></w:r></w:p>',
756
+ '<w:p><w:r><w:t>Invoice 105</w:t><mc:AlternateContent><mc:Choice Requires="w"><w:ptab w:alignment="right" w:relativeTo="margin" w:leader="none"/></mc:Choice><mc:Fallback><w:tab/></mc:Fallback></mc:AlternateContent><w:t>$800.00</w:t></w:r></w:p>',
757
+ '<w:p><w:r><w:t>Invoice 106</w:t><mc:AlternateContent><mc:Choice Requires="w"><w:ptab w:alignment="right" w:relativeTo="margin" w:leader="none"/></mc:Choice><mc:Fallback><w:t xml:space="preserve"> total </w:t></mc:Fallback></mc:AlternateContent><w:t>$900.00</w:t></w:r></w:p>',
758
+ "</w:body></w:document>",
759
+ ].join(""),
760
},
755
- expected: ["First\nSecond\nThird\u2011party\u2011End"],
761
+ expected: [
762
+ "First\nSecond\nThird\u2011party\u2011End\nInvoice 104\t$700.00\nInvoice 105\t$800.00\nInvoice 106 total $900.00",
763
+ ],
764
},
765
{
766
ext: "docx",