@@ -482,6 +482,7 @@ describe("bounded binary previews", () => {
482482 } ) ;
483483} ) ;
484484import { OFFICE_PREVIEW_MAX_COLUMNS , type OfficeWorkerResult } from "./officePreviewProtocol" ;
485+ import { WORD_LIMITS } from "./officeArchive" ;
485486
486487describe ( "large text preview recovery" , ( ) => {
487488 it ( "retains the workspace viewer across URL renewals and reloads revisions and manual refreshes" , async ( ) => {
@@ -1190,7 +1191,7 @@ it.each([
11901191 : scenario === "runs"
11911192 ? `<w:p>${ "<w:r><w:t>x</w:t></w:r>" . repeat ( 50_001 ) } </w:p>`
11921193 : scenario === "tables"
1193- ? "<w:tbl/>" . repeat ( 50_001 )
1194+ ? "<w:tbl/>" . repeat ( 2 * WORD_LIMITS . bodyElements )
11941195 : scenario === "XML bytes"
11951196 ? `<w:p><w:r><w:t>${ "x" . repeat ( 2 * 1024 * 1024 ) } </w:t></w:r></w:p>`
11961197 : "<w:p/>" ;
@@ -1203,7 +1204,7 @@ it.each([
12031204 if ( scenario === "auxiliary XML" || scenario === "renamed XML" )
12041205 archive . file (
12051206 scenario === "renamed XML" ? "word/footnotes.dat" : "word/footnotes.xml" ,
1206- `<w:footnotes>${ "<w:p/>" . repeat ( 50_001 ) } </w:footnotes>` ,
1207+ `<w:footnotes>${ "<w:p/>" . repeat ( WORD_LIMITS . auxiliaryMarkup + 1 ) } </w:footnotes>` ,
12071208 ) ;
12081209 const file = await archive . generateAsync ( { type : "blob" , compression : "DEFLATE" } ) ;
12091210 const src = URL . createObjectURL ( file ) ;
@@ -1254,8 +1255,8 @@ it.each([
12541255 ) ;
12551256 }
12561257 expect ( markup . length ) . toBeLessThan ( body . length ) ;
1257- expect ( markup . length ) . toBeLessThanOrEqual ( 3 * 1024 * 1024 ) ;
1258- expect ( markup . match ( / < (? ! [ ! ? / ] ) / g) ! . length ) . toBeLessThanOrEqual ( 100_000 ) ;
1258+ expect ( markup . length ) . toBeLessThanOrEqual ( WORD_LIMITS . bodyBytes ) ;
1259+ expect ( markup . match ( / < (? ! [ ! ? / ] ) / g) ! . length ) . toBeLessThanOrEqual ( WORD_LIMITS . bodyTags ) ;
12591260 }
12601261 } finally {
12611262 worker . terminate ( ) ;
@@ -1286,15 +1287,15 @@ it.each([
12861287 ) ;
12871288 archive . file (
12881289 "word/_rels/document.xml.rels" ,
1289- `<Relationships>${ scenario === "content type" ? "" : `<Relationship Id="style" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="${ path . slice ( 5 ) } "/>` } ${ scenario === "metadata type" ? "<unused/>" . repeat ( 50_001 ) : "" } </Relationships>` ,
1290+ `<Relationships>${ scenario === "content type" ? "" : `<Relationship Id="style" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="${ path . slice ( 5 ) } "/>` } ${ scenario === "metadata type" ? "<unused/>" . repeat ( WORD_LIMITS . auxiliaryMarkup + 1 ) : "" } </Relationships>` ,
12901291 ) ;
12911292 archive . file (
12921293 path ,
12931294 scenario === "relationship bytes"
1294- ? `<styles>${ "x" . repeat ( 1024 * 1024 ) } </styles>`
1295+ ? `<styles>${ "x" . repeat ( WORD_LIMITS . auxiliaryPartBytes ) } </styles>`
12951296 : scenario === "deep XML"
12961297 ? `${ "<styles>" . repeat ( 65 ) } ${ "</styles>" . repeat ( 65 ) } `
1297- : `<styles>${ "<style/>" . repeat ( 50_001 ) } </styles>` ,
1298+ : `<styles>${ "<style/>" . repeat ( WORD_LIMITS . auxiliaryMarkup + 1 ) } </styles>` ,
12981299 ) ;
12991300 const bytes = await archive . generateAsync ( { type : "arraybuffer" , compression : "DEFLATE" } ) ;
13001301 const worker = new Worker ( new URL ( "./officePreview.worker.ts" , import . meta. url ) , {
@@ -1348,35 +1349,38 @@ async function expandedWordFixture(role: (typeof EXPANDED_WORD_ROLES)[number]) {
13481349 '<Relationships><Relationship Id="doc" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>' ,
13491350 ) ;
13501351 const text = ( value : string ) => `<w:p><w:r><w:t>${ value } </w:t></w:r></w:p>` ;
1351- // Each copy of the 2,000-paragraph payload costs 8,000 markup units, so 20
1352- // references (or pages) exceed the 100,000-unit expansion budget.
1352+ // Each copy of the 2,000-paragraph payload costs 6,000 markup units, so enough
1353+ // references (or pages) exceed the expansion budget.
1354+ const copies = Math . ceil ( WORD_LIMITS . expandedMarkup / 6_000 ) + 1 ;
13531355 archive . file (
13541356 "word/document.xml" ,
1355- `<w:document xmlns:w="${ ns } " xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>${ text ( "body retained" ) } ${ Array . from ( { length : 20 } , ( ) => `<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/><w:commentReference w:id="1"/><w:br w:type="page"/></w:r></w:p>` ) . join ( "" ) } <w:sectPr><w:headerReference w:type="default" r:id="aux"/><w:footerReference w:type="default" r:id="aux"/></w:sectPr></w:body></w:document>` ,
1357+ `<w:document xmlns:w="${ ns } " xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body>${ text ( "body retained" ) } ${ Array . from ( { length : copies } , ( ) => `<w:p><w:r><w:footnoteReference w:id="1"/><w:endnoteReference w:id="1"/><w:commentReference w:id="1"/><w:br w:type="page"/></w:r></w:p>` ) . join ( "" ) } <w:sectPr><w:headerReference w:type="default" r:id="aux"/><w:footerReference w:type="default" r:id="aux"/></w:sectPr></w:body></w:document>` ,
13561358 ) ;
13571359 archive . file (
13581360 "word/_rels/document.xml.rels" ,
13591361 `<Relationships><Relationship Id="aux" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/${ type } " Target="media/aux.xml"/></Relationships>` ,
13601362 ) ;
13611363 const xml =
13621364 role === "style chain" || role === "style count" || role === "style visits"
1363- ? `<w:styles xmlns:w="${ ns } ">${ Array . from ( { length : role === "style count" ? 3_000 : 1_000 } , ( _ , i ) => `<w:style w:styleId="s${ i } " w:type="paragraph">${ i ? `<w:basedOn w:val="s${ role === "style visits" && i >= 30 ? 29 : i - 1 } "/>` : "" } <w:pPr><w:spacing w:after="120"/></w:pPr></w:style>` ) . join ( "" ) } </w:styles>`
1365+ ? `<w:styles xmlns:w="${ ns } ">${ Array . from ( { length : role === "style count" ? WORD_LIMITS . styles + 1 : role === "style visits" ? Math . ceil ( WORD_LIMITS . styleVisits / 30 ) + 1 : 1_000 } , ( _ , i ) => `<w:style w:styleId="s${ i } " w:type="paragraph">${ i ? `<w:basedOn w:val="s${ role === "style visits" && i >= 30 ? 29 : i - 1 } "/>` : "" } <w:pPr><w:spacing w:after="120"/></w:pPr></w:style>` ) . join ( "" ) } </w:styles>`
13641366 : role === "styles"
13651367 ? `<w:styles xmlns:w="${ ns } ">${ Array . from ( { length : 100 } , ( _ , i ) => `<w:style w:styleId="${ "s" . repeat ( 2_000 ) } ${ i } " w:type="paragraph"><w:basedOn w:val="s0"/><w:pPr><w:spacing w:after="120"/></w:pPr></w:style>` ) . join ( "" ) } </w:styles>`
13661368 : role . includes ( "numbering" )
1367- ? `<w:numbering xmlns:w="${ ns } ">${ role === "picture numbering" ? '<w:numPicBullet w:numPicBulletId="1"/>' : "" } <w:abstractNum w:abstractNumId="1">${ Array . from ( { length : 9 } , ( _ , i ) => `<w:lvl w:ilvl="${ i } "><w:lvlText w:val="%1."/></w:lvl>` ) . join ( "" ) } </w:abstractNum>${ Array . from ( { length : 100 } , ( _ , i ) => `<w:num w:numId="${ i } "><w:abstractNumId w:val="1"/></w:num>` ) . join ( "" ) } </w:numbering>`
1369+ ? `<w:numbering xmlns:w="${ ns } ">${ role === "picture numbering" ? '<w:numPicBullet w:numPicBulletId="1"/>' : "" } <w:abstractNum w:abstractNumId="1">${ Array . from ( { length : 9 } , ( _ , i ) => `<w:lvl w:ilvl="${ i } "><w:lvlText w:val="%1."/></w:lvl>` ) . join ( "" ) } </w:abstractNum>${ Array . from ( { length : WORD_LIMITS . numberingReferences + 1 } , ( _ , i ) => `<w:num w:numId="${ i } "><w:abstractNumId w:val="1"/></w:num>` ) . join ( "" ) } </w:numbering>`
13681370 : role === "header" || role === "footer"
13691371 ? `<w:${ role === "header" ? "hdr" : "ftr" } xmlns:w="${ ns } ">${ text ( "omitted payload" ) . repeat ( 2_000 ) } </w:${ role === "header" ? "hdr" : "ftr" } >`
13701372 : `<w:${ role } xmlns:w="${ ns } "><w:${ role . slice ( 0 , - 1 ) } w:id="1">${ text ( "omitted payload" ) . repeat ( 2_000 ) } </w:${ role . slice ( 0 , - 1 ) } ></w:${ role } >` ;
13711373 archive . file ( "word/media/aux.xml" , xml ) ;
13721374 if ( role === "note roots" ) {
1375+ // Each note's XML repeats the 32 KB root, which the expansion budget charges twice.
1376+ const notes = Math . ceil ( WORD_LIMITS . expandedBytes / 64_000 ) + 1 ;
13731377 archive . file (
13741378 "word/document.xml" ,
1375- `<w:document xmlns:w="${ ns } "><w:body>${ text ( "body retained" ) } ${ Array . from ( { length : 200 } , ( _ , id ) => `<w:p><w:r><w:footnoteReference w:id="${ id } "/></w:r></w:p>` ) . join ( "" ) } </w:body></w:document>` ,
1379+ `<w:document xmlns:w="${ ns } "><w:body>${ text ( "body retained" ) } ${ Array . from ( { length : notes } , ( _ , id ) => `<w:p><w:r><w:footnoteReference w:id="${ id } "/></w:r></w:p>` ) . join ( "" ) } </w:body></w:document>` ,
13761380 ) ;
13771381 archive . file (
13781382 "word/media/aux.xml" ,
1379- `<w:footnotes xmlns:w="${ ns } " padding="${ "x" . repeat ( 32_000 ) } ">${ Array . from ( { length : 200 } , ( _ , id ) => `<w:footnote w:id="${ id } ">${ text ( "omitted payload" ) } </w:footnote>` ) . join ( "" ) } </w:footnotes>` ,
1383+ `<w:footnotes xmlns:w="${ ns } " padding="${ "x" . repeat ( 32_000 ) } ">${ Array . from ( { length : notes } , ( _ , id ) => `<w:footnote w:id="${ id } ">${ text ( "omitted payload" ) } </w:footnote>` ) . join ( "" ) } </w:footnotes>` ,
13801384 ) ;
13811385 }
13821386 return archive . generateAsync ( { type : "blob" , compression : "DEFLATE" } ) ;
@@ -1401,7 +1405,7 @@ it.each(EXPANDED_WORD_ROLES)(
14011405 ( sum , [ chunk ] ) => sum + ( typeof chunk === "string" ? chunk . length : 0 ) ,
14021406 0 ,
14031407 ) ,
1404- ) . toBeLessThan ( 4 * 1024 * 1024 ) ;
1408+ ) . toBeLessThan ( WORD_LIMITS . expandedBytes + 4 * 1024 * 1024 ) ;
14051409 const bounded = await JSZip . loadAsync ( prepared . bytes ) ;
14061410 const markup = await bounded . file ( "word/document.xml" ) ! . async ( "string" ) ;
14071411 expect ( markup ) . toContain ( "body retained" ) ;
@@ -1622,7 +1626,8 @@ it.each([false, true])(
16221626 } ,
16231627) ;
16241628
1625- it . each ( [ 1 , 145 ] ) (
1629+ // A body just over the read limit once each paragraph repeats its text this often.
1630+ it . each ( [ 1 , Math . ceil ( WORD_LIMITS . bodyBytes / 5_000 / 4 ) ] ) (
16261631 "counts shared Word settings once with %s text repetitions" ,
16271632 async ( repetitions ) => {
16281633 const { default : JSZip } = await import ( "jszip" ) ;
@@ -1658,7 +1663,8 @@ it.each([1, 145])(
16581663 const document = await bounded . file ( "word/document.xml" ) ! . async ( "string" ) ;
16591664 const retainedSettings = await bounded . file ( "word/settings.xml" ) ! . async ( "string" ) ;
16601665 expect ( document . match ( / < w : p > / g) ! . length ) . toBeGreaterThan ( 100 ) ;
1661- expect ( document . length + retainedSettings . length ) . toBeLessThanOrEqual ( 3 * 1024 * 1024 ) ;
1666+ expect ( document . length ) . toBeLessThanOrEqual ( WORD_LIMITS . bodyBytes ) ;
1667+ expect ( retainedSettings ) . toBe ( settings ) ;
16621668 } finally {
16631669 worker . terminate ( ) ;
16641670 }
0 commit comments