Skip to content

Commit d8cadaa

Browse files
fix(web): Word previews keep long headers and footers
A header or footer over 128 KB of raw XML was always left out and the preview marked truncated, even when its layout was trivial. Drop that cutoff and rely on the expansion budget, as main does. AI-assisted; verified with the tests and tools listed above.
1 parent 75c3835 commit d8cadaa

2 files changed

Lines changed: 6 additions & 22 deletions

File tree

‎apps/web/src/components/files/officeArchive.test.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,19 @@ describe("Word preview limits", () => {
116116
expect(await result.part("word/document.xml")).toContain("After");
117117
});
118118

119-
it.each([
120-
["used", true],
121-
["unused", false],
122-
])("leaves out only a header too long to lay out (%s)", async (_, used) => {
119+
it("keeps a long header", async () => {
123120
const header = `<w:hdr ${W}>${Array.from({ length: 3_000 }, (_, line) => paragraph(`Header line ${line}`)).join("")}</w:hdr>`;
124121
const footer = `<w:ftr ${W}>${paragraph("Footer")}</w:ftr>`;
125122
const footnotes = `<w:footnotes ${W}><w:footnote w:id="1">${paragraph("Note")}</w:footnote></w:footnotes>`;
126123
const result = await prepared(
127124
await wordFile(
128125
`<w:p><w:r><w:t>Body</w:t></w:r><w:r><w:footnoteReference w:id="1"/></w:r></w:p>`,
129126
{ header, footer, footnotes },
130-
`${used ? '<w:headerReference w:type="default" r:id="header"/>' : ""}<w:footerReference w:type="default" r:id="footer"/>`,
127+
`<w:headerReference w:type="default" r:id="header"/><w:footerReference w:type="default" r:id="footer"/>`,
131128
),
132129
);
133-
expect(result.truncated).toBe(used);
134-
expect(await result.part("word/header.xml")).toBeUndefined();
130+
expect(result.truncated).toBe(false);
131+
expect(await result.part("word/header.xml")).toBe(header);
135132
expect(await result.part("word/footer.xml")).toBe(footer);
136133
expect(await result.part("word/footnotes.xml")).toBe(footnotes);
137134
expect(await result.part("word/document.xml")).toContain("Body");

‎apps/web/src/components/files/officeArchive.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export const WORD_LIMITS = {
2929
numberingBytes: 32 * 1024 * 1024,
3030
numberingMarkup: 400_000,
3131
auxiliaryPartBytes: 8 * 1024 * 1024,
32-
/** A header's layout grows with its own length, not the page count: 128 KB takes about 1 s. */
33-
headerBytes: 128 * 1024,
3432
auxiliaryMarkup: 1_000_000,
3533
/** Body, styles, lists and notes, with headers and footers counted once per page. */
3634
expandedBytes: 64 * 1024 * 1024,
@@ -814,22 +812,12 @@ async function wordExpansion(
814812
const nodes = new Map<string, { xml: string; path: string }>();
815813
const optional = new Set<string>();
816814
const noteParts = new Map<string, string>();
817-
// Headers and footers too long to lay out, and whether the document uses one.
818-
const oversized = new Set<string>();
819-
let oversizedUsed = false;
820815
for (const [path, roles] of parts.roles) {
821816
if (![...roles].some((role) => WORD_EXPANDED_PARTS.has(role))) continue;
822817
const xml = await archive.file(path)?.async("string");
823818
if (!xml) continue;
824819
optional.add(path);
825-
if (roles.has("header") || roles.has("footer")) {
826-
// A header's layout grows with its own length: a long one is left out, as an unused one is.
827-
if (xml.length > WORD_LIMITS.headerBytes) {
828-
oversized.add(path);
829-
continue;
830-
}
831-
nodes.set(path, { xml, path });
832-
}
820+
if (roles.has("header") || roles.has("footer")) nodes.set(path, { xml, path });
833821
for (const role of ["footnotes", "endnotes", "comments"]) {
834822
if (!roles.has(role)) continue;
835823
noteParts.set(role.slice(0, -1), path);
@@ -876,7 +864,6 @@ async function wordExpansion(
876864
if (tag.local === "headerReference" || tag.local === "footerReference") {
877865
const target = parts.relationships.get(path)?.get(attr("id"));
878866
if (target) headers.add(target);
879-
if (target && oversized.has(target)) oversizedUsed = true;
880867
}
881868
if (tag.local === "altChunk") cost.bytes = Infinity;
882869
if (tag.local !== "blip" && tag.local !== "imagedata") return;
@@ -984,7 +971,7 @@ async function wordExpansion(
984971
xml = xml.slice(0, range.start) + xml.slice(range.end);
985972
archive.file(relPath, xml);
986973
}
987-
return { costs, truncated: truncated || oversizedUsed, omitted };
974+
return { costs, truncated, omitted };
988975
}
989976

990977
// Stream each entry before handing it to an engine. A tiny compressed archive

0 commit comments

Comments
 (0)