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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All notable changes to `MarkupString`, `MarkupString.Ansi`, `MarkupString.Html`,
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and the project
follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- In HTML, an item of a `Flex` row with a set width keeps that width beside an item with none, as a
terminal draws it: the spare width goes to the item with none. A two-cell marker beside a reply took
half the row and pushed the reply to the right.

## 2.31.0 — 2026-11-12

### Added
Expand Down
10 changes: 7 additions & 3 deletions MarkupString.Html/Emitters/LayoutHtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,19 @@ private static void Flex(Flex flex, HtmlLayoutWriter html, BorderStyle? frame)
}
html.Write("\">");

// As in a terminal, a row with an item of no set width gives all its spare width to those items.
var hasAuto = items.Any(block => Layout.Flex.SizeOf(block).Basis.Kind == BlockSizeKind.Auto);
foreach (var block in items)
{
// Each item asks for its terminal width and grows to fill its row, so items that do not fit
// side by side wrap onto rows of their own. Spare width is left alone only when the layout
// asked for it to be placed somewhere.
var item = Layout.Flex.SizeOf(block);
// A Bounded item keeps its width, as in a terminal: the spare goes after it, not into it.
var grow = block is not Bounded && (flex.Justify == FlexJustify.Start || item.Basis.Kind == BlockSizeKind.Auto)
? Math.Max(1, item.Grow)
// A Bounded item keeps its width, as in a terminal: the spare goes after it, not into it. So
// does an item with a set width beside one without: the spare goes to the one without.
var grow = item.Basis.Kind == BlockSizeKind.Auto ? Math.Max(1, item.Grow)
: hasAuto ? 0
: block is not Bounded && flex.Justify == FlexJustify.Start ? Math.Max(1, item.Grow)
: item.Grow;
html.Write("<div class=\"ms-item\" style=\"flex:");
html.Write(Number(grow));
Expand Down
12 changes: 12 additions & 0 deletions MarkupString.Tests/Layout/BlockLayoutTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,18 @@ await Assert.That(BlockLayout.Lines(row, 20).Select(line => line.ToPlainText().T
await Assert.That(BlockLayout.Build(row, 20).Render(MarkupFormat.Html, Registry)).Contains("<div class=\"ms-item\" style=\"flex:0 1 5ch");
}

[Test]
public async Task Flex_ASetWidthBesideAnAutoItem_KeepsItsWidthInHtmlAsInATerminal()
{
var row = new Flex([T("*").Sized(BlockSize.Cells(2)), T("The reply")]) { Gap = 0 };

await Assert.That(BlockLayout.Lines(row, 20).Select(line => line.ToPlainText().TrimEnd()).ToArray()).IsEquivalentTo(new[] { "* The reply" });
var html = BlockLayout.Build(row, 20).Render(MarkupFormat.Html, Registry);
await Assert.That(html).Contains("<div class=\"ms-item\" style=\"flex:0 1 2ch\">")
.Because("the spare width goes to the item with no set width, not half to the marker");
await Assert.That(html).Contains("<div class=\"ms-item\" style=\"flex:1 1 0\">");
}

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