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 @@ -58,6 +58,11 @@ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- HTML: reverse video with only one colour now swaps it with the page's default on the other side, as a
terminal does. `\e[32;7m` was a green background under the page's text colour; the text is now
`var(--ms-bg)`, and `\e[41;7m` gets `var(--ms-fg)` as its background. These are the properties `ms-invert`
already swaps.

- `BlockLayout.Blocks` keeps the width of a block laid out at a width of its own: a non-fluid layout
nested in another comes back `Bounded` by the width it was built at, so a 30-cell box inside a
wider box stays 30 cells instead of stretching to the outer one. A fluid layout still takes the
Expand Down
15 changes: 15 additions & 0 deletions MarkupString.Ansi/Emitters/AnsiHtmlEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ private static void WriteSpan(in AnsiStyle style, ReadOnlySpan<char> body, in Em
var foregroundSlot = SlotOf(foreground);
var backgroundSlot = SlotOf(background);
var hasStyle = foregroundHex.Length > 0 || backgroundHex.Length > 0;

// Reverse video with one colour swaps it with the page's default on the other side, as a terminal
// does: the default background becomes the text colour, or the default text colour the background.
// These are the properties ms-invert swaps with, so both cases agree.
if (hasStyle && style.Inverted)
{
if (foregroundHex.Length == 0) foregroundHex = InvertedForeground;
if (backgroundHex.Length == 0) backgroundHex = InvertedBackground;
}
var hasClasses = HasClasses(style, hasStyle);

if (!hasStyle && !hasClasses)
Expand Down Expand Up @@ -80,6 +89,12 @@ private static void WriteSpan(in AnsiStyle style, ReadOnlySpan<char> body, in Em
output.Write("</span>");
}

/// <summary>The text colour of reverse video whose background is the default: the page's background.</summary>
private const string InvertedForeground = "var(--ms-bg, #000)";

/// <summary>The background of reverse video whose text colour is the default: the page's text colour.</summary>
private const string InvertedBackground = "var(--ms-fg, #fff)";

/// <summary>The standard colour, 0-15, <paramref name="color"/> is, or -1 for any other.</summary>
private static int SlotOf(AnsiColor? color) => color switch
{
Expand Down
11 changes: 10 additions & 1 deletion MarkupString.Tests/Ansi/AnsiRenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,16 @@ public async Task Html_InvertedWithOnlyForeground_MovesItToTheBackground()
var markup = AnsiMarkup.Create(foreground: new AnsiColor.Rgb(255, 0, 0), inverted: true);
var text = MarkupText.Wrap(markup, "x");
await Assert.That(Render(text, MarkupFormat.Html))
.IsEqualTo("<span style=\"background-color: #ff0000\">x</span>");
.IsEqualTo("<span style=\"color: var(--ms-bg, #000); background-color: #ff0000\">x</span>");
}

[Test]
public async Task Html_InvertedWithOnlyBackground_MovesItToTheText()
{
var markup = AnsiMarkup.Create(background: new AnsiColor.Rgb(0, 0, 255), inverted: true);
var text = MarkupText.Wrap(markup, "x");
await Assert.That(Render(text, MarkupFormat.Html))
.IsEqualTo("<span style=\"color: #0000ff; background-color: var(--ms-fg, #fff)\">x</span>");
}

[Test]
Expand Down
Loading