diff --git a/CHANGELOG.md b/CHANGELOG.md index 892e212..0047713 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/MarkupString.Ansi/Emitters/AnsiHtmlEmitter.cs b/MarkupString.Ansi/Emitters/AnsiHtmlEmitter.cs index d193120..00ee6d1 100644 --- a/MarkupString.Ansi/Emitters/AnsiHtmlEmitter.cs +++ b/MarkupString.Ansi/Emitters/AnsiHtmlEmitter.cs @@ -41,6 +41,15 @@ private static void WriteSpan(in AnsiStyle style, ReadOnlySpan 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) @@ -80,6 +89,12 @@ private static void WriteSpan(in AnsiStyle style, ReadOnlySpan body, in Em output.Write(""); } + /// The text colour of reverse video whose background is the default: the page's background. + private const string InvertedForeground = "var(--ms-bg, #000)"; + + /// The background of reverse video whose text colour is the default: the page's text colour. + private const string InvertedBackground = "var(--ms-fg, #fff)"; + /// The standard colour, 0-15, is, or -1 for any other. private static int SlotOf(AnsiColor? color) => color switch { diff --git a/MarkupString.Tests/Ansi/AnsiRenderTests.cs b/MarkupString.Tests/Ansi/AnsiRenderTests.cs index 082fa2b..5642eb9 100644 --- a/MarkupString.Tests/Ansi/AnsiRenderTests.cs +++ b/MarkupString.Tests/Ansi/AnsiRenderTests.cs @@ -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("x"); + .IsEqualTo("x"); + } + + [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("x"); } [Test]