import assert from "node:assert/strict"; import test from "node:test"; import { alignTableCells, clipboardCells, copyTableCells, deleteTableCells, formatTableCells, mapTableCells, pasteTableCells } from "../src/lib/editor/tableSelection.ts"; import { parseMarkdownTable } from "../src/lib/editor/markdownTable.ts"; import { renderMarkdownHtml, plainInlineMarkdown } from "../src/lib/workspace/noteHtml.ts"; import { tableCellMarkdown } from "../src/lib/editor/tableCellMarkdown.ts"; const source = "| A | B | C |\r\n| --- | :---: | ---: |\r\n| one | two | three |\r\n| four | five | six |\r\n\r\nTail"; const range = { anchor: { row: 2, column: 1 }, head: { row: 1, column: 0 } }; test("reversed rectangular selections only change selected cells and preserve CRLF", () => { const changed = formatTableCells(source, range, "bold"); assert.equal(changed, source.replace("one", "**one**").replace("two", "**two**").replace("four", "**four**").replace("five", "**five**")); assert.equal(formatTableCells(changed, range, "bold"), source); assert.equal(formatTableCells(source.replace("one", "**one**"), range, "bold"), changed); assert.equal(copyTableCells(source, range), "one\ttwo\nfour\tfive"); assert.equal(alignTableCells(source, range, "left"), source.replace("| --- | :---: | ---: |", "| :--- | :--- | ---: |")); }); test("all formats render and clear without interpreting escaped punctuation", () => { const cell = { anchor: { row: 1, column: 0 }, head: { row: 1, column: 0 } }; for (const [format, html] of [["bold", "one"], ["italic", "one"], ["strike", "one"], ["highlight", "one"], ["code", "one"], ["math", 'class="md-inline-math">one'], ["comment", 'class="md-inline-comment">one']] as const) { const changed = formatTableCells(source, cell, format); assert.ok(renderMarkdownHtml(changed, new Map()).includes(html), format); assert.equal(formatTableCells(changed, cell, "clear"), source); } assert.equal(plainInlineMarkdown("**bold** ==*highlight*== ~~strike~~ `a|b`
\\*literal\\*"), "bold highlight strike a\\|b
\\*literal\\*"); assert.equal(plainInlineMarkdown("**Photo** ![alt](image.png)"), "Photo ![alt]()"); assert.ok(renderMarkdownHtml("`==not highlighted==` \\==literal\\==", new Map()).includes("==not highlighted==")); for (const type of ["highlight", "math", "comment"]) { const text = tableCellMarkdown({ type: "text", text: "a|b", marks: [{ type }] }); assert.equal(parseMarkdownTable(`| H |\n| --- |\n| ${text} |`)?.rows[2].cells.length, 1); } }); test("clipboard accepts Markdown and quoted TSV without splitting escaped pipes", () => { assert.deepEqual(clipboardCells('"a\tb"\t"line 1\nline 2"\r\n"say ""hi"""\tx|y\r\n'), [["a\tb", "line 1
line 2"], ['say "hi"', "x\\|y"]]); assert.deepEqual(clipboardCells("| A | B |\n| --- | --- |\n| a\\|b | `code` |"), [["A", "B"], ["a\\|b", "`code`"]]); const pasted = pasteTableCells(source, range, "value"); assert.equal(copyTableCells(pasted, range), "value\tvalue\nvalue\tvalue"); const large = pasteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 2, column: 2 } }, "a\tb\nc\td"); assert.equal(parseMarkdownTable(large)?.columns, 4); assert.equal(parseMarkdownTable(large)?.rows.length, 5); assert.ok(large.endsWith("\r\n\r\nTail")); assert.equal(copyTableCells(large, { anchor: { row: 2, column: 2 }, head: { row: 3, column: 3 } }), "a\tb\nc\td"); }); test("clear preserves structure; delete removes whole rows/columns or shifts partial cells", () => { assert.equal(copyTableCells(mapTableCells(source, range, () => ""), range), "\t\n\t"); assert.equal(copyTableCells(deleteTableCells(source, range), range), "three\t\nsix\t"); const rows = deleteTableCells(source, { anchor: { row: 1, column: 0 }, head: { row: 1, column: 2 } }); assert.equal(parseMarkdownTable(rows)?.rows.length, 3); assert.ok(rows.includes("four")); assert.ok(!rows.includes("one")); const columns = deleteTableCells(source, { anchor: { row: 0, column: 1 }, head: { row: 2, column: 1 } }); assert.equal(parseMarkdownTable(columns)?.columns, 2); assert.ok(columns.includes("| one | three |")); const header = deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 0, column: 2 } }); assert.ok(header.startsWith("| one | two | three |\r\n| --- | :---: | ---: |")); assert.equal(deleteTableCells(source, { anchor: { row: 0, column: 0 }, head: { row: 2, column: 2 } }), "\r\n\r\nTail"); }); test("deleting the empty columns shown on the card removes their structure", () => { const emptyColumns = "| Column 2 | Column 1 | | | | |\n| --- | --- | --- | --- | --- | --- |\n| 2cm | Value | | | | |\n| text | 123 | | | | |\n| | | | | | |\n| | | | | | |\n\nAfter."; const result = deleteTableCells(emptyColumns, { anchor: { row: 4, column: 5 }, head: { row: 0, column: 3 } }); assert.equal(parseMarkdownTable(result)?.columns, 3); assert.equal(parseMarkdownTable(result)?.rows.length, 6); assert.ok(result.includes("| 2cm | Value | |")); assert.ok(result.endsWith("\n\nAfter.")); }); test("deleting the last body rows retains a valid header and alignment", () => { const result = deleteTableCells(source, { anchor: { row: 2, column: 2 }, head: { row: 1, column: 0 } }); assert.equal(result, "| A | B | C |\r\n| --- | :---: | ---: |\r\n\r\nTail"); assert.equal(parseMarkdownTable(result)?.rows.length, 2); });