Skip to content

fix(layout): honor align-items for bare text mixed with img in flex - #456

Open
tannevaled wants to merge 1 commit into
mainfrom
worktree-agent-ac7ff2988c74e91a7
Open

tannevaled wants to merge 1 commit into
mainfrom
worktree-agent-ac7ff2988c74e91a7

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Fixes a confirmed HEAD defect (pinned at v0.17.0, reported by a go-slides
session): a display:flex row container holding an <img> followed by
literal bare text (no wrapping element) ignored align-items entirely. The
text was always positioned per default CSS 2.1 inline baseline alignment — an
image's synthesized baseline is its own bottom edge — regardless of the
declared align-items value (flex-start/center/flex-end/stretch).

The adjacent, textually similar shape where the text is wrapped in an element
(e.g. <strong>text</strong> next to the <img>) was already correct at
HEAD: flexItems only ever collects element children, so that case reaches
flexRow's own cross-axis handling (crossOffset), which already honors
align-items correctly.

Root cause

flex()'s fallback branch in layout/flex.go triggers on hasDirectText,
true whenever the flex container has any direct text-node child. That
branch bypasses flexRow/flexColumn completely and lays the container out
as a plain CSS 2.1 inline formatting context via layoutInline, which never
reads align-items.

Fix

Added alignInlineFlexItems in layout/flex.go, called right after the
inline-fallback layout runs, to reposition each inline item within its line
according to align-items (flex-start/stretch → cross-start flush,
center → centered, flex-end → cross-end flush), keeping
InlineItem.NestedBox in sync via the existing translateBox helper. This
also removes a phantom gap in the single-line case: the line height used to
include baseline-descent room that no item actually occupied.

Scope: only the single-line case gets the line-height correction, to avoid
risking new gaps in the already-approximate multi-line mixed-fallback path.
The separate hasBlockBreak / placeInlineSegments path (a promoted
block-level child mixed with bare text) is untouched — out of scope for this
fix.

Testing

Added layout/flex_mixedtext_align_test.go:

  • TestFlexMixedTextAndImageHonorsAlignItems: matrix over
    flex-start/center/flex-end/stretch for the bare-text+img shape.
  • TestFlexMixedTextAndImageDefaultAlignItems: no align-items declared
    (the real-world theme rule), confirming the initial stretch value no
    longer glues text to the image's bottom edge.
  • TestFlexElementWrappedTextAndImageAlignItems: regression guard for the
    already-correct element-wrapped-text shape, unaffected by this change.

Full gate green: gotest ./..., gotest ./layout/... -race, gofmt -l .
(clean for the touched files), go vet ./..., govulncheck ./....

🤖 Generated with Claude Code

A display:flex row holding an <img> followed by literal bare text (no
wrapping element) ignored align-items entirely: the text always sat
flush with the image's synthesized baseline (its own bottom edge),
regardless of flex-start/center/flex-end/stretch.

flex()'s hasDirectText fallback (triggered whenever a flex container
has any direct text-node child, e.g. "<img>some text") bypasses
flexRow's cross-axis handling completely and lays the content out as
plain CSS 2.1 inline content, which never reads align-items. The
element-only case (text wrapped in e.g. <strong>) was already correct,
since flexItems only collects element children.

Add alignInlineFlexItems to reposition each inline item within its
line per align-items after the inline fallback runs, keeping
InlineItem.NestedBox in sync via the existing translateBox helper. As
a side effect this also removes a phantom gap: the line height used to
include baseline descent room no item occupied.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant