From 69393e8e66ae0d5e510ad7e5980652be5f468e56 Mon Sep 17 00:00:00 2001 From: "Trevor L. Davis" Date: Sat, 14 Feb 2026 11:06:48 -0800 Subject: [PATCH] fix `ansi_html()` when same same style repeated After closing the span in `html_cb_text()`, reset `state->old` so the next text segment correctly detects it needs styling. closes #752 Co-Authored-By: Claude Opus 4.6 --- src/ansi.c | 8 +++++++- tests/testthat/test-ansi-html.R | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ansi.c b/src/ansi.c index fc5b2665e..7bb7ab666 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 f9dc8fe85..b4aa4e485 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" + ) +})