From 80b70c42fb43775f28650290942ec26fdb24125e Mon Sep 17 00:00:00 2001 From: taekop Date: Mon, 5 Oct 2026 13:31:57 +0900 Subject: [PATCH] Strip ANSI from the target of {.url} hyperlinks --- NEWS.md | 4 ++++ R/ansi-hyperlink.R | 2 +- tests/testthat/test-links.R | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 2731f85b2..ccd41b5b3 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 3.6.6 * New `{.num}` and `{.bytes}` inline styles to format numbers diff --git a/R/ansi-hyperlink.R b/R/ansi-hyperlink.R index ae4054cb4..f2c1f6a33 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 23f00870d..49b9fc08e 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}", {