From ff5ab8ad4e1e59ce9e719928dc53a8ae4d297093 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 23:53:13 +0000 Subject: [PATCH 1/3] Write a tag over several runs as one element HtmlTagEmitter leaves out the open tag where the run before carries the same layer with the same layers outside it, and the close tag where the run after does. tagwrap(pre,...) over command links was one
 per run.

Co-Authored-By: Claude Opus 5.5 
Claude-Session: https://claude.ai/code/session_01Rqqt7SWqms8BzoJivqhW6r
---
 CHANGELOG.md                                 |  5 ++
 MarkupString.Html/Emitters/HtmlTagEmitter.cs | 51 ++++++++++++++++----
 MarkupString.Tests/Html/HtmlMarkupTests.cs   | 31 ++++++++++++
 3 files changed, 78 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e7b73c0..a9aabdf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,11 @@ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ### Fixed
 
+- An `HtmlMarkup` tag over text that an inner layer splits into runs is written as one element in HTML, Pueblo
+  and MXP. `tagwrap(pre,[cmdlink(aaa,look)][space(5)][cmdlink(bbb,look)])` was written as three `
`
+  elements, one per run, so a client drew three blocks on three lines. A tag still closes before an outer layer
+  it does not share, so the elements nest.
+
 - `MarkupText.ClearScreen()` is written in MXP as `ESC[H ESC[2J` when `WithAnsi()` is registered, in place of
   nothing: MXP has no clear of its own, and its clients already read SGR in the same stream. Pueblo keeps
   `` alone. [Formats](docs/formats.md#clearing-the-screen) has a table of which clients
diff --git a/MarkupString.Html/Emitters/HtmlTagEmitter.cs b/MarkupString.Html/Emitters/HtmlTagEmitter.cs
index e80f7ee..e46a514 100644
--- a/MarkupString.Html/Emitters/HtmlTagEmitter.cs
+++ b/MarkupString.Html/Emitters/HtmlTagEmitter.cs
@@ -13,6 +13,10 @@ namespace MarkupString.Html;
 /// Without a policy the tag name and attributes are written exactly as the markup carries them. With
 /// one, every tag is held to it as it is written — see  — and a tag it
 /// refuses leaves its body in place, unwrapped. The body is encoded by the renderer either way.
+/// A tag over several runs is one element: text split by an inner layer (a link inside <pre>)
+/// is written <pre><a>a</a> <a>b</a></pre>, not one <pre> per run. The open
+/// tag is left out where the run before carries the same layer with the same layers around it, and the close
+/// tag where the run after does, so the elements still nest.
 /// 
 public sealed class HtmlTagEmitter : IMarkupEmitter
 {
@@ -56,17 +60,46 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context
 			html = held;
 		}
 
-		output.Write("<");
-		output.Write(html.TagName);
-		if (html.Attributes is { Length: > 0 } attributes)
+		if (!Continues(markup, context.Markups, context.Previous))
 		{
-			output.Write(" ");
-			output.Write(attributes);
+			output.Write("<");
+			output.Write(html.TagName);
+			if (html.Attributes is { Length: > 0 } attributes)
+			{
+				output.Write(" ");
+				output.Write(attributes);
+			}
+			output.Write(">");
 		}
-		output.Write(">");
 		output.Write(body);
-		output.Write("");
+		if (!Continues(markup, context.Markups, context.Next))
+		{
+			output.Write("");
+		}
+	}
+
+	/// 
+	/// Whether 's element goes on through : the neighbour
+	/// carries it, and the layers outside it in  are the ones outside it there.
+	/// 
+	private static bool Continues(IMarkup markup, MarkupSet own, MarkupSet? neighbour)
+	{
+		if (neighbour is null || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false;
+		return mine.SequenceEqual(theirs);
+	}
+
+	///  and the drawn layers outside it, outermost last; null when the set lacks it.
+	private static List? Outside(MarkupSet markups, IMarkup markup)
+	{
+		List? outside = null;
+		foreach (var layer in markups)
+		{
+			if (outside is null && layer.Equals(markup)) outside = [];
+			if (outside is not null && layer is not (IDataMarkup or IPointMarkup)) outside.Add(layer);
+		}
+
+		return outside;
 	}
 }
