Skip to content

Keep keys typed into Leaf's fields from bubbling to the page - #5037

Merged
max-sixty merged 5 commits into
mainfrom
grind/issue-5023
Nov 2, 2026
Merged

max-sixty merged 5 commits into
mainfrom
grind/issue-5023

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 2, 2026 •

Copy link
Copy Markdown
Owner

Keys typed into a Leaf comment box, draft editor or search box no longer bubble to the page's own keyboard listeners. On a reveal.js deck served by Leaf, typing b into a comment blacked the deck out. Page shortcut code skips a key typed into a text field by asking whether the focused element is an input, a textarea or editable. Leaf's comment box (leaf-text) and its search boxes (wa-input) hide their editors in shadow roots, so the page sees only a host that is none of those.

What changed

  • One stop for every Leaf field (field-keys.js, new and dependency-free). keepFieldKeys(host) stops each keydown, keypress and keyup at the field's host as it bubbles out, after the field's own handling has run. No listener outside the field (on main, the document or the window) hears it bubble. A field's input events still reach the page, as a textarea's do.
  • Every Leaf field calls it. LeafText's constructor covers the comment box, reply box, panel field, draft editor and option field. searchField() covers page search, Page Map search, command reference search and a package widget's search, such as lf-diff's file filter. The panel's native Threads and menu search calls it too.
  • The dispatcher reads a field's keys at the field (keyboard/controller.js). Its pass after a control's own handling ran on the document; for a key from a field it now runs at the host, through readLeavingKeys in the same module. Enter, Escape and Tab from a field keep their meanings and their order after the field's own handling. Printed characters still go through the dispatcher's capture pass on the document, as before.
  • Leaf's other readers move to capture. The draft editor's caret keeper (drafts.js, keyup) and the ⌥ aim (composing/aim.js, keydown and keyup) listened in bubble on the document or window, and now listen in capture.
  • Contracts. page-behavior.md tells page authors that keys typed into a Leaf comment box, draft editor or search box never bubble to their listeners. keyboard/AGENTS.md states the capture rule for Leaf's own readers.

A key pressed anywhere else, the page itself included, reaches the page as before.

Rejected: the field answering isContentEditable. reveal.js asks that of the focused element, but a check by tag or selector never does, and a page that checks nothing would still hear every typed key.

Evidence

The issue's repro through leaf-dev probe: a page script logging keydown, keypress and keyup on the document and window, plus reveal.js's own filter (document.activeElement.isContentEditable or an input/textarea tag); select a word, press c, type b and ArrowLeft.

  • Before: the page logged keydown b, reveal b, window keydown b, keypress b, keyup b, and the same for ArrowLeft.
  • After: the page logged nothing, and the field held b. Same result in Chromium, Firefox and WebKit.

