Skip to content

Commit

Permalink
Fix html table class tweaking (#912)
Browse files Browse the repository at this point in the history
Closes #868. Fixes #910.
  • Loading branch information
jayhesselberth committed Nov 21, 2018
1 parent 7b21c86 commit 44460e6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# pkgdown 1.2.0.9000

* Tweaking of HTML table classes was fixed (@yonicd, #912)

* Restore accidentally removed `docsearch.css` file.

# pkgdown 1.2.0
Expand Down
2 changes: 1 addition & 1 deletion R/html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ tweak_tables <- function(html) {
}

prepend_class <- function(x, class = "table") {
if (length(x) == 0) {
if (!('class' %in% names(x))) {
c(class = class)
} else {
c(class = paste(class, x[["class"]]))
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-html-tweak.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ test_that("multiple tables with existing classes are prepended", {
expect_equal(c("table a", "table b", "table"))
})

test_that("multiple tables with existing classes are prepended and attributes", {
html <- xml2::read_html(
'<body>
<table style="width:100%;" class="a"></table>
<table class="b"></table>
<table></table>
</body>'
)
expect_silent(tweak_tables(html))
html %>%
xml2::xml_find_all(".//table") %>%
xml2::xml_attr("class") %>%
expect_equal(c("table a", "table b", "table"))
})

test_that("tables get class='table' prepended to existing classes", {
html <- xml2::read_html("<body><table class = 'foo bar'>\n</table></body>")
tweak_tables(html)
Expand Down
8 changes: 8 additions & 0 deletions vignettes/test/table.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
title: "Test: Table"
output: html_document
---

| col 1 | col 2 | col 3 | col 4 |
|:---------------|:-----------|:-------:|:---------:|
| Brightness | Total brightness, total reflectance, spectral intensity | $$y = x^2$$ | test |

0 comments on commit 44460e6

Please sign in to comment.