Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<pre>`
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
`<xch_page clear="text">` alone. [Formats](docs/formats.md#clearing-the-screen) has a table of which clients
Expand Down
71 changes: 62 additions & 9 deletions MarkupString.Html/Emitters/HtmlTagEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <see cref="HtmlTagPolicy.Apply"/> — and a tag it
/// refuses leaves its body in place, unwrapped. The body is encoded by the renderer either way.
/// <para>A tag over several runs is one element: text split by an inner layer (a link inside <c>&lt;pre&gt;</c>)
/// is written <c>&lt;pre&gt;&lt;a&gt;a&lt;/a&gt; &lt;a&gt;b&lt;/a&gt;&lt;/pre&gt;</c>, not one <c>&lt;pre&gt;</c> 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.</para>
/// </remarks>
public sealed class HtmlTagEmitter : IMarkupEmitter
{
Expand Down Expand Up @@ -56,17 +60,66 @@ public void Emit(IMarkup markup, ReadOnlySpan<char> 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))
Comment thread
HarryCordewener marked this conversation as resolved.
Resolved
{
output.Write("</");
output.Write(html.TagName);
output.Write(">");
}
}

/// <summary>
/// Whether <paramref name="markup"/>'s element goes on through <paramref name="neighbour"/>: the neighbour
/// carries it, and the layers outside it in <paramref name="own"/> are the ones outside it there.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
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);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

private static bool HoldsPoint(MarkupSet markups)
{
foreach (var layer in markups)
{
if (layer is IPointMarkup) return true;
}

return false;
}

/// <summary>
/// <paramref name="markup"/> and the drawn layers outside it, outermost last; null when the set lacks it, or a
/// drawn layer outside it is not a tag.
/// </summary>
private static List<IMarkup>? Outside(MarkupSet markups, IMarkup markup)
{
List<IMarkup>? 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;
}
}
77 changes: 77 additions & 0 deletions MarkupString.Tests/Html/HtmlMarkupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,83 @@ await Assert.That(Render(outer, MarkupFormat.Html))
.IsEqualTo("<div class=\"outer\"><span class=\"inner\">Inner</span></div>");
}

/// <summary>
/// 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.
/// </summary>
[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("<pre><a").And.EndsWith("</a></pre>");
await Assert.That(html.Split("<pre>").Length - 1).IsEqualTo(1);
await Assert.That(html.Split("</pre>").Length - 1).IsEqualTo(1);
await Assert.That(html).Contains("</a> <a");
var pueblo = Render(text, MarkupFormat.Pueblo);
await Assert.That(pueblo.Split("<pre>").Length - 1).IsEqualTo(1);
}

/// <summary>A tag that does not cover its neighbour's outer layers closes before them, so the elements nest.</summary>
[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("<b><i>x</i></b><i>y</i>");
}

/// <summary>
/// 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.
/// </summary>
[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("<b>").Length).IsEqualTo(html.Split("</b>").Length);
await Assert.That(html).Contains("<b><i>x</i></b></span>");
await Assert.That(html).Contains("<b>y</b></span>");
}

/// <summary>A tag closes before a point the format leaves out, so the element is not left open.</summary>
[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("<pre>").Length).IsEqualTo(html.Split("</pre>").Length);
await Assert.That(html).StartsWith("<pre>x</pre>");
}

/// <summary>Tags stay balanced around a point whether or not the format draws it, before or after the text.</summary>
[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("<pre>").Length).IsEqualTo(html.Split("</pre>").Length);
await Assert.That(html).StartsWith("<pre>").And.EndsWith("</pre>");
}
}

[Test]
public async Task EmptyTag_StillWritesOpenAndClose()
{
Expand Down
Loading