mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Refuse a pipe table whose padding would add more cells than the table holds
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user