Skip to content

Have keys at a draft's markup act on the blocks it shows, and fuzz the field's keys - #7112

Merged
max-sixty merged 3 commits into
mainfrom
draft-blank-lines
Nov 25, 2026
Merged

max-sixty merged 3 commits into
mainfrom
draft-blank-lines

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Keys at the edge of a draft's words now act on the blocks the draft shows, rather than on the Markdown it hides. Backspace at a heading's, item's or quote's first word takes its marker, and a second press joins it to the block above; Delete does the same to the block below. Return then Backspace gives the words back, Backspace at code's first line keeps the fence, Return inside a heading moves the words after it to a paragraph below, Home stops at the first word, words typed or pasted in the gap between paragraphs stand apart, a paste stays in its quote or item, and a double press on a bold first word selects the word. A click in a table cell whose row escapes a pipe now lands in that cell.

The rule is one section of field-editor.js ("Keys at the edge of a line's shown words"). Where an edit's form depends on how the words around it read, the key asks the parser, reading the whole draft with the candidate edit made: whether a blank line can go, whether typed words need a gap, whether words moved to a new line need an escape or a blank line after them, and how to write a hard break. Of the file's 1,091 added lines, 256 are comments; the tests add 728. This is the cost of keeping CodeMirror rather than moving the editor to ProseMirror, which max-sixty chose on the decision page.

A seeded fuzzer in field-editor.test.mjs holds every key to the field's promises over generated drafts: no throw, one Undo restores, saving shows what the field showed, and a key shows no markup as words. It runs 1,500 drafts in test:runtime and any number from pnpm run fuzz:editor. Over 400,000 drafts the hard checks pass and the markup check reports ten cases, none a key acting on hidden markup: markdown-it ending a quote's or item's lazy line at 2., which CommonMark keeps as words, a paste into a link's address that a later join closes up, and one join that makes a # and a hard break's \ one heading line.

To weigh: Return between a paragraph's words now keeps the space beside the hard break it writes; an empty item or quote line beside a paragraph goes whole under Backspace or Delete; and words typed just after a hard break that would open a block there, as - or = would, are escaped and stay in the paragraph, as Return already does for the words it moves.

Codex reviewed the change seven times. The commit message lists what is left, chiefly that composing with an input method on a gap still joins the words to the paragraphs around it.

Tests: field-editor.test.mjs (case table, fuzzer), body-text.test.mjs, and test_keys_at_the_edge_of_a_draft_s_words_act_on_what_it_shows in Chromium and Firefox; test:runtime and check:browser pass.

Part of #7077, whose browser stage (Home, double press and arrows driven at random in Chromium and Firefox) is still to come.

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

…es a pipe

Clicking the second cell of a Markdown table row such as `| \| | \| |`,
in a draft or any body Leaf reads as a file, opened the editor with the
caret in the first cell's source. The table reads `\|` as a pipe in a
cell's words and drops its backslash, so a cell's words no longer appear
verbatim on its row. Placement aligned the rest of the row against those
words, and the aligner matched a cell's `|` to the pipe between cells,
or to the escaped pipe of the cell before it. The field editor reads
the same placement for its marks.

A cell's words now stand at the first place on its row, past the cell
before it, where they appear with a `\` before each `|`, and each pipe
maps to its two source characters. An unescaped `|` is always a pipe
between cells, so the search can't land on one.

Tests: a case in `body-text.test.mjs` that places each cell of rows
holding `\|`, several escaped pipes in one cell, `\*`, an escaped
backtick and a code span with a pipe, and puts a caret at the end of
each cell inside that cell's source; it fails before the change.
`pnpm run test:runtime`, `pnpm run check:browser` and the shared
Markdown body cases in pytest pass. Python reads only a body's words,
which already agree with the browser's for these rows, so the shared
cases need no new case.

> _This was written by Claude Code on behalf of max-sixty_
…e field's keys

A key at the edge of a line's words acted on the source characters the
preview hides or dims there, one at a time, and turned them into words:
Backspace at a heading's first word left `#Title`, at an item's left
`-second item`, Delete before a quote left `Before quote.> Quoted`,
Return then Backspace left `Alpha\one`, Backspace at code's first line
moved it into the fence's language name, and Home on a heading went
before its `# `. A search for surprises after #7076 found ten such
cases. The maintainer chose to keep CodeMirror and build one rule for
them rather than move the draft editor to ProseMirror, which handled
most of them but writes a draft's Markdown afresh on every save.

