fix: a Word table draws only the borders it declares

The DOCX importer read w:tblBorders off the table but never off its table
style, so a table whose style declares none (Word's Normal Table, and the
thesis' Formel-tab around every formula) fell back to the editor's 0.5pt
black default. Resolved per side along the w:basedOn chain, and an
undeclared side is not drawn — LibreOffice writes fo:border="none" for
exactly those tables when it re-saves the file. The ODF importer already
treated an undeclared side that way.
This commit is contained in:
Steffen Becker committed 2026-08-09 08:54:59 +00:00
1 parent 2f6e771f06
commit 46ae0211af
5 files changed
+98 -3

No files matched your search

+5
View File
@@ -26,6 +26,11 @@ The **Open .odt** button (`App.svelte`) parses an uploaded file with `importOdt(
## Defaults on DOCX import
**Table borders** come from the table's own `w:tblBorders`, else its table style's along the
`w:basedOn` chain (per side, nearest declaring style wins). A side nobody declares is **not
drawn**: Word's Normal Table has no border, and the gridlines it shows on screen are not
printed. ODF says the same for an undeclared `fo:border`, so both importers agree.
Body text with no resolved font falls back to the *document's own theme minor font*
(`docx.ts` `runMarks`), not the editor default — Word's implicit body default. Headings don't
(they keep the editor heading default); DOCX page margins default to Word's 2.54cm.
+9 -1
View File
@@ -1539,8 +1539,16 @@ function buildTable(tbl: Element, ctx: Ctx): Node | null {
const pad = docxCellPadding(tbl, styleId, ctx);
const padBase = pad ?? DEFAULT_CELL_PADDING;
// The table's own w:tblBorders, then its style's; a side nobody declares is not drawn
// (Word's Normal Table has no border — its on-screen gridlines are not printed).
const tblBorders = fc(fc(tbl, 'tblPr'), 'tblBorders');
const tblSide = (name: string) => docxBorderAttr(fc(tblBorders, name));
const tblSide = (name: string) => {
for (const el of [tblBorders, ...ctx.styles.tableBorders(styleId)]) {
const v = docxBorderAttr(fc(el, name));
if (v !== undefined) return v;
}
return 'none';
};
const tblDef = {
top: tblSide('top'), bottom: tblSide('bottom'), left: tblSide('left'),
right: tblSide('right'), insideH: tblSide('insideH'), insideV: tblSide('insideV'),
+15 -1
View File
@@ -179,6 +179,7 @@ export class DocxStyles {
private ownKeepLines = new Map<string, boolean>(); // style's own w:pPr/w:keepLines
private ownTabs = new Map<string, TabStop[]>(); // style's own w:pPr/w:tabs
private ownCellMar = new Map<string, Element>(); // table style's own w:tblPr/w:tblCellMar
private ownTblBorders = new Map<string, Element>(); // table style's own w:tblPr/w:tblBorders
private defaultsWidow: boolean | null = null; // docDefaults w:pPrDefault/w:widowControl
private numToAbstract = new Map<string, string>();
private abstractLevels = new Map<string, Map<number, LevelDef>>();
@@ -253,8 +254,11 @@ export class DocxStyles {
const left = parseInt(ind.getAttributeNS(W, 'left') ?? ind.getAttributeNS(W, 'start') ?? '', 10);
if (Number.isFinite(left)) this.ownIndentTwip.set(id, left);
}
const cellMar = firstChild(style, 'tblPr') && firstChild(firstChild(style, 'tblPr')!, 'tblCellMar');
const tblPr = firstChild(style, 'tblPr');
const cellMar = tblPr && firstChild(tblPr, 'tblCellMar');
if (cellMar) this.ownCellMar.set(id, cellMar);
const tblBorders = tblPr && firstChild(tblPr, 'tblBorders');
if (tblBorders) this.ownTblBorders.set(id, tblBorders);
const kind = style.getAttributeNS(W, 'type');
if (kind === 'paragraph' || kind === 'character' || kind === 'table') {
const nameEl = firstChild(style, 'name');
@@ -466,6 +470,16 @@ export class DocxStyles {
return this.ownCellMar.get(styleId) ?? this.tableCellMar(this.basedOn.get(styleId) ?? null, seen);
}
// Every w:tblBorders along the w:basedOn chain, leaf first. The sides merge one by one,
// so the caller takes each from the nearest style that declares it.
tableBorders(styleId: string | null | undefined, seen = new Set<string>()): Element[] {
if (!styleId || seen.has(styleId)) return [];
seen.add(styleId);
const own = this.ownTblBorders.get(styleId);
const rest = this.tableBorders(this.basedOn.get(styleId) ?? null, seen);
return own ? [own, ...rest] : rest;
}
level(numId: number, ilvl: number): LevelDef {
const abs = this.numToAbstract.get(String(numId));
if (!abs) return {};