diff --git a/NEWS.md b/NEWS.md index f80bbe9d..170ac4d2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -43,6 +43,10 @@ * `ansi_collapse(style = "head")` no longer exceeds `width` when not even the first element fits (#570, @taekop). +* `{.url}` with a substituted value, e.g. `{.url https://example.com/{x}}`, + no longer puts ANSI escape sequences into the hyperlink target (#757, + @taekop). + * `cli_progress_update(set = )` no longer fails with a deferred `on.exit()` error when the progress bar is drawn with a value below zero or above the total (#580, @taekop). diff --git a/R/ansi-hyperlink.R b/R/ansi-hyperlink.R index ae4054cb..f2c1f6a3 100644 --- a/R/ansi-hyperlink.R +++ b/R/ansi-hyperlink.R @@ -310,7 +310,7 @@ make_link_url <- function(txt) { if (all(linked)) { return(txt) } - txt[!linked] <- style_hyperlink(txt[!linked], txt[!linked]) + txt[!linked] <- style_hyperlink(txt[!linked], ansi_strip(txt[!linked])) txt } diff --git a/tests/testthat/test-links.R b/tests/testthat/test-links.R index 23f00870..49b9fc08 100644 --- a/tests/testthat/test-links.R +++ b/tests/testthat/test-links.R @@ -299,6 +299,18 @@ test_that("make_link_url", { expect_equal(make_link_url(x), x) }) +test_that("{.url} with a substitution has no ANSI in the link target", { + withr::local_options( + cli.hyperlink = TRUE, + cli.ansi = TRUE, + cli.num_colors = 256 + ) + test <- "path" + out <- format_inline("{.url https://example.com/{test}}") + target <- sub("^.*?\033\\]8;;([^\a]*)\a.*$", "\\1", out, perl = TRUE) + expect_equal(target, "https://example.com/path") +}) + # -- {.vignette} ---------------------------------------------------------- test_that_cli(configs = "plain", links = c("all", "none"), "{.vignette}", {