The rule (`field-editor.js`, "Keys at the edge of a line's shown
words"): Backspace at a block's words takes its innermost marker first,
a quote's with its first paragraph's, then the blank lines the field
draws one at a time, then joins the block to the one before; Delete does
the same to the block after. A blank line between blocks goes only where
the blocks around it keep their kinds and lines without it. A line break
inside a block goes with its markup, a hard break's backslash and the
next line's markers. Code, a rule, a table and HTML are never joined to
another block, a fence's lines stay whole, and inside a table or HTML
block keys edit the source as characters. Return at a heading's, rule's
or code's start opens a paragraph above, and inside a heading's words
moves the words after it to a paragraph below the heading, past a setext
heading's underline, and out of any link or emphasis the caret stands
in; among a construct's markup it breaks outside it, and never inside a
code span or a picture; it keeps the spaces beside the break, so
Backspace gives the words back, and escapes words that would open a
block at the new line's start. Home stops at a line's first word. Words
typed or pasted in the gap between two blocks stand as blocks of their
own, the parser deciding which side needs a blank line, and words typed
after a hard break's backslash go before it. A paste in a quote or item
stays in it. A double press selects the word its first press put the
caret in, since that press reveals the `**` beside a bold first word and
moves the word. Where an edit's form depends on how the words around it
read, the parser decides, reading the whole draft with the candidate
edit made: whether a blank line can go, whether typed words need a gap,
whether the words a Return moves to a new line need their first mark
escaped or a blank line after them, and which hard break (`\`, `\\` or a
space and `\`) Return or an unwrapped item writes, since a link's
address takes a backslash as its own. Rules that read a line alone, in
place of the parser, kept failing in context: inside a list item, after
a lazy line, beside a link.

This reverses one decision: Return between a paragraph's words wrote its
hard break in place of the spaces there, and now keeps them, so Return
then Backspace restores the source. Return at a task's words now opens a
plain item above, as an empty task item shows its brackets, and a list
directly after a paragraph takes a blank line before an empty first
item, which can't interrupt the paragraph. An empty item or quote line
next to a block goes whole under Backspace or Delete, rather than taking
the block's first line in, which left the block's other lines to the
parser's lazy reading: a setext heading lost its underline that way.
Words typed on the line a hard break opens at a draft's end continue the
paragraph, their first mark escaped where it would open a block there,
as Return already escapes the words it moves.

A seeded fuzzer in `field-editor.test.mjs` (#7077) generates drafts and
key sequences in both readings and holds each key to the field's
promises: no throw, one Undo restores the words, a body saves as it
shows and saves again unchanged, and, where the caret stands beside no
markup the field shows there, a key that types nothing shows no
character the draft didn't, takes at most one shown letter, and Return
takes none. It runs 1,500 drafts in `test:runtime` (under a second) and
any number from `pnpm run fuzz:editor`. Its seeds come from an exact
32-bit generator (`tests/runtime/seeded.mjs`), which
`anchor-placement.test.mjs` now shares: the floating-point one it
replaced fell into short cycles. Over 400,000 drafts (twenty seeds of
20,000), the hard checks pass and the markup check reports ten cases of
three kinds: markdown-it ending a quote's or item's lazy line at `2.`,
which CommonMark keeps as words (three); a paste into a link's or
picture's address that a later join closes up, whose letters the check
counts as lost though the address holds them (six); and a join that
makes a `#` and a hard break's `\` one heading line (one). Its
exclusions name parser readings no key can avoid, such as a list
numbered past 1 that can't interrupt a paragraph.

A caret placed before an escaped character now lands before its
backslash, where the parser recorded the escape, and a click in a table
cell whose row escapes a pipe lands in that cell.

Codex reviewed the change seven times. Each review from the second to
the sixth found six to ten regressions in the previous round's fixes;
the fixes that held were those that ask the parser about the whole
draft, and the seventh found two, in rare drafts. Left as they are:
composing with an input method on a gap still joins the words to the
paragraphs around it; Backspace at a paragraph under an underlined
heading does nothing, so Return then Backspace inside such a heading
doesn't give the words back, and Return at a line boundary inside an
underlined heading of several lines doesn't split it; Return then
Backspace inside a bare link's address leaves the space that ended the
address; rare drafts that a key still leaves reading differently, such
as a multiline paste after a hard break that ends the draft, a code
paste in a quote, an entity in a heading's link words, and Return before
`_bar_`, which then reads as emphasis; taking thousands of empty
hard-break lines at once costs a scan per line; and a key that removes
an empty line, escapes or unwraps, or the first letter typed on an empty
line, parses the draft once or twice more than other typing does.

Tests: the fuzzer, a table of the edge cases in `field-editor.test.mjs`,
`body-text.test.mjs`, and
`test_keys_at_the_edge_of_a_draft_s_words_act_on_what_it_shows` (Home,
double press, paste and typing in a gap, in Chromium and Firefox).
`test_document_edit_enter_adds_a_line_and_modified_enter_saves_exact_source`
now expects the kept space.

Part of #7077, whose browser stage (Home, double press and arrows driven
at random in Chromium and Firefox) is still to come.
@max-sixty
max-sixty merged commit a1ac633 into main Nov 25, 2026
7 checks passed
@max-sixty
max-sixty deleted the draft-blank-lines branch November 25, 2026 03:20
leaf-agent added a commit that referenced this pull request Nov 25, 2026
#7112 changed two keys in a draft: a line holding nothing past its
markers, as a new list item does, now goes whole under Backspace
(`field-editor.js`, "Keys at the edge of a line's shown words"), and
Home stops at a line's first word. Four `tests/browser` tests still
drove the old keys, so they failed on `main` from
a1ac633 ([run
45539313453](https://github.com/max-sixty/leaf/actions/runs/45539313453));
a pull request's CI doesn't run that tier, so #7112's own checks passed.
This keeps #7112's behavior and changes only how each test reaches the
state it checks:

- `test_markdown_decoration_keeps_source_editable`: the marker of a
continued item is changed by selecting the `-` and typing `*`, rather
than by Backspace leaving `- First\n `.
- `test_a_caret_is_drawn_at_a_lines_end_and_after_a_list_bullet`: drops
the `- one\n-` step, a state Backspace no longer leaves; the caret ink
is still checked after continuing the list and after arrows.
- `test_a_new_line_in_an_open_fence_starts_in_its_code`: the retyped
line types over what continuing the list inserted, instead of
backspacing it away (which now removes the whole line).
- `test_a_draft_draws_a_list_as_the_message_will`: ArrowLeft from the
item's first word puts the caret on its marker, which Home no longer
does.

Each failed before the change (the first reproduced locally with the CI
message) and passes after it in Chromium and Firefox where parametrized:
6 passed, 8 subtests passed.

The same run's fifth failure,
`test_back_returns_to_the_card_a_later_open_in_panel_left[wide-browser]`,
is unrelated to drafts and is tracked from #7120.

Refs #7120 — automated triage

Co-authored-by: leaf-agent <[email protected]>
max-sixty added a commit that referenced this pull request Nov 25, 2026
…efore (#7117)

Backspace and Delete at the edge of a draft's words, Return's line break
inside a paragraph or heading, and typing after a hard break now propose
their edits as candidates, and the editor makes the first under which
the draft still reads as before (`keeps` in `field-editor.js`). The
draft passes when it shows no character it didn't, apart from those the
user types; loses none, apart from those the key deletes or the user's
typing turns into markup; and keeps the blocks above and below the
edited lines, lists and quotes included, of the same kinds on the same
lines. Where no candidate passes, the key changes nothing.

It fixes four cases `main` gets wrong: Return in `foo_bar_` turned the
underscores into emphasis; typing `1. hi` after a hard break left the
break's `\` above a list; Return in the setext heading `| xa | b |` over
`---` made a one-column table that dropped `b`; and Return inside `#
Hello #` moved the closing `#` below as words. The check replaces four
rules that each guessed at one context (`sameBlocks`, `unwrap`'s guess
of whether the paragraph above takes a line in, the heading split's
paragraph predicate, and the paragraph-span predicate on Return's
break). It is not a simplification: the file's code lines go from 2,255
to 2,290 and its decision points from 715 to 734, since two Codex
reviews found eleven drafts where the first version erred and the fixes
taught it to compare containers and the remainder of a block an edit
splits.

To weigh: where no candidate passes, Return now changes nothing, where
`main` made an unchecked edit; in the tests and 6,000 fuzzed drafts only
the table case reached that. Return's other branches, Shift+Return, Tab
and typing on a blank line stay off the check, since their edits change
the reading by design: over 5,000 fuzzed drafts it would refuse 106
Return, 56 Shift+Return and 14 Tab edits the fuzzer accepts. Each
checked candidate renders the draft (`markdownWords`); on a 16 KB draft
in Chromium, Backspace, Delete and Return at these edges take 2 to 3 ms
more than on `main` beside the existing 10 ms parse, and a key typed
among the marks that start a line after a hard break, which `main`
didn't check, takes 13 ms more; typing further along such a line costs
what it does on `main`. Two cases stay as on `main`: Delete joining `#
**A` to `**B` over `C**` moves the bold, and Backspace at `# ▏# T` takes
the visible `#`; comparing the words of each surviving source range,
rather than counts of characters, would catch both.

max-sixty asked for this after #7112 landed. Tests: the field-editor
tests with sixteen new cases, `test:runtime` (1,130 of 1,130), the
draft-edge browser test in Chromium and Firefox, `pnpm run
check:browser`, and the fuzzer over 365,000 drafts across the three
revisions; the final revision passed 100,000 on four seeds.

> _This was written by Claude Code on behalf of max-sixty_
max-sixty pushed a commit that referenced this pull request Nov 25, 2026
…7112 gave Backspace (#7183)

`test_shift_enter_and_tab_keep_a_draft_in_the_containers_the_message_will_hold`
failed in both engines because two of its cases used Backspace in a way
#7112 changed on purpose. #7112 (`field-editor.js`, "Keys at the edge of
a line's shown words") made Backspace take a whole line that holds
nothing but its markers. The two cases still removed a new item's marker
one character at a time. Under the new keys their extra presses also
deleted the words of the item above: `b` in the nested case, `a` in the
fence case. So the missing item in the issue came from the test's own
keystrokes. The keys did what #7112 says they do. This keeps #7112's
behavior and changes only how the two cases reach the state they check,
the same way #7123 updated four `tests/browser` tests:

- **fence in an item**: `- a⏎```⏎x⏎```⏎more` → `- a\n- ```\n x\n ```\n
more`. The fence now opens on the continued item's line instead of after
a marker was erased. The check is the same: the fence's code and closing
marks stay in the item.
- **outdenting a nested item**: `1. a⏎b⏎⇥c⏎⏎d` → `1. a\n2. b\n 1. c\n3.
d`. The case nests the item with Tab instead of deleting and retyping a
marker. The check is the same: Shift+Enter on an empty nested item takes
it back out to the outer list.

The other eight cases are unchanged. The journey failed locally in
Chromium and Firefox with the issue's diff and passes after the change
(2 passed, 4 subtests passed, `--run-nightly` selection by node id).

Closes #7173 — 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.

2 participants