A table with `float: { relativeVerticalPosition: RelativeVerticalPosition.INLINE }` was written with
`w:tblpYSpec="inline"` in its `w:tblpPr`. Word has no floating table in line with the text: it places such a table
at the top of the paragraph after it, with text beside it, and saves it back with the `inline` dropped
(`<w:tblpPr w:vertAnchor="text" w:tblpXSpec="center"/>`). So the table never stayed in line with the text, as the
option promises.
Such a table is now written in line with the text, as Word writes a table with no text wrapping: without
`w:tblpPr` or `w:tblOverlap`. Its `relativeHorizontalPosition` of left, center or right becomes its `w:jc`, as Word's
row alignment reports those of a floating table, unless the table has an `alignment` of its own. Word reads the
issue's table as not wrapping text and centered, and lays it out centered between the paragraphs before and after it.
The tables docs now show `alignment`, and say what `INLINE` writes.
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
A cell's borders take `diagonalDown` and `diagonalUp`, Word's "Diagonal
Down Border" and "Diagonal Up Border", written as `w:tl2br` and `w:tr2bl`
after the sides, in CT_TcBorders' order. A diagonal down border splits a
header cell into a label for the columns, above the line, and one for the
rows, below it.
Checked in Word for Mac: a cell merged with columnSpan or rowSpan gets one
diagonal across the whole merged cell. docx already copies a merged cell's
borders into the cells it adds below, and Word draws the same either way,
as it keeps the diagonal on the cells below when it saves.
Claude-Session: https://claude.ai/code/session_01X5zGDL3sgGVBMXPfny376c
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Word makes a row below a cell merged down rows as tall as the merged
cell's margins in it, as word-merged-cell-margins.docx shows: with
rowSpan, the cells docx adds take the table's margins, so the rows below
are as tall as the table's margins above and below and a line.
docx/layout left those cells out of the row's height, and now counts
their margins.
The table docs now show merging cells by hand with verticalMerge, which
gives the merged cell's part in each row settings of its own, such as no
margins, which keeps the rows below to a line. That is what #668 asked
for besides keeping rows together.
Claude-Session: https://claude.ai/code/session_0121RHRfmgN8jDksNAdAWKnK
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
A table always wrote `w:tblBorders` with a thin single line on every side
it wasn't given, which covered the borders of its table style. A table
with a `style` now writes only the sides `borders` gives, and none
without `borders`, so the style's borders show. Tables without a style
keep their thin single borders.
Fixes#1497
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Word repeats a table's first row and the rows straight after it that set
tableHeader, and ignores tableHeader on rows further down. Document this,
and show how to give each part of a long table its own repeating header
by making each part a table of its own.
Closes#2425
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
* feat(table): write each cell's width from columnWidths, so Word keeps them
Word lays a table out from its cells' widths (w:tcW), and sizes the
columns of cells without one to their text, whatever the grid
(w:tblGrid) says. docx wrote columnWidths only to the grid, so in Word
they did nothing unless every cell was given the same width again, as
#517, #352, #1099 and #561 found. A table saved as a PDF from Word 16 for
Mac showed it: "Entry" shrank to its text and gave its room to the other
columns.
Each cell without a width of its own now gets the width of the columns it
spans, the cells added for rowSpan included. When the table's width is
wider than its columns add up to, the cells' widths are scaled up to fill
it, as Word and LibreOffice lay the columns out, so columnWidths given as
proportions of a 100% table, as some issues suggest, keep them. The grid
is written as given.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* feat(styles): write Normal Table, the default table style, with Word's cell margins
docx wrote no table style, and without one Word gives cells no margins,
so their text touched the borders: in a PDF saved from Word, the text of
each cell started half a point inside its border. LibreOffice gives them
the 0.075 inches (108 twips) on the left and right that Word gives the
tables it makes, so the same table had narrower lines of text there.
docx now writes Normal Table as Word writes it in its own documents, the
default table style with those margins, so tables look the same in Word
as tables made in it, and as in LibreOffice. A Normal Table in
externalStyles takes its place, and margins given to a table or cell
still win.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* fix(layout): lay cells out at their own widths, with the margins of their table's style
The layout took each cell's width from the grid and gave every cell
margins of 5.4 points, as LibreOffice does. Word goes by a cell's own
width in twips when it has one, and takes cell margins from the table's
style, or the default table style, and gives none without either.
Cells are now laid out at their own width in twips when they have one,
and their margins come from the table's style and the styles it is based
on, or the default table style when it has none, then the table's own.
With docx's Normal Table, that is the 5.4 points it was, and the table
demos' headings stay on LibreOffice's pages. The docs say to give tables
columnWidths for their page numbers to be right, as Word sizes the columns
of a table without widths to their text.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* docs(table): don't suggest columnWidths as proportions for readers that go by the grid
The warning about readers that lay tables out from the grid alone ended by
suggesting columnWidths as proportions of a table with a width. Only the
cells' widths are scaled to the table's width, and the grid is written as
given, so those readers still take [20, 80] as 20 and 80 twips. The
warning asks for real twips again, and the paragraph on scaling says it
applies to the readers that go by the cells' widths.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NijyutGmb3bNR3dNRWJken
* fix(layout): stop at a table whose rows give a column different widths
With cells laid out at their own widths, two rows could give the same
column different widths, such as a one-column table whose first cell is
1000 twips wide and whose second is 3000. Word settles the column's width
in a way the layout doesn't follow yet, so at least one row's lines were
broken at the wrong width. The layout now stops at such a table and
leaves the page numbers after it blank, as it does for other things it
can't lay out. Widths a point apart, from rounding, still count as equal.
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>
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded
The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.
Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.
Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
demo/assets, or make their own
`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ
* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document
"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* chore: organise the demos into a folder for each topic
The demos were one flat list of 124 numbered files, which made it hard to
find the one you wanted. Each is now in a folder for its topic, such as
demo/tables/basic-table.ts, with a name that says what it shows instead of
a number.
npm run demo lists every demo and narrows the list as you type. It also
takes a demo's path or name, a topic, or search words.
The demos' images moved to demo/assets/images, as images is now a topic.
The docs' links, the Demos workflow, the shape render script and its
reference images use the new paths and names.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ
* fix: list only a topic's own demos, and drop stale output file names
npm run demo text also offered the text-boxes demos and others with
"text" in their name, so a topic now searches for "text/". Three demos
logged their old numbered file name after writing My Document.docx.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* fix(table): emit numeric pct widths in fiftieths of a percent
createTableWidthElement wrote a numeric PERCENTAGE size as a "50%" string.
Both that string and the fiftieths-of-a-percent integer (50% = 2500) are
valid under ST_MeasurementOrPercent, but Word writes the integer, and
stricter consumers — Google Docs and Apple Pages/QuickLook/Mail — misread
the string form and collapse table columns to one character.
Emit the integer (Math.round(size * 50)) for numeric pct sizes to match
Word's output. Callers who want the literal percent string can still pass a
Percentage (e.g. "50%"), which is left untouched. Adds table-width.spec.ts
and updates the two specs that asserted the old string output.
* fix(table): derive tblGrid from table and cell widths so tables render in Google Docs and Pages
Writing pct widths as fiftieths of a percent (previous commit) is correct but
does not fix#1457 on its own. Google Docs, Apple Pages, QuickLook and other
consumers lay a table out from w:tblGrid alone and ignore w:tblW / w:tcW
percentages, while docx wrote a placeholder grid of 100 twips per column
whenever columnWidths was omitted. Every percentage-width table therefore
collapsed to one character per column in those readers (#1457, #349, #216,
#3015). Word recomputes the layout from the preferred widths, which is why it
was never affected.
When columnWidths is omitted the grid is now derived, in twips, the way Word
writes it after laying the table out:
- percentages resolve against the text width of the section the table is in
(page width minus margins and gutter, per column in multi-column sections),
or against the parent cell for nested tables, at serialization time
- dxa and universal-measure widths are used as they are
- columns without a width share what is left of the table width equally
- column spans and the continuation cells inserted for rowSpan are honoured
Explicit columnWidths are written unchanged. Tables formatted without a
document (bare Formatter, patcher) resolve against the default A4 page, and
formatting a table more than once is idempotent.
New: SectionProperties.AvailableTextWidth, Body.getSectionPropertiesFor,
universalMeasureToTwips, Table.ColumnWidths and Table.getCellWidth. Adds a
demo, documents columnWidths, and applies the review nits on table-width.spec.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
* test: use a cspell-friendly malformed measure in the universalMeasureToTwips spec
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
---------
Co-authored-by: Dolan Miu <dolan_miu@hotmail.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
* Added the table look parameter to new Table class to enable controlling various conditional formatting table properties (first row, first column etc)
* Improve documentation
---------
Co-authored-by: Dolan <dolan_miu@hotmail.com>
* fix: separate vertical alignment enums for table and section properties
- Introduced `VerticalAlignTable` for table-cell vertical alignment with valid values: `top`, `center`, and `bottom`.
- Added `VerticalAlignSection` for section properties, extending `VerticalAlignTable` with an additional value `both`.
- Marked the original `VerticalAlign` as deprecated for backward compatibility, directing users to the new enums.
- Updated type definitions for better clarity on valid vertical alignments.
* refactor: update vertical alignment imports and types for section and table properties
- Renamed `VerticalAlign` to `VerticalAlignSection` in section properties and `VerticalAlignTable` in table-cell properties for clarity.
- Updated type definitions to reflect the new enum names, ensuring better organization and understanding of vertical alignment options.
- Adjusted related test cases to utilize the new imports and types.
* refactor: update demos to use new enums for table and section properties for vertical alignment