Skip to content

Commit 3ad47be

Browse files
authored
Fix accept_table with incomplete rows (#1599)
`header.zip(*body)` pads shorter rows with nil, causing NoMethodError in column width calculation. Also fix body rendering to output the correct number of columns for short rows instead of silently dropping them.
1 parent 23bccee commit 3ad47be

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

lib/rdoc/markup/to_rdoc.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def accept_table(header, body, aligns)
243243
header = header.map { |h| attributes h }
244244
body = body.map { |row| row.map { |t| attributes t } }
245245
widths = header.zip(*body).map do |cols|
246-
cols.map { |col| calculate_text_width(col) }.max
246+
cols.compact.map { |col| calculate_text_width(col) }.max
247247
end
248248
aligns = aligns.map do |a|
249249
case a
@@ -261,7 +261,8 @@ def accept_table(header, body, aligns)
261261
end.join("|").rstrip << "\n"
262262
@res << widths.map {|w| "-" * w }.join("|") << "\n"
263263
body.each do |row|
264-
@res << row.zip(widths, aligns).map do |t, w, a|
264+
@res << widths.zip(aligns).each_with_index.map do |(w, a), i|
265+
t = row[i] || ""
265266
extra_width = t.size - calculate_text_width(t)
266267
t.__send__(a, w + extra_width)
267268
end.join("|").rstrip << "\n"

test/rdoc/markup/to_ansi_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ def list_verbatim
348348
assert_equal expected, @to.end_accepting
349349
end
350350

351+
def accept_table_ragged_rows
352+
expected = "\e[0m" + <<-EXPECTED
353+
Name|Description
354+
----|---------------
355+
foo |Foo description
356+
bar |
357+
EXPECTED
358+
assert_equal expected, @to.end_accepting
359+
end
360+
351361
def accept_table_align
352362
expected = "\e[0m" + <<-EXPECTED
353363
AA |BB |CCCCC|DDDDD

test/rdoc/markup/to_bs_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ def list_verbatim
349349
assert_equal expected, @to.end_accepting
350350
end
351351

352+
def accept_table_ragged_rows
353+
expected = <<-EXPECTED
354+
Name|Description
355+
----|---------------
356+
foo |Foo description
357+
bar |
358+
EXPECTED
359+
assert_equal expected, @to.end_accepting
360+
end
361+
352362
def accept_table_align
353363
expected = <<-EXPECTED
354364
AA |BB |CCCCC|DDDDD

test/rdoc/markup/to_markdown_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,16 @@ def list_verbatim
346346
assert_equal expected, @to.end_accepting
347347
end
348348

349+
def accept_table_ragged_rows
350+
expected = <<-EXPECTED
351+
Name|Description
352+
----|---------------
353+
foo |Foo description
354+
bar |
355+
EXPECTED
356+
assert_equal expected, @to.end_accepting
357+
end
358+
349359
def accept_table_align
350360
expected = <<-EXPECTED
351361
AA |BB |CCCCC|DDDDD

test/rdoc/markup/to_rdoc_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,16 @@ def list_verbatim
375375
assert_equal expected, @to.end_accepting
376376
end
377377

378+
def accept_table_ragged_rows
379+
expected = <<-EXPECTED
380+
Name|Description
381+
----|---------------
382+
foo |Foo description
383+
bar |
384+
EXPECTED
385+
assert_equal expected, @to.end_accepting
386+
end
387+
378388
def accept_table_align
379389
expected = <<-EXPECTED
380390
AA |BB | CCCCC| DDDDD

test/rdoc/support/text_formatter_test_case.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ def test_accept_table_align
116116
accept_table_align
117117
end
118118

119+
##
120+
# Test case that calls <tt>@to.accept_table</tt> with body rows
121+
# that have fewer columns than the header
122+
123+
def test_accept_table_ragged_rows
124+
header = ['Name', 'Description']
125+
body = [
126+
['foo', 'Foo description'],
127+
['bar'],
128+
]
129+
aligns = [:left, :left]
130+
@to.start_accepting
131+
@to.accept_table header, body, aligns
132+
133+
accept_table_ragged_rows
134+
end
135+
119136
##
120137
# Test case that calls <tt>@to.attributes</tt> with an escaped
121138
# cross-reference. If this test doesn't pass something may be very

0 commit comments

Comments
 (0)