diff --git a/MarkupString.Tests/Html/HtmlMarkupTests.cs b/MarkupString.Tests/Html/HtmlMarkupTests.cs
index 496b59d..94f6f36 100644
--- a/MarkupString.Tests/Html/HtmlMarkupTests.cs
+++ b/MarkupString.Tests/Html/HtmlMarkupTests.cs
@@ -53,6 +53,37 @@ await Assert.That(Render(outer, MarkupFormat.Html))
 			.IsEqualTo("
Inner
"); } + /// + /// A tag over text an inner layer splits into runs is one element, not one per run: tagwrap(pre,...) over two + /// command links and the spaces between them. + /// + [Test] + public async Task TagOverSeveralRuns_IsOneElement() + { + var link = AnsiMarkup.Create(linkUrl: "look", linkKind: LinkKind.Command); + var text = MarkupText.Wrap(HtmlMarkup.Create("pre"), + MarkupText.Concat([MarkupText.Wrap(link, "aaa"), MarkupText.Plain(" "), MarkupText.Wrap(link, "bbb")])); + + var html = Render(text, MarkupFormat.Html); + await Assert.That(html).StartsWith("
"); + await Assert.That(html.Split("
").Length - 1).IsEqualTo(1);
+		await Assert.That(html.Split("
").Length - 1).IsEqualTo(1); + await Assert.That(html).Contains(" ").Length - 1).IsEqualTo(1); + } + + /// A tag that does not cover its neighbour's outer layers closes before them, so the elements nest. + [Test] + public async Task TagInsideALayerItDoesNotShare_ClosesWhereItEnds() + { + var b = HtmlMarkup.Create("b"); + var i = HtmlMarkup.Create("i"); + var text = MarkupText.Concat(MarkupText.Wrap(b, MarkupText.Wrap(i, "x")), MarkupText.Wrap(i, "y")); + + await Assert.That(Render(text, MarkupFormat.Html)).IsEqualTo("xy"); + } + [Test] public async Task EmptyTag_StillWritesOpenAndClose() { From 62afd984049b58b61b7c9067799e77bc95dfa5e1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 00:03:19 +0000 Subject: [PATCH 2/3] Close a tag inside a colour span or before a point Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Rqqt7SWqms8BzoJivqhW6r --- MarkupString.Html/Emitters/HtmlTagEmitter.cs | 26 ++++++++++++++++-- MarkupString.Tests/Html/HtmlMarkupTests.cs | 29 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/MarkupString.Html/Emitters/HtmlTagEmitter.cs b/MarkupString.Html/Emitters/HtmlTagEmitter.cs index e46a514..ccc5f86 100644 --- a/MarkupString.Html/Emitters/HtmlTagEmitter.cs +++ b/MarkupString.Html/Emitters/HtmlTagEmitter.cs @@ -84,20 +84,40 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context /// Whether 's element goes on through : the neighbour /// carries it, and the layers outside it in are the ones outside it there. /// + /// + /// Only tags are known to continue: another layer outside, such as a colour span, is closed and opened again + /// on every run, so an element inside it cannot go on past the run. A neighbour holding a point may be left out + /// by a format that does not draw it, so the element is closed before it. + /// private static bool Continues(IMarkup markup, MarkupSet own, MarkupSet? neighbour) { - if (neighbour is null || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false; + if (neighbour is null || HoldsPoint(neighbour) || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false; return mine.SequenceEqual(theirs); } - /// and the drawn layers outside it, outermost last; null when the set lacks it. + private static bool HoldsPoint(MarkupSet markups) + { + foreach (var layer in markups) + { + if (layer is IPointMarkup) return true; + } + + return false; + } + + /// + /// and the drawn layers outside it, outermost last; null when the set lacks it, or a + /// drawn layer outside it is not a tag. + /// private static List? Outside(MarkupSet markups, IMarkup markup) { List? outside = null; foreach (var layer in markups) { if (outside is null && layer.Equals(markup)) outside = []; - if (outside is not null && layer is not (IDataMarkup or IPointMarkup)) outside.Add(layer); + if (outside is null || layer is IDataMarkup) continue; + if (layer is not HtmlMarkup) return null; + outside.Add(layer); } return outside; diff --git a/MarkupString.Tests/Html/HtmlMarkupTests.cs b/MarkupString.Tests/Html/HtmlMarkupTests.cs index 94f6f36..a72eec9 100644 --- a/MarkupString.Tests/Html/HtmlMarkupTests.cs +++ b/MarkupString.Tests/Html/HtmlMarkupTests.cs @@ -84,6 +84,35 @@ public async Task TagInsideALayerItDoesNotShare_ClosesWhereItEnds() await Assert.That(Render(text, MarkupFormat.Html)).IsEqualTo("xy"); } + /// + /// A colour span is closed and opened again on every run, so a tag inside it is too, and the spans and tags + /// still nest. + /// + [Test] + public async Task TagInsideAColour_ClosesWithEachSpan() + { + var b = HtmlMarkup.Create("b"); + var i = HtmlMarkup.Create("i"); + var text = MarkupText.Wrap(Red, MarkupText.Wrap(b, MarkupText.Concat(MarkupText.Wrap(i, "x"), MarkupText.Plain("y")))); + + var html = Render(text, MarkupFormat.Html); + await Assert.That(html.Split("").Length).IsEqualTo(html.Split("").Length); + await Assert.That(html).Contains("x"); + await Assert.That(html).Contains("y"); + } + + /// A tag closes before a point the format leaves out, so the element is not left open. + [Test] + public async Task TagBeforeADroppedPoint_IsClosed() + { + var registry = MarkupRegistry.Empty.WithHtml(); + var text = MarkupText.Wrap(HtmlMarkup.Create("pre"), MarkupText.Concat(MarkupText.Plain("x"), MarkupText.Bell())); + + var html = text.Render(MarkupFormat.Html, registry); + await Assert.That(html.Split("
").Length).IsEqualTo(html.Split("
").Length); + await Assert.That(html).StartsWith("
x
"); + } + [Test] public async Task EmptyTag_StillWritesOpenAndClose() { From 0280e71959342970fbd464e56a129d809c2f5b21 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 00:04:07 +0000 Subject: [PATCH 3/3] Keep tags balanced around a point run Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Rqqt7SWqms8BzoJivqhW6r --- MarkupString.Html/Emitters/HtmlTagEmitter.cs | 4 ++-- MarkupString.Tests/Html/HtmlMarkupTests.cs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/MarkupString.Html/Emitters/HtmlTagEmitter.cs b/MarkupString.Html/Emitters/HtmlTagEmitter.cs index ccc5f86..4353fdb 100644 --- a/MarkupString.Html/Emitters/HtmlTagEmitter.cs +++ b/MarkupString.Html/Emitters/HtmlTagEmitter.cs @@ -87,11 +87,11 @@ public void Emit(IMarkup markup, ReadOnlySpan body, in EmitContext context /// /// Only tags are known to continue: another layer outside, such as a colour span, is closed and opened again /// on every run, so an element inside it cannot go on past the run. A neighbour holding a point may be left out - /// by a format that does not draw it, so the element is closed before it. + /// by a format that does not draw it, so the element is closed before it and opened again after it, and a point run is its own element. /// private static bool Continues(IMarkup markup, MarkupSet own, MarkupSet? neighbour) { - if (neighbour is null || HoldsPoint(neighbour) || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false; + if (neighbour is null || HoldsPoint(own) || HoldsPoint(neighbour) || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false; return mine.SequenceEqual(theirs); } diff --git a/MarkupString.Tests/Html/HtmlMarkupTests.cs b/MarkupString.Tests/Html/HtmlMarkupTests.cs index a72eec9..19e5d2a 100644 --- a/MarkupString.Tests/Html/HtmlMarkupTests.cs +++ b/MarkupString.Tests/Html/HtmlMarkupTests.cs @@ -113,6 +113,23 @@ public async Task TagBeforeADroppedPoint_IsClosed() await Assert.That(html).StartsWith("
x
"); } + /// Tags stay balanced around a point whether or not the format draws it, before or after the text. + [Test] + [Arguments(true)] + [Arguments(false)] + public async Task TagAroundAPoint_IsBalanced(bool pointFirst) + { + var body = pointFirst ? MarkupText.Concat(MarkupText.Bell(), MarkupText.Plain("x")) : MarkupText.Concat(MarkupText.Plain("x"), MarkupText.Bell()); + var text = MarkupText.Wrap(HtmlMarkup.Create("pre"), body); + + foreach (var registry in new[] { Registry, MarkupRegistry.Empty.WithHtml() }) + { + var html = text.Render(MarkupFormat.Html, registry); + await Assert.That(html.Split("
").Length).IsEqualTo(html.Split("
").Length); + await Assert.That(html).StartsWith("
").And.EndsWith("
"); + } + } + [Test] public async Task EmptyTag_StillWritesOpenAndClose() {