@devries-cameron20 / t3code Mirror · read-only / commits / 2854038b61
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(server): preserve DOCX non-breaking hyphens in attachment text (#36845)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 18:35 UTC 2854038b61a4260881fadbb2864e0847f22ab3e8
2 files changed +24 -14
apps/server/src/provider/documentTextWorker.ts
+12 -6
@@ -41,12 +41,12 @@ interface XmlNode {
41 hasTextChoice?: boolean;
42 }
43
44 -function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
44 +function xml(source: string, extension: string): XmlNode {
45 const root: XmlNode = { name: "root", attrs: {}, children: [] };
46 const stack = [root];
47 let nodes = 0;
48 const parser = new SaxesParser();
49 - if (htmlEntities) Object.assign(parser.ENTITIES, characterEntities);
49 + if (extension === ".epub") Object.assign(parser.ENTITIES, characterEntities);
50 parser.on("opentag", (tag) => {
51 if (++nodes > 200_000 || stack.length > 128) throw new Truncation("XML structure limit");
52 const node: XmlNode = {
@@ -59,20 +59,25 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode {
59 ),
60 children: [],
61 };
62 - if (odfSpaces && node.name === "s") node.spaceCount = repeatCount(node.attrs.c);
62 + if ([".odt", ".ods", ".odp"].includes(extension) && node.name === "s")
63 + node.spaceCount = repeatCount(node.attrs.c);
64 stack.at(-1)!.children.push(node);
65 stack.push(node);
66 });
67 parser.on("closetag", () => {
68 const node = stack.pop()!;
69 const parent = stack.at(-1)!;
70 + if (extension === ".docx" && node.name === "del") {
71 + parent.children.pop();
72 + return;
73 + }
74 // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes.
75 // Keep one when no Choice carries text, such as a chart with a VML caption.
76 if (node.name === "Fallback" && parent.hasTextChoice) {
77 parent.children.pop();
78 return;
79 }
75 - if (!node.hasText) return;
80 + if (!node.hasText && node.name !== "noBreakHyphen") return;
81 parent.hasText = true;
82 if (node.name === "Choice") parent.hasTextChoice = true;
83 });
@@ -99,6 +104,7 @@ function* text(node: XmlNode): Generator<string, void> {
104 if (HIDDEN.has(node.name)) return;
105 if (["tab", "cell"].includes(node.name)) yield "\t";
106 else if (["br", "line-break", "cr"].includes(node.name)) yield "\n";
107 + else if (node.name === "noBreakHyphen") yield "\u2011";
108 else if (node.spaceCount !== undefined) {
109 yield " ".repeat(Math.min(MAX_OUTPUT, node.spaceCount));
110 if (node.spaceCount > MAX_OUTPUT) throw new Truncation("Space repeat limit");
@@ -164,7 +170,7 @@ function odfValue(cell: XmlNode): string {
170 }
171 }
172 function hasCellText(cell: XmlNode): boolean {
167 - if (odfValue(cell).trim()) return true;
173 + if (odfValue(cell).trim() || cell.name === "noBreakHyphen") return true;
174 // Space markers can have enormous repeat counts; emptiness never requires expanding them.
175 if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false;
176 return cell.children.some((child) =>
@@ -441,7 +447,7 @@ async function* document(path: string, extension: string): AsyncGenerator<TextPa
447 try {
448 const data = files.get(name);
449 if (!data) throw new Error("Missing document part");
444 - return xml(data, [".odt", ".ods", ".odp"].includes(extension), extension === ".epub");
450 + return xml(data, extension);
451 } catch (error) {
452 if (error instanceof Truncation) throw error;
453 skip(name);
apps/server/src/provider/userInputAttachments.test.ts
+12 -8
@@ -695,28 +695,32 @@ describe("question answer paths", () => {
695 bounded: true,
696 parts: {
697 "word/document.xml":
698 - '<w:document xmlns:w="urn:w"><w:body><w:p><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Report &amp; totals</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>North</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>12</w:t></w:r></w:p><w:p><w:r><w:t>34</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:p><w:r><w:t>Before</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>Inner</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p><w:r><w:t>After</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>Neighbor</w:t></w:r></w:p></w:tc></w:tr></w:tbl></w:body></w:document>',
698 + '<w:document xmlns:w="urn:w"><w:body><w:p><w:pPr><w:pStyle w:val="Heading2"/></w:pPr><w:r><w:t>Report &amp; totals</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>North</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>12</w:t></w:r></w:p><w:p><w:r><w:t>34</w:t></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:p><w:r><w:t>Before</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>Inner</w:t></w:r></w:p></w:tc></w:tr></w:tbl><w:p><w:r><w:t>After</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>Neighbor</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:noBreakHyphen/></w:r></w:p></w:tc></w:tr><w:tr><w:tc><w:p><w:r><w:noBreakHyphen/></w:r></w:p></w:tc></w:tr></w:tbl></w:body></w:document>',
699 },
700 - expected: ["## Report & totals", "North\t12 34", "Before Inner After\tNeighbor"],
700 + expected: [
701 + "## Report & totals",
702 + "North\t12 34",
703 + "Before Inner After\tNeighbor\t\u2011\n\u2011",
704 + ],
705 },
706 {
707 ext: "docx",
708 bounded: true,
709 parts: {
710 "word/document.xml":
707 - '<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:r></w:p></w:body></w:document>',
711 + '<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>',
712 },
709 - expected: ["First\nSecond\nThird"],
713 + expected: ["First\nSecond\nThird\u2011party\u2011End"],
714 },
715 {
716 ext: "docx",
717 bounded: true,
718 parts: {
719 "word/document.xml":
716 - '<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>',
720 + '<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:noBreakHyphen/></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>',
721 },
722 expected: ["Intro 3\nFee $700\tdue"],
719 - absent: ["PAGEREF", "$500", "Moved"],
723 + absent: ["PAGEREF", "$500", "Moved", "\u2011"],
724 },
725 {
726 ext: "odt",
@@ -1042,7 +1046,7 @@ describe("question answer paths", () => {
1046 "OEBPS/book.opf":
1047 '<package><manifest><item id="a" href="a.xhtml"/><item id="b" href="b.xhtml"/></manifest><spine><itemref idref="b"/><itemref idref="a"/></spine></package>',
1048 "OEBPS/a.xhtml":
1045 - '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><body><p>Last chapter</p><p>Price <s>10</s> 8</p></body></html>',
1049 + '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><body><p>Last chapter</p><p>Price <s>10</s> 8 <del>old</del></p></body></html>',
1050 "OEBPS/b.xhtml":
1051 "<!DOCTYPE html><html><body><h1>First chapter</h1><p>Larkspur</p><div>Direct div<section>Direct section<li>Direct item <em>emphasis</em> trailing</li></section></div><div>Account: A<br/>Balance: 731<line-break/>Currency: USD</div><table><thead><tr><th>Heading</th><th>Total</th></tr></thead><tbody><tr><td>Important value</td><td>731</td></tr><tr><td>Before<table><tr><td>Inner</td></tr></table>After</td><td>Neighbor</td></tr></tbody></table></body></html>",
1052 },
@@ -1059,7 +1063,7 @@ describe("question answer paths", () => {
1063 "Before Inner After\tNeighbor",
1064 "## Chapter 2",
1065 "Last chapter",
1062 - "Price 10 8",
1066 + "Price 10 8 old",
1067 ],
1068 },
1069 ];