8000 `ansi_html()` bug when strings with the same style have been pasted together · Issue #752 · r-lib/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ansi_html() bug when strings with the same style have been pasted together #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
trevorld opened this issue Mar 13, 2025 · 1 comment
Labels
bug an unexpected problem or unintended behavior

Comments

@trevorld
Copy link

I'm observing an error with ansi_html() when strings with the same styles have been pasted together. An example:

library("cli")
asf <- combine_ansi_styles(bg_blue, col_br_yellow)
s <- paste(rep_len(asf(" "), 10L), collapse = "")
h_cli <- ansi_html(s)
print(h_cli)
[1] "<span class=\"ansi ansi-color-3 ansi-bg-color-4\"> </span>         "

Note only the first space is styled with the correct background color and the remaining nine spaces are unstyled. In contrast I believe the right output should instead be:

[1] "<span class=\"ansi ansi-color-3 ansi-bg-color-4\">          </span>"

(note the different placement of the </span>). In contrast fansi::sgr_to_html() seems to correctly preserve a style for all ten spaces:

h_fansi <- fansi::sgr_to_html(s)
print(h_fansi)
[1] "<span style='color: #FFFF55; background-color: #0000BB;'>          </span>"
@gaborcsardi gaborcsardi added the bug an unexpected problem or unintended behavior label Apr 25, 2025
@trevorld
Copy link
Author
trevorld commented May 9, 2025

A possible test case for tests/testthat/test-ansi-html.R:

# https://github.com/r-lib/cli/issues/752
expect_equal(
  ansi_html("\033[91mR\033[39m\033[91mR\033[39m"),
  "<span class=\"ansi ansi-color-1\">RR</span>"
)

where

> paste0(col_br_red("R"), col_br_red("R"))
[1] "\033[91mR\033[39m\033[91mR\033[39m"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants
0