3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 06:35:44 +01:00

Refuse a pipe table whose padding would add more cells than the table holds

This commit is contained in:
2026-11-10 17:03:53 +01:00
committed by Lillem4n
parent 3b61501fe2
commit 785388d426
6 changed files with 32 additions and 7 deletions
+9
View File
@@ -551,6 +551,15 @@ function pipeTableBlock(rows: readonly [string[], ...string[][]], position: Sour
const padsShortRows = claims.pipeTables === 'gfm'
const ragged = [delimiter, ...body].find((row) => (padsShortRows && row !== delimiter ? row.length > header.length : row.length !== header.length))
if (ragged !== undefined) return faultedBlock(`a pipe table row holds ${cellCount(ragged.length)} where its header holds ${cellCount(header.length)}`, position)
let held = header.length + delimiter.length
let padding = 0
for (const row of body) {
held += row.length
padding += header.length - row.length
}
if (padding > held) {
return faultedBlock(`a pipe table's short rows would pad ${cellCount(padding)}, more than the ${cellCount(held)} it holds: write the empty cells out`, position)
}
const padded = body.map((row) => [...row, ...Array.from({ length: header.length - row.length }, () => '')])
return { kind: 'table', position, rows: [header, ...padded] }
}
@@ -406,6 +406,12 @@ test('reads GFM\'s short row padded with empty cells, a line holding no pipe as
assert.deepEqual(read('a |\n:--- |\nb\n'), [column(['a', 'b'])])
assert.deepEqual(read('| a |\n| --- |\nb\n'), [column(['a', 'b'])])
assert.deepEqual(read('a\n--- |\n'), [said('a --- |')])
const wide = (rows: number): string => `a | b | c | d\n--- | --- | --- | ---\n${'x\n'.repeat(rows)}`
assert.ok(portableMarkdownToAdf(wide(4)).ok)
const padded = portableMarkdownToAdf(wide(5))
assert.deepEqual(padded.ok ? undefined : [padded.error.code, padded.error.position], ['malformed-pipe-table', { line: 1, offset: 0 }])
const amplified = portableMarkdownToAdf(`${'a|'.repeat(2000)}\n${'-|'.repeat(2000)}\n${'x\n'.repeat(2000)}`)
assert.deepEqual(amplified.ok ? undefined : amplified.error.code, 'malformed-pipe-table')
for (const refused of ['a | b\n--- | ---\nc | d | e\n', '| a | b |\n| --- |\n']) {
const result = portableMarkdownToAdf(refused)
assert.deepEqual(result.ok ? undefined : result.error.code, 'malformed-pipe-table', refused)