Skip to content

Fix #60: wrap long URLs, paths and identifiers - #113

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/60-long-tokens
Nov 16, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/60-long-tokens

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Fixes #60. It needed the canvas fix from #112 (tdewolff/canvas#397), now in place.

Break points: breakable inserts U+200B, which canvas treats as an invisible optional break:

  • after / ? & #, but not inside ://
  • at camelCase boundaries
  • every 20 characters of an otherwise unbroken run

Where they go

  • Table cells, whose widths aren't known until layout: every token of 12+ characters (code, links) or 24+ (prose). cellWidths treats U+200B as a word boundary, so columns can shrink instead of overlapping.
  • Elsewhere: only tokens wider than 70% of the measure. A narrower token can always move to a line of its own. Break points aren't free: every one is a candidate for the Knuth-Plass breaker on every layout pass, and adding them to every moderately long URL took a 2,000-paragraph benchmark from 1.8 s to 3.1 s. With the gate it's back to baseline (1.8–2.0 s against 1.7–1.9 s on main).

Exact splits: splitting a paragraph or table row used to rebuild text from the laid-out spans and add " " at each line end, which would put a stray space into a URL at a U+200B break. Now:

  • lineCuts aligns the laid-out lines with the original runs, skipping unused U+200B and U+00AD inside a line and matching a line-end - to its soft hyphen.
  • splitAt cuts the original runs there, so faces, links, U+200B and U+00AD are kept exactly. A soft hyphen ending the head becomes a visible -.
  • joinLines remains as a fallback, used when the lines can't be aligned.

Extracted text has no stray spaces at break points.

Tests

  • TestBreakable: the insertion rules, visible text unchanged, the short-token threshold.
  • TestLongTokensStayInMeasure: no OverflowsX in a paragraph with a long URL and identifier, or in any cell of a three-column table of long paths and URLs.
  • TestSplitPreservesText: at every possible split, the original equals head + separator + tail.

Removing the break points fails the overflow test 4 times; forcing the old rebuild path fails the split test. The existing paragraph, footnote and table-split tests pass on the new path. I also checked the render visually: the URL and identifier wrap in the paragraph, and the three columns share the width.

A long URL or identifier had no break opportunity, so it ran past the
right margin, and in a table it overlapped the next column.

- breakable inserts zero-width spaces, which canvas treats as invisible
  optional breaks (working since the fork fix, tdewolff/canvas#397):
  after / ? & # (not inside ://), at camelCase boundaries, and every 20
  characters of an unbroken run. In table cells, whose widths are only
  known at layout, every token of 12+ (code, links) or 24+ (prose)
  characters gets them, and cellWidths treats them as word boundaries so
  columns can shrink. Elsewhere only a token wider than 70% of the
  measure does. Break points cost Knuth-Plass work on every layout pass,
  and adding them to every moderately long URL nearly doubled render
  time; with the gate, the 2000-paragraph benchmark is back to baseline
  (1.8-2.0s against 1.7-1.9s on main).
- Paragraph and table-row splits used to rebuild text from the laid-out
  spans and add a space at every line end, which would put a stray space
  into a URL at a zero-width-space break. lineCuts now aligns the lines
  with the original runs, and splitAt cuts the runs there, keeping faces,
  links, zero-width spaces and soft hyphens; a soft hyphen that ends the
  head becomes a visible '-'. joinLines remains only as a fallback.

Tests: TestBreakable (the rules, visible text unchanged, the short-token
threshold), TestLongTokensStayInMeasure (no overflow in a paragraph or
in a three-column table) and TestSplitPreservesText (the original is
exactly head + separator + tail at every possible split). Removing the
break points fails the overflow test 4 times, and forcing the old
rebuild path fails the split test.
@scttfrdmn
scttfrdmn merged commit cde7d3d into main Nov 16, 2026
1 check passed
@scttfrdmn
scttfrdmn deleted the fix/60-long-tokens branch November 16, 2026 17:26
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.

Long URLs, identifiers and inline code overflow the page and table cells

1 participant