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 text | opportunity sits | |
|---|---|---|
| before | " \u200B ijkl " | between the two spaces |
| after | " \u200Bijkl " | after the whole run |
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.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 "
| item | offsets |
|---|---|
| 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 "
| item | offsets |
|---|---|
| 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
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.
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().
InlineNodeTest.LeadingPreservedSpacesAcrossBoxes
(fresh layout), LeadingPreservedSpacesAcrossFloat (a float
does not end the run), LeadingPreservedSpacesBeforeAtomicInline
(an atomic inline does), and ReusingWithPreservedCase3, which
used to assert the buggy string.*Inline* / *OffsetMapping* /
*LayoutText* unit tests pass, including the builder tests
that drive ToString() directly.wpt/css/css-text + fast/text +
editing: 4588 tests as expected. The one outlier,
text-transform-fullwidth-001.xht, fails with and without the
patch and is in TestExpectations under crbug.com/453781784.LeadingSpaceBreakOpportunityFix
kill switch.