Add a class to the root node of an HTML string

add_class(html, class_name)

Arguments

html

Character. An HTML string.

class_name

Character. Class name to be added to the root node.

Value

Character. An HTML string with the class name added to the root node.

Examples

library(magrittr) html <- "<!-- function handles comments--><div><p>R is fun</div></p>" html %>% add_class("second-class")
#> [1] "<!-- function handles comments--><div class=\"second-class\"><p>R is fun</div></p>"
html %>% add_class(".ok-to-start-with-period")
#> [1] "<!-- function handles comments--><div class=\"ok-to-start-with-period\"><p>R is fun</div></p>"
html %>% add_class("you-can") %>% add_class("chain-function-calls-too")
#> [1] "<!-- function handles comments--><div class=\"you-can chain-function-calls-too\"><p>R is fun</div></p>"
html %>% add_class("or add five at once")
#> [1] "<!-- function handles comments--><div class=\"or add five at once\"><p>R is fun</div></p>"
html <- "<div class='using-single-quotes'><p>R is fun</div></p>" html %>% add_class("is-ok-too")
#> [1] "<div class='using-single-quotes is-ok-too'><p>R is fun</div></p>"
html <- "<div class = 'lots-of-spaces'><p>R is fun</div></p>" html %>% add_class("is-also-ok")
#> [1] "<div class = 'lots-of-spaces is-also-ok'><p>R is fun</div></p>"