Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions web/src/editor/paste/cleaner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,33 @@ describe("字号:span[data-font-size] 载具保留与清洗", () => {
)
})
})

/**
* 表格列宽解析的精确匹配:`min-width` / `max-width` 不是列宽,不得被归一成 width 属性。
* 与 table-columns.ts 的 readColgroupWidths 同一条口径——未设列宽的站内表格序列化成
* `<col style="min-width: 25px">`,若被误读成 25px,粘贴进编辑态就会钉成窄列。
*/
describe("表格列宽:只认独立的 width 声明", () => {
const cols = (style: string) =>
cleanConfluenceHtml(
`<table><colgroup><col style="${style}"></colgroup><tbody><tr><td>a</td></tr></tbody></table>`
)

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=/)
})
})
3 changes: 2 additions & 1 deletion web/src/editor/paste/cleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
36 changes: 36 additions & 0 deletions web/src/editor/paste/roundtrip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<table><colgroup><col style="min-width: 25px"><col style="min-width: 25px"><col style="min-width: 25px"></colgroup>' +
"<tbody><tr><td>a</td><td>bb</td><td>ccc</td></tr></tbody></table>"
)
)
)
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)。
Expand Down
63 changes: 63 additions & 0 deletions web/src/lib/table-columns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,40 @@ describe("colgroup → 单元格 colwidth", () => {
expect(html).toContain('colwidth="240"')
})

it("未设列宽的 colgroup(仅 min-width: 25px)不产生 colwidth", () => {
const html = applyColWidthsFromColgroup(
table(
'<col style="min-width: 25px"><col style="min-width: 25px"><col style="min-width: 25px">',
"<tr><th>a</th><th>b</th><th>c</th></tr>"
)
)
expect(html).not.toContain("colwidth")
})

it("min-width 与真实 width 混合时只取 width", () => {
const html = applyColWidthsFromColgroup(
table(
'<col style="min-width: 25px; width: 285px"><col style="min-width: 25px">',
"<tr><th>a</th><th>b</th></tr>"
)
)
// 第一列只认 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(
'<col style="max-width: 120px"><col style="max-width: 240px">',
"<tr><th>a</th><th>b</th></tr>"
)
)
expect(html).not.toContain("colwidth")
})

it("没有 colgroup 的表格原样返回", () => {
const source = "<table><tbody><tr><th>a</th></tr></tbody></table>"
expect(applyColWidthsFromColgroup(source)).toBe(source)
Expand Down Expand Up @@ -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
Expand Down
46 changes: 35 additions & 11 deletions web/src/lib/table-columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 不得被读成列宽。
// 否则未设列宽的 `<col style="min-width: 25px">` 会被误读成 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)
Expand Down Expand Up @@ -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。
*
Expand Down