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..4353fdb 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 /// public sealed class HtmlTagEmitter : IMarkupEmitter { @@ -56,17 +60,66 @@ public void Emit(IMarkup markup, ReadOnlySpan<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.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(""); - output.Write(html.TagName); - output.Write(">"); + if (!Continues(markup, context.Markups, context.Next)) + { + output.Write(""); + output.Write(html.TagName); + output.Write(">"); + } + } + + /// + /// 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 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(own) || HoldsPoint(neighbour) || Outside(own, markup) is not { } mine || Outside(neighbour, markup) is not { } theirs) return false; + return mine.SequenceEqual(theirs); + } + + private static bool HoldsPoint(MarkupSet markups) + { + foreach (var layer in markups) + { + if (layer is IPointMarkup) return true; + } + + return false; + } + + ///+ /// + private static Listand the drawn layers outside it, outermost last; null when the set lacks it, or a + /// drawn layer outside it is not a tag. + /// ? Outside(MarkupSet markups, IMarkup markup) + { + List ? outside = null; + foreach (var layer in markups) + { + if (outside is null && layer.Equals(markup)) outside = []; + 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 496b59d..19e5d2a 100644 --- a/MarkupString.Tests/Html/HtmlMarkupTests.cs +++ b/MarkupString.Tests/Html/HtmlMarkupTests.cs @@ -53,6 +53,83 @@ 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"); + } + + ///+ /// 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"); + } + + ///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() {