tests/testthat/helper-validate_html.R

# the HTML validation server was started in GHA
validate_html = function(files, server = Sys.getenv('W3C_MARKUP_VALIDATOR_BASEURL')) {
  if (server == '') return()
  res = lapply(files, function(f) {
    h = curl::new_handle()
    curl::handle_setform(handle = h, out = 'json', file = curl::form_file(f, 'text/html'))
    jsonlite::fromJSON(rawToChar(curl::curl_fetch_memory(server, h)$content))
  })
  expected_errors = c(
    'Attribute “number” not allowed on element “div” at this point.',
    'CSS: “border-top”: “solid\\9” is not a “color” value.',
    'CSS: “border-bottom”: “solid\\9” is not a “color” value.'
  )
  res = do.call(rbind, lapply(res, function(x) {
    m <- x$messages$message[x$messages$type == 'error']
    m <- setdiff(m, expected_errors)
    if (length(m)) data.frame(file = x$url, messages = m)
  }))
  if (NROW(res) > 0) stop(
    'HTML issues detected:\n',
    paste0('  ', res$file, ': ', res$messages, collapse = '\n')
  )
}

Try the bookdown package in your browser

Any scripts or data that you put into this service are public.

bookdown documentation built on April 15, 2025, 5:09 p.m.