Skip to content

[mdast-editor] Bug Fix: Don't crash the toolbar when select all + delete empties the document - #38

Closed
etrepum wants to merge 1 commit into
mainfrom
claude/fix-mdast-editor-select-all-delete-75ptfx
Closed

etrepum wants to merge 1 commit into
mainfrom
claude/fix-mdast-editor-select-all-delete-75ptfx

Conversation

@etrepum

@etrepum etrepum commented Sep 26, 2026

Copy link
Copy Markdown
Owner

Requested by Bob · project thread

Description

In the mdast-editor dev example, selecting all of the rich text and pressing Backspace can remove every child of the root (the footnotes slot keeps its content) and leave the selection anchored on the root element itself. ToolbarStateExtension then fell back to anchorNode.getTopLevelElementOrThrow(), which throws for the root ("root nodes are not top level elements") from inside the editor-state signal, surfacing as an uncaught exception.

The toolbar now uses only $findMatchingParent to find the top level block and treats a root-anchored selection as a paragraph.

Test plan

New unit test dev-examples/mdast-editor/src/__tests__/unit/ToolbarState.test.ts watches the toolbar's blockType signal, runs select all + delete on a document with a footnote and a code block, checks the selection lands on an empty root, then types into it.

Before

$ npx vitest run --project unit dev-examples/mdast-editor/src/__tests__/unit/ToolbarState.test.ts
Error: getTopLevelElementOrThrow: root nodes are not top level elements
 ❯ RootNode.getTopLevelElementOrThrow packages/lexical/src/nodes/LexicalRootNode.ts:40:5
 ❯ $readSelectionState dev-examples/mdast-editor/src/extensions/ToolbarStateExtension.ts:56:22
      Tests  1 failed (1)

After

$ npx vitest run --project unit dev-examples/mdast-editor
      Tests  44 passed (44)

Not exercised by hand in a browser.

🤖 Generated with Claude Code


Generated by Claude Code

…ete empties the document

## Description

In the mdast-editor dev example, selecting all of the rich text and
pressing Backspace can remove every child of the root (the footnotes
slot keeps its content) and leave the selection anchored on the root
element itself. `ToolbarStateExtension` then fell back to
`anchorNode.getTopLevelElementOrThrow()`, which throws for the root
("root nodes are not top level elements") from inside the editor-state
signal, surfacing as an uncaught exception.

The toolbar now uses only `$findMatchingParent` to find the top level
block and treats a root-anchored selection as a paragraph.

## Test plan

New unit test `dev-examples/mdast-editor/src/__tests__/unit/ToolbarState.test.ts`
watches the toolbar's `blockType` signal, runs select all + delete on a
document with a footnote and a code block, checks the selection lands on
an empty root, then types into it.

### Before

```
$ npx vitest run --project unit dev-examples/mdast-editor/src/__tests__/unit/ToolbarState.test.ts
Error: getTopLevelElementOrThrow: root nodes are not top level elements
 ❯ RootNode.getTopLevelElementOrThrow packages/lexical/src/nodes/LexicalRootNode.ts:40:5
 ❯ $readSelectionState dev-examples/mdast-editor/src/extensions/ToolbarStateExtension.ts:56:22
      Tests  1 failed (1)
```

### After

```
$ npx vitest run --project unit dev-examples/mdast-editor
      Tests  44 passed (44)
```

Not exercised by hand in a browser.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@etrepum etrepum self-assigned this Sep 26, 2026
@cloudflare-workers-and-pages

Copy link
Copy Markdown

@etrepum etrepum closed this Sep 26, 2026
etrepum pushed a commit that referenced this pull request Oct 21, 2026
…TML apart in cells and escape nested attributes once

## Description

In a table cell:

- Two blocks embedded in raw HTML (a callout's two body paragraphs) were written with nothing between them, so they read back as one (`onetwo`); they are now set apart by a `<br>`. Only one of the two line ends `rawHtmlBlock` puts beside embedded blocks was dropped, so the other became a stray `&#10;`; all of them are now dropped.
- The phrasing of a directive or an MDX element exported as a block was rewritten twice, so an attribute of an element inside it had its `&` escaped twice and read back as `&#38;`. It is now rewritten once.
- `<style-box>` and `<script-x>` were read as raw text elements running to the end of the HTML, a comment closed at once (`<!-->`) ran on to the next `-->`, and an unclosed tag rescanned every later `<`, which took seconds on a long run of them.
- Test helpers that take callbacks needing an update context carry the `$` prefix.

## Test plan

### Before

```
pnpm exec vitest run --project unit packages/lexical-mdast
       × keeps the blocks of a block element apart
       × keeps {"type":"html","value":"<style-box><pre>a\nb</pre></style-box><!--><pre>c\nd</pre><!---->"} a block exports to in a cell on its row
       × reads long runs of spaces in a cell in linear time 9489ms
         × keeps the attributes of what a leafDirective holds
         × keeps the attributes of what a containerDirective holds
         × keeps the attributes of what a mdxJsxFlowElement holds
AssertionError: expected { title: 'a&#38;b&#10;c' } to deeply equal { title: 'a&b\nc' }
      Tests  6 failed | 344 passed (350)
```

### After

```
pnpm exec vitest run --project unit packages/lexical-mdast
      Tests  350 passed (350)
```

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 22, 2026
…d escape only real references

## Description

The hard line break marker was set apart only for a line attached to the root, so `$generateNodesFromMarkdownString`, which builds nodes outside it, kept `foo\` and `baz  ` as text with no break. It is now set apart for any line with a parent, as the rest of the import does. Export escaped the `&` of any `&name;` and of numbers of any length, in text (`\&`) and in link destinations and titles (`&#38;`), though only real HTML entity names and numbers of up to seven decimal or six hex digits read as references; now both escape only those, as import reads them.

## Test plan

### Before

```
npx vitest run --project unit packages/lexical-markdown/src/__tests__/unit/EscapedBackslashHardLineBreak.test.ts packages/lexical-markdown/src/__tests__/unit/CodeSpanEscapes.test.ts packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
     × reads a hard line break in nodes generated outside the root 10ms
     × escapes the & of text ["&nosuch; &#12345678; &#x1234567;"] only where it reads as a reference 7ms
     × preserves "https://example.com/?a&nosuch;&#12345678;" 12ms
      Tests  3 failed | 647 passed (650)
```

### After

```
npx vitest run --project unit packages/lexical-markdown packages/lexical-playground/__tests__/unit
      Tests  1050 passed (1050)
```

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit that referenced this pull request Oct 28, 2026
…rmats closed in inline elements, and match custom patterns after an escaped character

## Description

- Export writes a character reference instead of a backslash right after a format closed by text inside or before an inline element such as a mark, as it already did between sibling text nodes, so `*Q&A.*&#38;copy;` keeps its italic when the italic text is highlighted. When preserving newlines, a literal backslash there is written as `&meta#92;` for the same reason.
- When an import pattern's match starts at an escaped character, matching starts again from the character after it as if the text began there. A custom pattern that matches the character before its syntax, such as `(^|[^\w])#(\w+)` on `\(#x)`, matches again as it did before. The `importRegExp` docs describe the skipping.
- The playground's image transformer no longer reads `![a\](b)` as an image.
- The nested list escaping test asserts the whole export, and a custom transformer test is named for what it checks.

## Test plan

### After

`vitest run --project unit packages/lexical-markdown packages/lexical-playground/__tests__/unit`

```
      Tests  1007 passed (1007)
```

Co-Authored-By: Claude Opus 5.5 <[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