The new test, tests/browser/drafts/test_keys.py::test_keys_typed_into_leaf_fields_never_bubble_to_the_page, listens on main, the document and the window. It types into the comment box and the page search (both in Leaf's chrome) and the draft's editor (inside main). It fails on the first commit of this PR, where the page hears 16 key events from the comment box. With only the searchField stop reverted, it fails with "the page heard keys typed into the page search". It passes with the fix. Its controls: a key pressed on the page still reaches all three listeners, Enter sends the comment, and Escape closes the draft's editor and the page search.

What I ran

  • uv run pytest -n 4 over tests/browser/drafts/test_keys.py and selections from drafts/test_composer.py (Return, a single space, Resume writing, line keys, Markdown source), navigation/test_key_scopes.py (text boxes, unsent words, Escape steps), test_render_aim.py (⌥ aim with a composer open), threads/test_composing.py (Escape from a box, Enter send, input method composition) and test_render_reactions.py (Tab opens the picker): 41 passed, 1 failed.
  • After the search-box change: every -k search test in test_render_semantic_selection.py, test_render_floating_panel.py, navigation/test_command_reference.py, threads/test_summaries.py and margin/test_page_map.py, plus test_keys.py and the composing selection: 45 passed, 1 failed.
  • Two failures are inherited. test_a_text_box_keeps_its_keys_from_the_widget_around_it uses a native textarea this change doesn't touch, and fails the same way on this PR's first commit, without the fix. test_the_reference_keeps_local_search_state_on_one_lit_surface waits for a thread.resolution.toggle row the reference doesn't list, and fails on main too.
  • pnpm run test:runtime: 737 passed.
  • pnpm run check:browser: the committed kernel matches a fresh build.
  • A probe of the draft caret keeper: after typing abcd and two ArrowLefts in a comment box, the kept writing place reads [2, 2] before and after the change.

Not verified

  • A real input method's keydowns. Playwright's composition (Input.imeSetComposition) sends no keydown; the composition test above passed. The dispatcher already ignores composing keydowns.
  • lf-diff's file filter, which gains the stop through searchField; it filters on input events, which still pass.
  • A page listener in capture still hears keys typed into a field, since the field's handling comes after capture. Page shortcut code rarely listens in capture.
  • The keyup of a key that moves focus out of the field, such as Escape or Enter, lands on the new focus and reaches the page. The keyup of any Leaf command key already does.

Closes #5023

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 3 commits November 1, 2026 16:07
A page script listening for keydown on the document hears every key typed
into a Leaf comment: page shortcut code skips keys typed into an input, a
textarea or an editable element, and the focused element is the leaf-text
host, which is none of those. A reveal.js deck blacks out on a typed `b`.

The test listens on the document, the window and `main`, types into the
comment box (in Leaf's chrome) and the draft's editor (inside `main`), and
fails while the page hears those keys. Its controls: a key pressed on the
page reaches the page, Enter sends the comment and Escape closes the editor.

Part of #5023.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A page script listening for keydown on the document heard every key typed
into a Leaf comment, so a reveal.js deck blacked out on a typed `b`. Page
shortcut code skips keys typed into a text field by asking whether the
focused element is an input, a textarea or editable, and from outside the
field's closed shadow root the focused element is the `leaf-text` host,
which is none of those.

The host now stops each keydown, keypress and keyup as it bubbles out of
the field, after CodeMirror inside it has handled the key. Leaf's key
dispatcher, whose pass after a control's own handling ran on the document,
reads a field's keys at the host instead (`readLeavingKeys`), so Enter,
Escape and Tab from a field behave as before. The two other Leaf readers of
these keys move to capture: the draft editor's caret keeper (keyup) and the
⌥ aim (keydown, keyup). Every listener outside the field, on `main`, the
document or the window, no longer hears the key bubble; a key pressed
anywhere else reaches the page as before.

Rejected: the host answering `isContentEditable`, which reveal.js asks of
the focused element but a check by tag or selector never does, and which
leaves a page that checks nothing hearing every typed key.

page-behavior.md tells page authors that keys typed into a Leaf comment
box or draft editor don't reach their listeners; keyboard/AGENTS.md states
the capture rule for Leaf's own readers.

Closes #5023.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the shared keyboard guarantee in maintainer guidance and the host-handling mechanism in its owning module.

Comment thread browser/runtime/keyboard/AGENTS.md Outdated
Comment on lines +32 to +34
typed into a `leaf-text` stops at its host once the editor and the dispatcher have read
it there (`composing/text-field.js`), so the page never hears it bubble, and any other
Leaf listener that needs keys typed into a field listens in capture.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The host/editor/dispatcher sequence duplicates the implementation contract already in composing/text-field.js. The repository’s running-tend review rule asks maintainer guidance to state what other code relies on. Keep the propagation guarantee and capture requirement here:

Suggested change
typed into a `leaf-text` stops at its host once the editor and the dispatcher have read
it there (`composing/text-field.js`), so the page never hears it bubble, and any other
Leaf listener that needs keys typed into a field listens in capture.
typed into a Leaf field never bubbles to the page. Any other
Leaf listener that needs keys typed into a field listens in capture.

Leaf's page search, Page Map search and command reference search are
`searchField`s, `wa-input` elements whose native input sits in a shadow
root, so a page's shortcut check by tag or by `isContentEditable` saw a
`WA-INPUT` host and acted on every key typed there. The panel's Threads and
menu search is a native input, which a page's checks recognize but which
the page's author never wrote a check for.

The stop-and-read moves from text-field.js into field-keys.js, a
dependency-free module exporting `keepFieldKeys(host)` and
`readLeavingKeys`. `LeafText`'s constructor, `searchField()` and the
panel's search input call `keepFieldKeys`; the key dispatcher reads from
the same module. Package widgets that build a `searchField`, such as
lf-diff's file filter, keep their keys the same way.

The browser test, renamed to say the keys never bubble to the page (a page
listener in capture still hears them), adds a `/` page-search step: type,
then Escape closes the search. Without the `searchField` stop it fails with
"the page heard keys typed into the page search". page-behavior.md now
names comment box, draft editor or search box.

Part of #5023.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@max-sixty max-sixty changed the title Keep keys typed into Leaf's fields out of the page's listeners Keep keys typed into Leaf's fields from bubbling to the page Nov 2, 2026
@max-sixty
max-sixty marked this pull request as ready for review November 2, 2026 05:22
# Conflicts:
#	dist/kernel/kernel.json
#	dist/kernel/leaf.js
#	dist/kernel/runtime/bundle-portable.js
@max-sixty
max-sixty merged commit 8a4e213 into main Nov 2, 2026
3 checks passed
@max-sixty
max-sixty deleted the grind/issue-5023 branch November 2, 2026 05:46
leaf-agent added a commit that referenced this pull request Nov 4, 2026
…d state answers and deferred fault reports (#5334)

Every test in #5070 fails on main because three merged changes moved
contracts these tests still read the old way. Each repair updates only
the test, to the behavior its change intended. None moves product
behavior.

- **Control+Alt+r never pressed Latest (14 tests, plus the journey in
#5070's comment).** The test page's script listens for Control+Alt+r on
`window` and clicks the panel's Latest row notice. #5037 stops keys
typed into a Leaf field at the field so a page's bubble listeners never
hear them; its contract (`field-keys.js`) keeps capture listeners
hearing them. With an editor focused, the hook never ran, the revision
stayed deferred behind the composition, and `wait_for_revision(page, 2)`
timed out. The hook now listens in capture. Bisect: the composer case
passes at `1c38e1070` and times out at `8a4e213d0` (#5037).
-
**`test_a_mechanical_revision_failure_leaves_accepted_publication_ready`.**
It began as `StopIteration`, from the same missing press. With the press
fixed, it raised `KeyError: 'reading'`, because #5307 answers a tab that
names its held reading with a patch. The test now reads the new
reading's name from the patch's `["reading"]` change.
-
**`test_a_fresh_revision_caret_failure_does_not_strand_deferred_arrivals`.**
This one had a separate cause and was failing before #5037 with `assert
([])`. Bisecting over `7c1e4494ac..9a142d1` points to #4966, which
reports a fault in the next task. The test took the errors as soon as
the revision installed, so it now waits for the report with
`reported_browser_errors`.

Verification: all 16 tests listed in #5070 and
`tests/journeys/test_render_drafts.py::test_executable_revision_does_not_restore_a_new_draft_generation`
pass locally on this head (29 passed across `test_revisions.py` and the
two named tests). Before the change, the composer case failed on main
after its 120 s timeout.

Closes #5070 — automated triage

Co-authored-by: leaf-agent <[email protected]>
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.

Keys typed into a comment reach the page's own keyboard handlers

2 participants