diff --git a/src/ansi.c b/src/ansi.c index fc5b2665..7bb7ab66 100644 --- a/src/ansi.c +++ b/src/ansi.c @@ -940,7 +940,13 @@ static void clic__html_start(struct html_data *data) { static void clic__html_end(struct html_data *data) { struct cli_buffer *buffer = &data->buffer; - if (data->had_tags) EMITS(""); + if (data->had_tags) { + EMITS(""); + /* Reset old state after closing tags, so the next text segment + correctly detects that it needs to open a new span, even if + the same style was reset and re-applied (issue #752). */ + memset(&data->state.old, 0, sizeof(data->state.old)); + } if (data->is_link) EMITS(""); } diff --git a/tests/testthat/test-ansi-html.R b/tests/testthat/test-ansi-html.R index f9dc8fe8..b4aa4e48 100644 --- a/tests/testthat/test-ansi-html.R +++ b/tests/testthat/test-ansi-html.R @@ -83,3 +83,16 @@ test_that("ansi_html_style", { ansi_html_style(colors = 256, palette = "ubuntu") ) }) + +# https://github.com/r-lib/cli/issues/752 +test_that("consecutive strings with the same style pasted together", { + x <- "\033[91mR\033[39m\033[91mR\033[39m" + expect_equal( + ansi_html(x), + "RR" + ) + expect_equal( + ansi_html(ansi_simplify(x)), + "RR" + ) +})