From 72c9364f946a97ad6c2840249cd344f9f7d42f6b Mon Sep 17 00:00:00 2001 From: lqtdys Date: Mon, 14 Sep 2026 23:33:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(docx):=20=E8=A1=A8=E6=A0=BC=E5=88=97?= =?UTF-8?q?=E5=AE=BD=E7=B2=BE=E7=A1=AE=E6=BB=A1=E5=AE=BD=209026=EF=BC=88?= =?UTF-8?q?=E6=8B=89=E4=BC=B8=E4=B8=8E=E5=8E=8B=E7=BC=A9=E5=8F=8C=E5=88=86?= =?UTF-8?q?=E6=94=AF=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - total < MAX_TOTAL 拉伸:等比放大 + 取整误差补首列 - total > MAX_TOTAL 压缩:min 800 保护后的精确修正循环 - 回归#13-1~5:拉伸/压缩/混合/触底保护四场景断言 fixes #13 --- packages/mdpkg/src/docx.ts | 25 ++++++++- packages/mdpkg/test/docx.test.ts | 90 ++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) diff --git a/packages/mdpkg/src/docx.ts b/packages/mdpkg/src/docx.ts index 5e7efb7..1e677a7 100644 --- a/packages/mdpkg/src/docx.ts +++ b/packages/mdpkg/src/docx.ts @@ -582,7 +582,30 @@ function computeTableColumnWidths(rows: { children?: unknown[] }[], colCount: nu const total = widths.reduce((a, b) => a + b, 0); if (total > MAX_TOTAL) { const scale = MAX_TOTAL / total; - return widths.map((w) => Math.max(MIN_COL_W, Math.floor(w * scale))); + const scaled = widths.map((w) => Math.max(MIN_COL_W, Math.floor(w * scale))); + let sum = scaled.reduce((a, b) => a + b, 0); + // 精确修正:若取整后总和仍 > MAX_TOTAL,从最宽列逐次 -1 + while (sum > MAX_TOTAL) { + let best = -1; + for (let i = 0; i < scaled.length; i++) { + if (scaled[i] > MIN_COL_W && (best === -1 || scaled[i] > scaled[best])) best = i; + } + if (best === -1) break; // 全列触底 800,无法再减 + scaled[best]--; + sum--; + } + // 取整导致总和 < MAX_TOTAL,差值加到首列 + if (sum < MAX_TOTAL) scaled[0] += MAX_TOTAL - sum; + return scaled; + } + if (total < MAX_TOTAL) { + // 内容宽度不足页宽时,按比例放大至满宽(fixed 布局按 gridCol 渲染,不满宽会留白) + const scale = MAX_TOTAL / total; + const scaled = widths.map((w) => Math.round(w * scale)); + // 修正取整误差,使总和精确等于 MAX_TOTAL(差值加到首列) + const diff = MAX_TOTAL - scaled.reduce((a, b) => a + b, 0); + scaled[0] += diff; + return scaled; } return widths; } diff --git a/packages/mdpkg/test/docx.test.ts b/packages/mdpkg/test/docx.test.ts index 54d47fc..9438c41 100644 --- a/packages/mdpkg/test/docx.test.ts +++ b/packages/mdpkg/test/docx.test.ts @@ -828,4 +828,94 @@ test('回归#9-7b callout 类型色:warning 黄 / note 蓝 / quote 灰', async assert.ok(doc.includes('w:fill="FCF8E8"'), 'warning 应使用黄色背景'); assert.ok(doc.includes('w:fill="F0F7FA"'), 'note 应使用蓝色背景'); assert.ok(doc.includes('w:fill="F5F5F5"'), 'quote 应使用灰色背景'); +}); + +// ============ 回归#13:短内容表格列宽不满宽 ============ + +function gridColSum(doc: string): number { + const re = /([\s\S]*?)<\/w:tblGrid>/; + const tbl = tblRe.exec(doc); + if (!tbl) return []; + const re = /w:w="(\d+)"/g; + const out: number[] = []; + let m: RegExpExecArray | null; + while ((m = re.exec(tbl[1])) !== null) out.push(Number(m[1])); + return out; +} + +test('回归#13-1 2列短内容表格:gridCol 总和 === 9026', async () => { + const body = '| 列A | 列B |\n| --- | --- |\n| 甲 | 乙 |\n'; + const out = await unpackDocx(toDocx(pkg(body))); + const doc = dec.decode(out.get('word/document.xml')!); + const sum = gridColSum(doc); + assert.equal(sum, 9026, `2列短内容表格 gridCol 总和应为 9026,实际 ${sum}`); +}); + +test('回归#13-2 4列短内容表格:gridCol 总和 === 9026', async () => { + const body = '| A | B | C | D |\n| --- | --- | --- | --- |\n| 甲 | 乙 | 丙 | 丁 |\n'; + const out = await unpackDocx(toDocx(pkg(body))); + const doc = dec.decode(out.get('word/document.xml')!); + const sum = gridColSum(doc); + assert.equal(sum, 9026, `4列短内容表格 gridCol 总和应为 9026,实际 ${sum}`); +}); + +test('回归#13-3 长内容压缩:3列各60全角字符 gridCol 总和 === 9026', async () => { + const long = '国'.repeat(60); + const body = `| ${long} | ${long} | ${long} |\n| --- | --- | --- |\n| ${long} | ${long} | ${long} |\n`; + const out = await unpackDocx(toDocx(pkg(body))); + const doc = dec.decode(out.get('word/document.xml')!); + const sum = gridColSum(doc); + assert.equal(sum, 9026, `长内容压缩表格 gridCol 总和应为 9026,实际 ${sum}`); +}); + +test('回归#13-4 混合文档:短表格+长表格各一,每个 gridCol 总和 === 9026', async () => { + const long = '字'.repeat(60); + const body = [ + '| 短A | 短B |', + '| --- | --- |', + '| 甲 | 乙 |', + '', + `| ${long} | ${long} |`, + '| --- | --- |', + `| ${long} | ${long} |`, + ].join('\n'); + const out = await unpackDocx(toDocx(pkg(body))); + const doc = dec.decode(out.get('word/document.xml')!); + const re = /([\s\S]*?)<\/w:tblGrid>/g; + let m: RegExpExecArray | null; + let idx = 0; + while ((m = re.exec(doc)) !== null) { + const colRe = /gridCol w:w="(\d+)"/g; + let s = 0; + let c: RegExpExecArray | null; + while ((c = colRe.exec(m[1])) !== null) s += Number(c[1]); + assert.equal(s, 9026, `表格${idx + 1} gridCol 总和应为 9026,实际 ${s}`); + idx++; + } + assert.equal(idx, 2, `应有 2 个表格,实际 ${idx}`); +}); + +// 回归#13-5 触底保护:12 列极短内容表格,每列 ≥800 且总和不被扣破 +test('回归#13-5 触底保护:12 列极短内容表格,每列 ≥800 且总和不被扣破', async () => { + const body = '| A | B | C | D | E | F | G | H | I | J | K | L |\n' + + '| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n' + + '| 甲 | 乙 | 丙 | 丁 | 戊 | 己 | 庚 | 辛 | 壬 | 癸 | 子 | 丑 |\n'; + const out = await unpackDocx(toDocx(pkg(body))); + const doc = dec.decode(out.get('word/document.xml')!); + const cols = gridColValues(doc); + assert.equal(cols.length, 12, `应有 12 列,实际 ${cols.length}`); + for (const w of cols) { + assert.ok(w >= 800, `每列应 ≥800(MIN_COL_W),实际 ${w}`); + } + const sum = cols.reduce((a, b) => a + b, 0); + assert.ok(sum >= 9026, `总和应 ≥9026(触底保护生效,宁可超宽也不扣破 min),实际 ${sum}`); }); \ No newline at end of file