From 9bba92aa493d25471dc7d8ca29549597081cbc67 Mon Sep 17 00:00:00 2001 From: mapxn <16299874+mapxn@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:54:25 +0800 Subject: [PATCH] chore: checkpoint co8eW_C1VXlx-DywfJtzV --- web/src/editor/paste/cleaner.test.ts | 30 ++++++++++++ web/src/editor/paste/cleaner.ts | 3 +- web/src/editor/paste/roundtrip.test.ts | 36 +++++++++++++++ web/src/lib/table-columns.test.ts | 63 ++++++++++++++++++++++++++ web/src/lib/table-columns.ts | 46 ++++++++++++++----- 5 files changed, 166 insertions(+), 12 deletions(-) diff --git a/web/src/editor/paste/cleaner.test.ts b/web/src/editor/paste/cleaner.test.ts index 02fab46..582330e 100644 --- a/web/src/editor/paste/cleaner.test.ts +++ b/web/src/editor/paste/cleaner.test.ts @@ -359,3 +359,33 @@ describe("字号:span[data-font-size] 载具保留与清洗", () => { ) }) }) + +/** + * 表格列宽解析的精确匹配:`min-width` / `max-width` 不是列宽,不得被归一成 width 属性。 + * 与 table-columns.ts 的 readColgroupWidths 同一条口径——未设列宽的站内表格序列化成 + * ``,若被误读成 25px,粘贴进编辑态就会钉成窄列。 + */ +describe("表格列宽:只认独立的 width 声明", () => { + const cols = (style: string) => + cleanConfluenceHtml( + `
a
` + ) + + it("仅 min-width 的列不产生 width 属性", () => { + const cleaned = cols("min-width: 25px") + expect(cleaned).not.toContain('width="25px"') + expect(cleaned).not.toMatch(/col[^>]*style=/) + }) + + it("min-width 与真实 width 混合时只保留 width", () => { + const cleaned = cols("min-width: 25px; width: 285px") + expect(cleaned).toContain('width="285px"') + expect(cleaned).not.toContain("25px") + }) + + it("max-width 不被读成列宽", () => { + const cleaned = cols("max-width: 120px") + expect(cleaned).not.toContain('width="120px"') + expect(cleaned).not.toMatch(/col[^>]*style=/) + }) +}) diff --git a/web/src/editor/paste/cleaner.ts b/web/src/editor/paste/cleaner.ts index dba007d..5fc66dd 100644 --- a/web/src/editor/paste/cleaner.ts +++ b/web/src/editor/paste/cleaner.ts @@ -220,9 +220,10 @@ export function cleanConfluenceHtml(html: string): string { // 6) 表格:保留 colgroup 列宽(v0.4,表格不应默认撑满行宽): // 把 col 的 style width 归一为 width 属性,只接受 px / % 数值。 + // 只认独立的 width: 声明(行首或分号之后),min-width / max-width 不得被读成列宽。 root.querySelectorAll("col").forEach((col) => { const style = col.getAttribute("style") || "" - const match = style.match(/width\s*:\s*([\d.]+(?:px|%))/i) + const match = style.match(/(?:^|;)\s*width\s*:\s*([\d.]+(?:px|%))/i) const width = col.getAttribute("width") || (match ? match[1] : "") if (/^[\d.]+(px|%)$/.test(width)) col.setAttribute("width", width) else col.removeAttribute("width") diff --git a/web/src/editor/paste/roundtrip.test.ts b/web/src/editor/paste/roundtrip.test.ts index 58f5004..e5befd8 100644 --- a/web/src/editor/paste/roundtrip.test.ts +++ b/web/src/editor/paste/roundtrip.test.ts @@ -161,6 +161,42 @@ describe("清洗后 TipTap 回读(小样 v0.4 断言)", () => { }) }) +/** + * 未设列宽的站内表格(colgroup 只有 `min-width: 25px`)被复制粘贴回来时,粘贴管线 + * 不得把 min-width 误读成列宽。走的是与读态共用的 `applyColWidthsFromColgroup`。 + */ +describe("未设列宽的站内表格粘贴:min-width 不是列宽", () => { + let editor: Editor + + beforeAll(() => { + installTiptapJsdomShims() + const host = document.createElement("div") + document.body.appendChild(host) + editor = new Editor({ element: host, extensions: createExtensions("edit") }) + }) + + afterAll(() => { + editor?.destroy() + }) + + it("仅 min-width 的 colgroup 回读后单元格没有 colwidth", () => { + editor.commands.setContent( + applyColWidthsFromColgroup( + cleanConfluenceHtml( + '' + + "
abbccc
" + ) + ) + ) + const table = docJson(editor).content?.find((node) => node.type === "table") + const cells = table?.content?.[0]?.content ?? [] + expect(cells).toHaveLength(3) + for (const cell of cells) { + expect(cell.attrs?.colwidth).toBeNull() + } + }) +}) + /** * 颜色支持批次:清洗后的颜色载具必须能被产品侧真实扩展集读回模型, * 并在序列化时保持契约形态(data 属性 + 小写 #rrggbb)。 diff --git a/web/src/lib/table-columns.test.ts b/web/src/lib/table-columns.test.ts index 7b34456..d8646f8 100644 --- a/web/src/lib/table-columns.test.ts +++ b/web/src/lib/table-columns.test.ts @@ -78,6 +78,40 @@ describe("colgroup → 单元格 colwidth", () => { expect(html).toContain('colwidth="240"') }) + it("未设列宽的 colgroup(仅 min-width: 25px)不产生 colwidth", () => { + const html = applyColWidthsFromColgroup( + table( + '', + "abc" + ) + ) + expect(html).not.toContain("colwidth") + }) + + it("min-width 与真实 width 混合时只取 width", () => { + const html = applyColWidthsFromColgroup( + table( + '', + "ab" + ) + ) + // 第一列只认 width: 285px;第二列没有真实宽度,按已知列均值补齐(= 285)。 + // 两个单元格都是 285,绝不能出现被误读成 25 的 colwidth。 + expect(html).toContain('colwidth="285"') + expect(html).not.toContain('colwidth="25"') + expect(html.match(/colwidth="285"/g)).toHaveLength(2) + }) + + it("max-width 不被读成列宽", () => { + const html = applyColWidthsFromColgroup( + table( + '', + "ab" + ) + ) + expect(html).not.toContain("colwidth") + }) + it("没有 colgroup 的表格原样返回", () => { const source = "
a
" expect(applyColWidthsFromColgroup(source)).toBe(source) @@ -188,6 +222,35 @@ describe("stripSingleColumnColwidths(编辑态 JSON 归一)", () => { expect(cells[1]?.attrs?.colwidth).toEqual([200]) }) + it("多列表格全列都是默认最小宽 25 → 视为未设列宽,剥掉 colwidth", () => { + const next = stripSingleColumnColwidths( + doc(table([[cell([25]), cell([25]), cell([25])]])) as never + ) + const cells = next.content?.[0]?.content?.[0]?.content ?? [] + for (const c of cells) { + expect(c.attrs).toEqual({ colspan: 1, rowspan: 1 }) + expect(c.attrs).not.toHaveProperty("colwidth") + } + }) + + it("多列表格 25 与真实宽度混合 → 保留(只归一全 25)", () => { + const next = stripSingleColumnColwidths( + doc(table([[cell([25]), cell([285])]])) as never + ) + const cells = next.content?.[0]?.content?.[0]?.content ?? [] + expect(cells[0]?.attrs?.colwidth).toEqual([25]) + expect(cells[1]?.attrs?.colwidth).toEqual([285]) + }) + + it("colspan=2 且两段都 25 → 视为未设列宽,剥掉 colwidth", () => { + const next = stripSingleColumnColwidths( + doc(table([[cell([25, 25], "合计", 2)]])) as never + ) + const attrs = next.content?.[0]?.content?.[0]?.content?.[0]?.attrs + expect(attrs).toEqual({ colspan: 2, rowspan: 1 }) + expect(attrs).not.toHaveProperty("colwidth") + }) + it("colspan=2 的单单元格表格不当单列", () => { const next = stripSingleColumnColwidths( doc(table([[cell([120, 200], "合计", 2)]])) as never diff --git a/web/src/lib/table-columns.ts b/web/src/lib/table-columns.ts index 0cb97fa..283a7bb 100644 --- a/web/src/lib/table-columns.ts +++ b/web/src/lib/table-columns.ts @@ -73,7 +73,9 @@ function readColgroupWidths( const width = col.getAttribute("width") if (width) return width const style = col.getAttribute("style") || "" - const match = style.match(/width\s*:\s*([\d.]+\s*(?:px|%))/i) + // 只认独立的 width: 声明(行首或分号之后),min-width / max-width 不得被读成列宽。 + // 否则未设列宽的 `` 会被误读成 25px。 + const match = style.match(/(?:^|;)\s*width\s*:\s*([\d.]+\s*(?:px|%))/i) return match ? match[1].replace(/\s+/g, "") : null }) const widths = resolveColumnWidths(raws, baseWidth) @@ -174,20 +176,42 @@ function stripColwidth(node: JSONContent): JSONContent { if (next.type !== "table") return next // 列数按首行算(与 HTML 侧同一判据);colspan=2 的单单元格表格算两列,不动 - if (rowColumnCount(next.content?.[0]) === 1 && next.content) { - next.content = next.content.map((row) => ({ - ...row, - content: row.content?.map((cell) => { - if (!cell.attrs || !("colwidth" in cell.attrs)) return cell - const attrs = { ...cell.attrs } - delete attrs.colwidth - return { ...cell, attrs } - }) - })) + if (rowColumnCount(next.content?.[0]) === 1 || allDefaultMinWidth(next)) { + return { + ...next, + content: next.content?.map((row) => ({ + ...row, + content: row.content?.map((cell) => { + if (!cell.attrs || !("colwidth" in cell.attrs)) return cell + const attrs = { ...cell.attrs } + delete attrs.colwidth + return { ...cell, attrs } + }) + })) + } } return next } +/** + * 多列表格里,首行每个单元格的 colwidth 摊平后全为默认最小宽 25(旧正则把 + * `min-width: 25px` 误读成列宽 25 的产物,经粘贴/HTML 路径落进 contentJson 后 + * 再保存留下的存量)。这种表格同样视为「未设列宽」,剥掉全部 colwidth。 + */ +function allDefaultMinWidth(table: JSONContent): boolean { + const firstRowCells = (table.content?.[0]?.content ?? []).filter( + (node) => node.type === "tableCell" || node.type === "tableHeader" + ) + if (!firstRowCells.length) return false + const widths: unknown[] = [] + for (const cell of firstRowCells) { + const colwidth = cell.attrs?.colwidth + if (!Array.isArray(colwidth) || colwidth.length === 0) return false + widths.push(...colwidth) + } + return widths.length > 0 && widths.every((value) => value === 25) +} + /** * 编辑态用的归一:JSON 里单列表格(列数=1)不保留任何 colwidth。 *