5 Commits
Author SHA1 Message Date
DolanandClaude Opus 5.5 2e07d2d688 perf: make the browser bundle 29 KB smaller, zipping with fflate and hashing with docx's own SHA-1 and SHA-256 (#3834) (#3836)
* build(deps): add fflate, to zip and unzip packages in place of JSZip

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* perf: hash with docx's own SHA-1 and SHA-256, in place of hash.js

hash.js was about 6.5 KB of a browser bundle, minified and gzipped, as its whole library is bundled for the two hashes
docx uses: SHA-1 for the names of media files (`hashedId`) and SHA-256 for the password that protects a document. It
hasn't had a release since 2018, and brought `inherits` and `minimalistic-assert` with it. The platforms' own hashes
can't replace it, as browsers only hash asynchronously and documents are written synchronously.

The hashes are the same: they match FIPS 180-4's examples and node:crypto, and the password hash Word accepts is
unchanged. They are also faster: hashing a 5 MB image takes about half the time, and a password's 100,000 rounds about
130 ms rather than 155 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* perf: zip and unzip with fflate, in place of JSZip

JSZip was about 30 KB of a browser bundle, minified and gzipped, as its prebuilt browser file brings its own copy of
pako and more. fflate's zipSync and unzipSync, the only parts docx needs, are about 6 KB. Together with the hashes, the
bundle of a page that packs a document goes from 123 KB to 94 KB.

Nothing changes for users:
- Packer and patchDocument give each output type as JSZip gave it, including "string" and "binarystring" as a
  character for each byte, a Blob with the .docx or .docm MIME type, and the same errors for "nodebuffer" without
  Buffer, a Blob without Blob, and "text", which JSZip never gave.
- patchDocument and patchDetector still take a JSZip instance, which is read through its generateAsync, so the types
  no longer import jszip. They also still take the other forms JSZip read: a string with a character for each byte, an
  array of bytes, a Blob, an ArrayBuffer and bytes from another realm.
- A part's text keeps a byte order mark at its start, as it did.
- Packer.toStream still compiles synchronously, so it throws as before, and gives the document a turn of the event
  loop later.

Zipping is now synchronous, and faster: packing a document with 5,000 paragraphs and 5 MB of images takes 83 ms
rather than 182 ms.

The zip files no longer have folder entries, which JSZip added for each folder. Word doesn't write them, and nothing
reads them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: test the packer and the patcher's zip reading without JSZip

These specs mocked JSZip or read the JSZip instance the compiler returned. They now build real zip files and read
what the Packer and the patcher give, so they test the zipping too:
- loadZip reads every form a template can be given in, a JSZip instance through its generateAsync, and leaves out
  folders
- Packer.toStream throws when the document can't be written, and gives it even to a listener added after an await
- toBlob, toArrayBuffer and toStream give real zip files, rather than what a mocked compiler gave

The counts of the files in a package no longer include the folder entries JSZip added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: test zipping, unzipping and each output type of a zip file

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* chore: read and write .docx files in the scripts with fflate, in place of JSZip

