Skip to content

Commit

Permalink
(Un)escape URLs when reading/writing <img src=> attributes (#2711)
Browse files Browse the repository at this point in the history
Fixes #2705
  • Loading branch information
HenningLorenzen-ext-bayer authored Aug 2, 2024
1 parent 73dd873 commit 502c3e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/check-built.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ check_missing_images <- function(pkg, src_path, dst_path) {
img <- xml2::xml_find_all(html, ".//img")
src <- xml2::xml_attr(img, "src")

rel_src <- src[xml2::url_parse(src)$scheme == ""]
rel_src <- xml2::url_unescape(src[xml2::url_parse(src)$scheme == ""])
rel_path <- path_norm(path(path_dir(dst_path), rel_src))
exists <- file_exists(path(pkg$dst_path, rel_path))

Expand Down
12 changes: 9 additions & 3 deletions R/tweak-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ tweak_rmarkdown_html <- function(html, input_path, pkg = list(bs_version = 3)) {
src <- xml2::xml_attr(img, "src")
abs_src <- is_absolute_path(src)
if (any(abs_src)) {
img_target_nodes <- img[abs_src]
img_src_real <- path_real(xml2::url_unescape(src[abs_src]))
input_path_real <- path_real(xml2::url_unescape(input_path))
img_rel_paths <- path_rel(path = img_src_real, start = input_path_real)
img_rel_paths <- xml2::url_escape(img_rel_paths)

purrr::walk2(
img[abs_src],
path_rel(path_real(src[abs_src]), path_real(input_path)),
xml2::xml_set_attr,
.x = img_target_nodes,
.y = img_rel_paths,
.f = xml2::xml_set_attr,
attr = "src"
)
}
Expand Down
21 changes: 18 additions & 3 deletions tests/testthat/test-build-article.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test_that("BS5 article gets correctly activated navbar", {

html <- xml2::read_html(article_path)
navbar <- xml2::xml_find_first(html, ".//div[contains(@class, 'navbar')]")

expect_equal(
xpath_text(navbar,".//li[contains(@class, 'active')]//button"),
"Articles"
Expand All @@ -104,7 +104,7 @@ test_that("output is reproducible by default, i.e. 'seed' is respected", {
r_code_block("runif(5L)")
))
suppressMessages(path <- build_article("test", pkg))

html <- xml2::read_html(path)
output <- xpath_text(html, "//main//pre")[[2]]
expect_snapshot(cat(output))
Expand Down Expand Up @@ -133,7 +133,7 @@ test_that("can control math mode", {
suppressMessages(path <- build_article("math", pkg))
html <- xml2::read_html(path)
expect_equal(xpath_length(html, ".//span[contains(@class, 'math')]"), 1)

pkg$meta$template$`math-rendering` <- "katex"
suppressMessages(init_site(pkg))
suppressMessages(path <- build_article("math", pkg))
Expand Down Expand Up @@ -276,6 +276,21 @@ test_that("warns about missing images", {
expect_snapshot(build_article("kitten", pkg))
})

test_that("spaces in sorce paths do work", {
# create simulated package
pkg0 <- local_pkgdown_site()
pkg0 <- pkg_add_file(pkg0, "vignettes/kitten.Rmd", "![Kitten](kitten.jpg)")
pkg0 <- pkg_add_kitten(pkg0, "vignettes")

# copy simulated pkg to path that contains spaces
pkg1 <- fs::dir_copy(pkg0$src_path, fs::file_temp(pattern = "beware of spaces-"))

# check that pkgdown site builds anyways
expect_no_error(suppressMessages(
build_article("kitten", as_pkgdown(pkg1))
))
})

test_that("warns about missing alt-text", {
pkg <- local_pkgdown_site()
pkg <- pkg_add_file(pkg, "vignettes/kitten.Rmd", "![](kitten.jpg)")
Expand Down

0 comments on commit 502c3e2

Please sign in to comment.