Skip to content

Commit

Permalink
add test case for complex rowspan+colspan
Browse files Browse the repository at this point in the history
  • Loading branch information
siuying committed Jan 26, 2010
1 parent 5437ced commit 608686d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/table_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
require 'table_parser/parser'

module TableParser
VERSION = '0.5.6'
VERSION = '0.5.7'
end
6 changes: 5 additions & 1 deletion lib/table_parser/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ def self.extract_nodes(rows, headers, dup_rows, dup_cols)
end
end

if col.rowspan > 1 && data[row_index+1]
if col.rowspan > 1
unless data[row_index+1]
data.insert(row_index, [])
end

if dup_rows
data[row_index+1].insert(col_index, TableNode.new(col.element, col.rowspan - 1))
else
Expand Down
20 changes: 20 additions & 0 deletions test/test_table_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,31 @@ def test_parse_noheader
assert_equal(3, table[1].size)
end

def test_parse_complex_colrowspan
html = "<html><body><table><tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>\
<tr><td rowspan=\"5\">1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr> \
<tr><td rowspan=\"2\">2b</td><td>3d</td><td>4d</td><td>5b</td></tr> \
<tr><td>3c</td><td rowspan=\"2\">4c</td><td>5c</td></tr>\
<tr><td rowspan=\"2\">2d</td><td>3d</td><td>5d</td></tr>\
<tr><td>3e</td><td>4e</td><td>5e</td></tr>\
</table></body></html>"

doc = Nokogiri::HTML(html)
table = TableParser::Table.new doc, "/html/body/table", {:dup_cols => false, :dup_rows => false}
puts table
assert_equal(5, table.columns.size, 'header_count should = 5 ')
assert_equal(1, table[0].size)
assert_equal(3, table[1].size)
assert_equal(5, table[2].size)
assert_equal(4, table[3].size)
assert_equal(5, table[4].size)
end

def test_web
html = open("test4.html").read
doc = Nokogiri::HTML::Document.parse(html, nil, "Shift_JIS")
table = TableParser::Table.new doc, "/html/body/div/div[3]/div/div[2]/table", {:header => false, :dup_rows => false}
puts table.columns[0].size
end

end

0 comments on commit 608686d

Please sign in to comment.