Skip to content

Commit

Permalink
Gracefully handle tables with only empty rows (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
epiben authored Jan 23, 2024
1 parent f44b7bc commit b3b6916
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# rvest (development version)

* `html_table()` discards rows without cells (@epiben, #360).

# rvest 1.0.3

* Re-document to fix HTML issues in `.Rd`.
Expand Down
1 change: 1 addition & 0 deletions R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ html_table.xml_node <- function(x,
ns <- xml2::xml_ns(x)
rows <- xml2::xml_find_all(x, ".//tr", ns = ns)
cells <- lapply(rows, xml2::xml_find_all, ".//td|.//th", ns = ns)
cells <- compact(cells)

if (length(cells) == 0) {
return(tibble::tibble())
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@

# A tibble: 0 x 0

# can handle tables consisting of a single empty row

# A tibble: 0 x 0

# can handle tables consisting of only empty rows

# A tibble: 0 x 0

12 changes: 12 additions & 0 deletions tests/testthat/test-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,15 @@ test_that("can handle empty tables", {
table <- html_table(html)[[1]]
expect_snapshot_output(table)
})

test_that("can handle tables consisting of a single empty row", {
html <- minimal_html('<table><tr></tr></table>')
table <- html_table(html)[[1]]
expect_snapshot_output(table)
})

test_that("can handle tables consisting of only empty rows", {
html <- minimal_html('<table><tr></tr><tr></tr></table>')
table <- html_table(html)[[1]]
expect_snapshot_output(table)
})

0 comments on commit b3b6916

Please sign in to comment.