Skip to content

Editor: a click past the end of a line opens nothing, not the link that ends it - #14

Open
DomKing wants to merge 1 commit into
Azilone:mainfrom
DomKing:fix/link-tail-click-upstream
Open

DomKing wants to merge 1 commit into
Azilone:mainfrom
DomKing:fix/link-tail-click-upstream

Conversation

@DomKing

@DomKing DomKing commented Nov 10, 2026 •

Copy link
Copy Markdown

The bug

A link at the end of a line opens when clicked in the empty space after it, instead of putting the caret there.

EditorTextView.hit(_:at:) took the character under the click, else the one before the caret, whatever the point was:

let index = characterIndexForInsertion(at: point)
return attributeRun(key, at: index) ?? attributeRun(key, at: index - 1)

Past the end of a line the caret goes before the line's break, so that fallback landed on the link's last character. The room on the right of a line is where a note read back differs from what was typed: the space typed after a link is not saved, so a note ending on https://example.com comes back with the address flush to the line's end, and a click past it opened the address.

The fix

The fallback stays, for a click on a character's right half, but only when the point is actually over that character's own room. Two details make it safe:

  • A hidden character's room is its kern. The styling gives concealed characters a vanishing font, so they take no width; the kern after them is where their chip is drawn (a note link, a date pill). Reading the kern keeps those clickable.
  • The height is the line's, not the glyph's. A task's box is drawn larger than the characters it hides, so it takes a click anywhere down the line.

Verification

Ten checks against real TextKit, with the shipping hit/covers/rect/lineBox/segments compiled verbatim from the source and the pre-fix lookup beside them:

BEFORE, click past a bare address ending its line: opened https://example.com
AFTER,  click past a bare address ending its line: opened nothing
AFTER,  click on the address' middle:              opened https://example.com
AFTER,  click on its last letter's right half:     opened https://example.com
AFTER,  click below the note's last line:          opened nothing
AFTER,  click on a chip's room (kern):             opened loam-note:Ideas
AFTER,  click on a task's box:                     found it

App/Tests/MarkdownEditorTests/LinkClickTests.swift holds the regression tests, in the offscreen-window style the other editor tests use.

Also

One line under ## Unreleased, as a user-visible fix.

`hit(_:at:)` took the character under the click, else the one before the caret —
whatever the point. Past the end of a line the caret goes before the line's break,
so a link ending its line was opened from the empty room after it. That room is
where a note read back differs from what was typed: the space typed after a link
is not saved, so a note ending on an address came back with a click past it
opening the address.

The fallback to the character before the caret stays, for a click on its right
half, but only when the point is over that character's own room. A hidden one's
room is the kern the styling gives it, where its chip is drawn; the height is the
line's, so a task's box, drawn larger than its characters, still takes a click.
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