fix: align trailing collapsible space handling across all line-break paths#29
Open
barry166 wants to merge 1 commit intochenglou:mainfrom
Open
fix: align trailing collapsible space handling across all line-break paths#29barry166 wants to merge 1 commit intochenglou:mainfrom
barry166 wants to merge 1 commit intochenglou:mainfrom
Conversation
…paths
countPreparedLinesSimple (used by layout()) correctly skips collapsible
spaces that overflow the line edge — CSS white-space: normal behavior
where trailing spaces hang without triggering a break.
walkPreparedLinesSimple (used by layoutWithLines/walkLineRanges) and
layoutNextLineRangeSimple (used by layoutNextLine) were missing this
guard, causing canBreakAfter('space') to trigger spurious line breaks
when word + space > maxWidth.
Add the isSimpleCollapsibleSpace check before the canBreakAfter check
in both walk paths to match the count path behavior.
Add a regression test with crafted segment data that directly triggers
the divergence condition (word=40 + space=5 > maxWidth=42, but
word=40 + next=1 <= 42).
Closes chenglou#11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #11 —
layout()andlayoutWithLines()return differentlineCountfor trailing collapsible spaces.countPreparedLinesSimple(used bylayout()) correctly skips collapsible spaces that overflow the line edge — CSSwhite-space: normalbehavior where trailing spaces hang without triggering a break. The two walk-path counterparts were missing this guard:walkPreparedLinesSimple(used bylayoutWithLines()/walkLineRanges()) —canBreakAfter('space')triggered a spurious line breaklayoutNextLineRangeSimple(used bylayoutNextLine()) — same issueThe fix adds the
isSimpleCollapsibleSpacecheck beforecanBreakAfterin both paths, matching the count path.Reproduction
Any text where
wordWidth + spaceWidth > maxWidthbutwordWidth + nextSegmentWidth <= maxWidth:Changes
src/line-break.ts— addisSimpleCollapsibleSpaceguard inwalkPreparedLinesSimpleandlayoutNextLineRangeSimple(+12 lines)src/layout.test.ts— regression test with crafted segment data that directly triggers the divergence (+33 lines)Test plan
bun test— 61/61 pass (including new regression test)tsc --noEmit— 0 errorsoxlint --type-aware src— 0 warnings, 0 errorsline-break.tsfix causes the new test to fail (walked=2vscounted=1)