The break opportunity after leading preserved spaces lands in the middle of them

#40808833 Area: Blink>Layout>Inline CL 8663299 In Review

In one line

For <div style="white-space:pre-wrap"> <span> ijkl </span></div> the leading preserved space run is two spaces long and spans two LayoutTexts. InlineItemsBuilder put the generated break opportunity after the first one.

collected textopportunity sits
before" \u200B ijkl " between the two spaces
after" \u200Bijkl " after the whole run

Character by character

U+200B is zero width and never painted, so there is no pixel diff to show for this markup: in pre-wrap a break is already allowed after every space, so both strings happen to lay out the same. What is wrong is the item stream the inline model is built from, which is why the proof below is the item dump, not a screenshot.

The item stream, dumped from the builder

Both columns come from the same binary on the same markup, with the LeadingSpaceBreakOpportunityFix kill switch flipped (ScopedLeadingSpaceBreakOpportunityFixForTest), printing InlineItem::ToString() plus [StartOffset,EndOffset):

before — text " \u200B ijkl "

itemoffsets
Text " "[0,1)
Control (ZWSP)[1,2)
OpenTag SPAN[2,2)
Text " ijkl "[2,8)
CloseTag SPAN[8,8)

the opportunity is emitted before the span is even entered, so the space the span contributes ends up after it

after — text " \u200Bijkl "

itemoffsets
Text " "[0,1)
OpenTag SPAN[1,1)
Text " "[1,2)
Control (ZWSP)[2,3)
Text "ijkl "[3,8)
CloseTag SPAN[8,8)

the opportunity is deferred until the run ends inside the span, and is attached to that span's LayoutText

Root cause

InsertBreakOpportunityAfterLeadingPreservedSpaces() scanned the leading spaces of one LayoutText and appended the opportunity right after them. When that text node ends in the middle of the run, the rest of the run follows the opportunity.

The bug is filed as "a control item is not ignored when reusing text after removing a <br>", and the expectation in ReusingWithPreservedCase3 carried a TODO blaming the reuse path. It is not the reuse path. I probed an unpatched build: fresh layout, nothing reused, already collects " \u200B ijkl ". AppendTextReusing() only inherits the placement, which is what the reporter saw after deleting a <br>.

The fix defers the opportunity until the sequence actually ends: text that does not start with a space, an atomic inline, a forced break, a block-in-inline, or the end of the block.

Where the end-of-block flush has to live

A run that reaches the end of the block still needs its opportunity, because a later relayout may append content after it. The obvious place to flush it is ToString(), where the final text is read — that is wrong, and loudly so:

FATAL offset_mapping_builder.cc:244] DCHECK failed:
      destination_length_ == string.length() (2 vs. 3)
      in HTMLFormMcpToolTest.ParameterSchema_Readonly

appending to text_ while the string is being read is past the point where OffsetMappingBuilder has accounted for it. The flush is therefore in ExitBlock(), the real end of collection, and ToString() only DCHECK(!has_pending_break_opportunity_). That leaves one flush point instead of a flush in ToString() plus a separate call in CollectInlinesInternal().

Verification

Links

Issue 40808833 CL 8663299 standalone sampler.html