GeezerVison is showing github.com — fix(server): preserve DOCX hyphen semantics · pingdotgg/t3code@c232b98
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/documentTextWorker.ts +11-6Lines changed: 11 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,12 +41,12 @@ interface XmlNode { | 41 | 41 | hasTextChoice?: boolean; | 42 | 42 | } | 43 | 43 |
| 44 | | -function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode { | | 44 | +function xml(source: string, extension: string): XmlNode { | 45 | 45 | const root: XmlNode = { name: "root", attrs: {}, children: [] }; | 46 | 46 | const stack = [root]; | 47 | 47 | let nodes = 0; | 48 | 48 | const parser = new SaxesParser(); | 49 | | - if (htmlEntities) Object.assign(parser.ENTITIES, characterEntities); | | 49 | + if (extension === ".epub") Object.assign(parser.ENTITIES, characterEntities); | 50 | 50 | parser.on("opentag", (tag) => { | 51 | 51 | if (++nodes > 200_000 || stack.length > 128) throw new Truncation("XML structure limit"); | 52 | 52 | const node: XmlNode = { | @@ -59,20 +59,25 @@ function xml(source: string, odfSpaces = false, htmlEntities = false): XmlNode { | 59 | 59 | ), | 60 | 60 | children: [], | 61 | 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); | 63 | 64 | stack.at(-1)!.children.push(node); | 64 | 65 | stack.push(node); | 65 | 66 | }); | 66 | 67 | parser.on("closetag", () => { | 67 | 68 | const node = stack.pop()!; | 68 | 69 | const parent = stack.at(-1)!; | | 70 | + if (extension === ".docx" && node.name === "del") { | | 71 | + parent.children.pop(); | | 72 | + return; | | 73 | + } | 69 | 74 | // Markup-compatibility fallbacks repeat their mc:Choice content, e.g. Word text boxes. | 70 | 75 | // Keep one when no Choice carries text, such as a chart with a VML caption. | 71 | 76 | if (node.name === "Fallback" && parent.hasTextChoice) { | 72 | 77 | parent.children.pop(); | 73 | 78 | return; | 74 | 79 | } | 75 | | - if (!node.hasText) return; | | 80 | + if (!node.hasText && node.name !== "noBreakHyphen") return; | 76 | 81 | parent.hasText = true; | 77 | 82 | if (node.name === "Choice") parent.hasTextChoice = true; | 78 | 83 | }); | @@ -165,7 +170,7 @@ function odfValue(cell: XmlNode): string { | 165 | 170 | } | 166 | 171 | } | 167 | 172 | function hasCellText(cell: XmlNode): boolean { | 168 | | - if (odfValue(cell).trim()) return true; | | 173 | + if (odfValue(cell).trim() || cell.name === "noBreakHyphen") return true; | 169 | 174 | // Space markers can have enormous repeat counts; emptiness never requires expanding them. | 170 | 175 | if (HIDDEN.has(cell.name) || cell.spaceCount !== undefined) return false; | 171 | 176 | return cell.children.some((child) => | @@ -442,7 +447,7 @@ async function* document(path: string, extension: string): AsyncGenerator<TextPa | 442 | 447 | try { | 443 | 448 | const data = files.get(name); | 444 | 449 | if (!data) throw new Error("Missing document part"); | 445 | | - return xml(data, [".odt", ".ods", ".odp"].includes(extension), extension === ".epub"); | | 450 | + return xml(data, extension); | 446 | 451 | } catch (error) { | 447 | 452 | if (error instanceof Truncation) throw error; | 448 | 453 | skip(name); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/userInputAttachments.test.ts +12-8Lines changed: 12 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -695,28 +695,32 @@ describe("question answer paths", () => { | 695 | 695 | bounded: true, | 696 | 696 | parts: { | 697 | 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 & 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 & 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 | 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 | + ], | 701 | 705 | }, | 702 | 706 | { | 703 | 707 | ext: "docx", | 704 | 708 | bounded: true, | 705 | 709 | parts: { | 706 | 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:noBreakHyphen/><w:t>party</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>', | 708 | 712 | }, | 709 | | - expected: ["First\nSecond\nThird\u2011party"], | | 713 | + expected: ["First\nSecond\nThird\u2011party\u2011End"], | 710 | 714 | }, | 711 | 715 | { | 712 | 716 | ext: "docx", | 713 | 717 | bounded: true, | 714 | 718 | parts: { | 715 | 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>', | 717 | 721 | }, | 718 | 722 | expected: ["Intro 3\nFee $700\tdue"], | 719 | | - absent: ["PAGEREF", "$500", "Moved"], | | 723 | + absent: ["PAGEREF", "$500", "Moved", "\u2011"], | 720 | 724 | }, | 721 | 725 | { | 722 | 726 | ext: "odt", | @@ -1042,7 +1046,7 @@ describe("question answer paths", () => { | 1042 | 1046 | "OEBPS/book.opf": | 1043 | 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>', | 1044 | 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>', | 1046 | 1050 | "OEBPS/b.xhtml": | 1047 | 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>", | 1048 | 1052 | }, | @@ -1059,7 +1063,7 @@ describe("question answer paths", () => { | 1059 | 1063 | "Before Inner After\tNeighbor", | 1060 | 1064 | "## Chapter 2", | 1061 | 1065 | "Last chapter", | 1062 | | - "Price 10 8", | | 1066 | + "Price 10 8 old", | 1063 | 1067 | ], | 1064 | 1068 | }, | 1065 | 1069 | ]; | |
|