docx is replacing JSZip with fflate (#3834), and jszip is leaving package.json, so the scripts that read documents
(compare-layout.ts, the layout probes' line checkers) and the probes that edit docx's output before writing it use
fflate's unzipSync and zipSync. They leave out folder entries when they unzip, as Word's own files have none, and zip
at level 6. The written documents have the same files in the same order, with the same contents, as before.

check-api.ts's comment no longer says the built declarations import jszip. Those of releases up to 9.14.0 still do,
and the import, which no longer resolves, is any there, which the check accepts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: load Node's types from docx's declarations, which JSZip's used to load

The declarations use Node's types, such as Buffer and NodeJS.ReadableStream. They had them because they imported
JSZip's declarations, which reference Node's types. Without JSZip, a project that doesn't load every @types package,
such as one with `types` set, or TypeScript 6's default, would find no Buffer in docx's types, and docx's own optional
entries didn't build. The reference is kept in the built declarations with `preserve="true"`, as TypeScript otherwise
leaves it out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: read and make zip files with fflate in the specs of charts, embedded files, macros, shapes and the core, in place of JSZip

jszip is leaving docx's dependencies, so these specs read packed documents and make changed templates with the helpers
in tests/zip, which unzip and zip with fflate, as docx now does. Each test checks what it checked before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: read and make zip files with fflate in the specs of docx/html, docx/layout and the patcher, in place of JSZip

jszip is leaving docx's dependencies, as docx now zips and unzips with fflate, so these specs read packed documents
and make templates with the helpers in src/tests/zip.ts. Templates that were JSZip instances are now zip files' bytes,
and the specs read text and bytes from the unzipped files directly, so some helpers that only awaited JSZip are no
longer asynchronous.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* test: patch real zip files in patchDocument's spec, rather than mocking JSZip

JSZip is leaving the dependencies. The spec mocked JSZip.loadAsync and passed an empty Buffer as the template, so
patchDocument never unzipped anything. Each template is now a real zip file, made with the specs' zip helper, so the
tests exercise the unzipping too. The tests of a template opened with JSZip pass an object with generateAsync, which
is how a JSZip instance is read now.

The path keys of the templates are covered by one file-level naming-convention exception, in place of the comments on
each patch key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* build(deps): remove jszip, as nothing uses it now

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: refuse a zip file's entry of the wrong size before taking memory for it, and inflate only what patchDetector reads

fflate takes room for the size a zip file gives an entry before inflating it, so a template of a few hundred bytes
could make docx take 4 GB, which fails in a browser or a process with a memory limit. JSZip refused such an entry
("uncompressed data size mismatch"). An entry whose given size is more than DEFLATE can inflate its data to (1032
times) is now refused before fflate takes the room, and one that inflates to less than its given size after.

JSZip also inflated only the entries that were read, so patchDetector, which reads only the XML parts in word/, never
inflated a template's images or embedded files. unzipFiles and loadZip now take the paths to read, and patchDetector
gives them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

* fix: refuse a zip file whose entries claim more compressed data than the zip file holds, before taking memory for them

The size an entry inflates to was checked against its compressed size, but that is also read from the zip file, so an
entry could claim compressed data larger than the zip file and pass. The entries docx reads now have their compressed
sizes added up, and a total larger than the zip file is refused before fflate takes room for the entry. Together with
DEFLATE's ratio, that keeps the room taken to 1032 times the zip file's size, as a real zip bomb's is.

The tests damage the entry's data too, so only a refusal before inflating gives their error: without either check, each
fails with fflate's own error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 00:08:58 +01:00
Dolan 7907016816 fix: read and write XML without xml and xml-js, which are no longer maintained (#3779) 2026-10-17 09:44:22 +01:00
Dolan e008c45c0c feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages (#3619)
* feat(layout): read a .docx into the layout, to write a template's page numbers and check Word's own pages

docx/layout read only the document being written, from its formatted
XML. A second adapter, read-docx.ts, reads a parsed .docx into the same
blocks: it follows the package's relationships to the main document and
its styles, theme, numbering, settings, headers, footers and notes, and
reads them with the same reader, which now takes a document's parts
rather than context.file.

patchDocument takes the same estimatePageNumbers as Document, as its
pageNumbers. Once the patches are in, it gives the estimator the
template's parsed parts and writes the page numbers into the PAGEREF,
NUMPAGES and SECTIONPAGES fields of its body, headers and footers. The
numbers a template was saved with are those from before it was filled
in, so one the layout can't work out is left blank. The writing of the
fields is shared between formatted and parsed trees.

A document in compatibility mode stops the layout: Word lays it out as
an older version of Word did, which isn't followed. LibreOffice writes
mode 12, and docx writes 15.

compare-layout.ts lays out documents Word saved through the adapter, and
compares them with the pages Word marked with w:lastRenderedPageBreak,
read with the rules Word's PDFs of the layout demos showed, and with
Word's PDFs. The 7 demos whose marks have all their pages are committed
in scripts/layout-probes, so CI checks docx/layout against Word's own
pages. A new demo fills a template in with patchDocument and writes its
page numbers.

* fix(layout): fail the layout comparison when there are no documents saved from Word to compare

compare-layout.sh copied the documents saved from Word in
scripts/layout-probes into its output directory only if they were there,
so when the glob found none, as when the script runs from another
directory, Word's own pages weren't compared and the comparison passed
anyway. It now stops with an error. compare-layout.ts still compares a
directory without them, as it does when run on its own.

* fix(layout): compare documents saved from Word only with the pages Word marked in them

compare-layout.ts compared each document saved from Word with Word's PDF
of the demo of the same name too. When a demo changes, as columns did in
#3611, Word's PDF is made again of the new demo, and no longer is of the
document Word saved, so the two disagreed although docx/layout matched
both. A document saved from Word has Word's own pages in its marks, so it
is compared only with them, and one whose marks don't have all its pages
isn't compared.

* fix(layout): fail the layout comparison when a committed document saved from Word can't be compared

The documents saved from Word in scripts/layout-probes are committed
because Word's marks have all their pages. One whose marks no longer read
as having them all was skipped without failing, so a mistake in reading
Word's pages would have turned off the check in CI. It now fails. A
document only in the output directory whose marks don't have all its
pages, as Word's columns and long-table-of-contents, is still left out.
2026-10-02 01:48:30 +01:00
DolanandClaude Opus 5.5 3f5a17cd10 feat(layout): break table rows across pages, lay out footnotes and columns, and write the numbers of pages (#3592)
* feat(layout): break table rows across pages between the lines of their cells

Word and LibreOffice break a table row that doesn't fit across the pages
unless it is kept whole (cantSplit), but the layout moved every row to the
next page whole, which put the headings after a table of long rows on the
wrong page. Rows now break between the lines of their cells, with the
header rows repeated above the rest, and without widow control inside
them, as LibreOffice does. Rows kept whole, of an exact height, or whose
text fits but not their set height, still move whole. A row that would
break with merged cells, a table in it, or a height set taller than its
text stops the layout, as nothing checks how those break yet.

LibreOffice's lines are whole twips tall, so lines are rounded to twips:
that matches its line pitch for 80 of 90 font sizes measured, against none
before. The tables demo is at 12 points, as LibreOffice's lines of Times
New Roman at 10 points are a twip taller than the font's metrics.

A new layout demo, tables-across-pages, has rows of each kind across page
breaks. All 185 headings of the 7 demos are on LibreOffice's page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* feat(layout): lay out footnotes at the bottom of their pages, and endnotes after the text

The layout stopped at the first footnote, so most reports lost every page
number after it. Now a page's footnotes take room at its bottom, below
their separator, with the space between them, but not before the
separator or after the last, as LibreOffice lays them out. A line whose
one-line footnote doesn't fit below it moves to the next page with it, as
in LibreOffice. LibreOffice continues a longer footnote on the next page,
splitting even one widow control would keep together, and which of its
lines stay isn't followed yet, so the layout stops there, and at a
footnote in a table row that breaks across pages.

Endnotes follow the text, after their separator, as Word and LibreOffice
lay them out. Footnotes are numbered 1, 2, 3 and endnotes i, ii, iii, and
their numbers are as narrow as superscript and as tall as their font.

A new layout demo, footnotes-and-endnotes, has 40 footnotes over 8 pages.
All 18 of its headings are on LibreOffice's page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* feat(layout): lay out sections in columns

The layout stopped at the first section in columns. Now each page's
columns are filled in turn, each as wide as the section gives, before the
next page: text moves to the next column when it doesn't fit, or is kept
with what is there, and a column break starts the next column, or a new
page in the last. A section in columns that starts on the same page as
the text before it starts its columns below that text.

Word and LibreOffice even out a section's columns before a continuous
section break, so the next section starts below the longest, and which
lines go in which column isn't followed yet, so the layout stops there,
as it does at a footnote in columns and at a table's header rows repeated
at the top of a column.

scripts/compare-layout.sh reads LibreOffice's text in reading order, so a
page's columns come one after the other rather than side by side. A new
layout demo, columns, has sections of one, two and three columns. All 227
headings of the 9 demos are on LibreOffice's page.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* feat(layout): write the numbers of pages into NUMPAGES and SECTIONPAGES fields

paginate counted the pages, but nothing wrote the count, so "Page 3 of"
footers were written without their number, which applications that show
fields as they are written show blank. Now docx/layout works out the
number of pages of the document, when it lays all of it out, and of each
section whose pages are its own, laying the pages out again until the
numbers, which can change how lines wrap, stay the same.

EstimatedPageNumbers gains pageCount and sectionPageCounts, and docx
writes them into the NUMPAGES and SECTIONPAGES fields of the body, and
of the headers and footers once the body is written, with the page
references in them. A header or footer shows the number of pages of the
sections it is on only when they all have the same. A section that
shares a page with another, or has a blank page before or after it, is
left blank, as is a field in a number format of its own. Like PAGEREF
fields, those with a numeric picture (\#) are left as they are.

scripts/compare-layout.ts checks the numbers of pages written into the
demos against the pages of LibreOffice's PDFs: both demos with them match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix(layout): keep a paragraph with the next on a new page when they fit there

Whether what is kept together fits in a fresh column was measured from
where the page's columns start, which is partway down the page below a
continuous section break. When the column is the last, the kept
paragraphs go on a new page, whose text starts at the top, so a heading
that would fit there with the next paragraph stayed at the bottom of the
page, and the paragraph moved on alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix(layout): stop at a section whose columns differ in width

A paragraph is broken into lines at the width of the column it starts
in, and a paragraph that goes on into a column of another width kept
those lines, so its height, and the pages after it, were wrong. Breaking
the rest of a paragraph again needs to know where each line starts in
its text, so a section with columns of different widths stops the
layout, and the page numbers after it are left blank.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

* fix: fail the layout comparison when LibreOffice didn't lay out a document

The comparison skipped a document without the text of LibreOffice's
pages, as when LibreOffice fails to convert it, and reported its
headings as matching, so CI could pass without comparing it. Now it
fails, and names the documents LibreOffice didn't lay out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 12:50:47 +01:00
Dolan 0307c6556d feat: docx/layout, which writes the page numbers of tables of contents and page references (#3591) 2026-09-30 10:46:29 +01:00