Skip to content

fix(muya): skip empty sibling containers in caret navigation (#4644 follow-up) - #4715

Merged
Jocs merged 1 commit into
marktext:developfrom
FurkaanBoraa:fix/4644-list-nav-skip-empty-item
Jun 25, 2026
Merged

Jocs merged 1 commit into
marktext:developfrom
FurkaanBoraa:fix/4644-list-nav-skip-empty-item

Conversation

@FurkaanBoraa

Copy link
Copy Markdown
Contributor

Follow-up to #4660, which closed #4644.

#4660 fixed the editing path that produced a childless list item (Enter on the empty first paragraph of a loose list item). But a childless list item can also come straight from Markdown, which #4660 doesn't touch — so the original symptom still reproduces:

muya.setContent('* A\n*  \n* B')   // the middle item parses to a list-item with 0 children

With the caret in "B", ArrowUp no longer moves up to "A" (and ArrowDown from "A" can't reach "B"). The cause is exactly the one #4660's commit message names: previousContentInContext / nextContentInContext return null when the adjacent sibling is an empty container with no content descendant.

Fix

Make the two navigation helpers walk the previous/next siblings and skip empty containers, falling through to the parent only when no sibling holds content. Symmetric for both directions, so navigation is robust regardless of how the empty container arises.

Tests

Adds ArrowUp/ArrowDown regression tests in arrowNavigation.spec.ts (a list with an empty middle item via setContent('* A\n* \n* B')); both fail without this change. Full muya suite (unit + conformance), typecheck, lint, circular-dep, and build all pass locally.

Context: I left a heads-up on the issue first — #4644 (comment)

marktext#4644)

previousContentInContext / nextContentInContext returned null when the
adjacent sibling was an empty container with no content descendant — e.g. a
list item whose only paragraph was removed during editing, or an empty
`*  ` item parsed from markdown. That left ArrowUp/ArrowDown unable to cross
the empty item, so the caret got stuck and could only be moved by clicking.

Walk the previous/next siblings and skip empty containers, falling through to
the parent only when no sibling holds content. Add regression tests covering
ArrowUp and ArrowDown across an empty list item (they fail without the fix).
@Jocs

Jocs commented Jun 25, 2026

Copy link
Copy Markdown
Member

This PR fix the same issue as #4691 ? also solve the empty list item.

@FurkaanBoraa

Copy link
Copy Markdown
Contributor Author

Good question — they're complementary, not the same.

#4691 fixes serialization (stateToMarkdown.ts): an empty list item now terminates its own Markdown line so it round-trips instead of collapsing into - - - (a thematic break) on reload (#4679).

This PR fixes caret navigation (treeNode.ts): previousContentInContext / nextContentInContext returned null when an adjacent sibling is a childless list item, so ArrowUp/ArrowDown couldn't cross it and the caret got stuck (#4644).

Different files, different layers, no overlap — and they reinforce each other: since #4691 keeps an empty list item as a valid, preserved state, navigation has to handle it. The navigation bug reproduces independently of serialization:

muya.setContent('* A\n*  \n* B')  // childless middle item
// caret in "B" → ArrowUp no longer reaches "A"

No shared files, so they don't conflict — happy to coordinate ordering if you'd prefer one to land first.

@Jocs

Jocs commented Jun 25, 2026

Copy link
Copy Markdown
Member

How to produce this issue, in 0.20.0-beta.2?

@FurkaanBoraa

Copy link
Copy Markdown
Contributor Author

Confirmed on 0.20.0-beta.2.

The original report's keystroke sequence no longer triggers it on beta.2 — that editing path was fixed by #4660. It still reproduces by loading the empty list item from Markdown, which #4660 doesn't touch:

  1. Open a .md file containing a tight list whose middle item is empty:
    * A
    * 
    * B
    
    (the middle line is * — a bullet with no text)
  2. Click into B and press ↑ → the caret stays in B instead of moving up to A.
  3. Click into A and press ↓ → it skips B entirely and drops into a new empty paragraph below, appending a blank line to the document.

The empty item parses to a list-item with no child block, so previous/nextContentInContext return null when crossing it: arrowHandler then parks the caret (↑) or appends a trailing paragraph (↓). The affected engine code (treeNode.ts) is identical in beta.2 and current develop.

@Jocs

Jocs commented Jun 25, 2026

Copy link
Copy Markdown
Member

Confirmed on 0.20.0-beta.2.

The original report's keystroke sequence no longer triggers it on beta.2 — that editing path was fixed by #4660. It still reproduces by loading the empty list item from Markdown, which #4660 doesn't touch:

  1. Open a .md file containing a tight list whose middle item is empty:

    * A
    * 
    * B
    

    (the middle line is * — a bullet with no text)

  2. Click into B and press ↑ → the caret stays in B instead of moving up to A.

  3. Click into A and press ↓ → it skips B entirely and drops into a new empty paragraph below, appending a blank line to the document.

The empty item parses to a list-item with no child block, so previous/nextContentInContext return null when crossing it: arrowHandler then parks the caret (↑) or appends a trailing paragraph (↓). The affected engine code (treeNode.ts) is identical in beta.2 and current develop.

So the root cause is markdown to state? #4691 (review)

@FurkaanBoraa

Copy link
Copy Markdown
Contributor Author

Yes — the childless list-item comes from markdownToState. The block-end handler already synthesizes an empty paragraph for an empty blockquote (#1735), but there's no equivalent for an empty list-item / task-list-item, so * parses to a list-item with no children — unlike a typed empty item, which keeps an empty paragraph.

So your call in #4691 is the right altitude: mirroring the blockquote handling for empty list items fixes #4679 and this on-load navigation symptom at the source.

This PR is then the defense-in-depth guard you mentioned — childless containers can still arrive via OT ops / paste / programmatic setContent, so having previous/nextContentInContext skip them is a useful invariant-independent guard. Happy to keep it framed purely as that, or stand it down in favor of the parser fix — whichever you prefer.

@Jocs

Jocs commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

I think this defensive fix can be retained to avoid generating empty list items and empty quote blocks due to other missed spots elsewhere.

@Jocs
Jocs merged commit 479f164 into marktext:develop Jun 25, 2026
7 checks passed
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.

[Bug] keyboard navigation issue with lists

2 participants