Skip to content

add undo/redo test helpers - #92

Merged
feedthejim merged 1 commit into
mainfrom
undo-redo-helpers
Feb 12, 2021
Merged

feedthejim merged 1 commit into
mainfrom
undo-redo-helpers

Conversation

@feedthejim

Copy link
Copy Markdown

This adds helpers for undo/redo.

I feel like I uncovered a few bugs while trying it. WIll add more tests as I try to fix them.

@feedthejim
feedthejim requested a review from trueadm February 12, 2021 16:51
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 12, 2021
@feedthejim
feedthejim merged commit 467d48f into main Feb 12, 2021
@yangshun
yangshun deleted the undo-redo-helpers branch February 12, 2021 17:03
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 5, 2026
…de in a Markdown table cell

## Description

GFM splits a row on each pipe that no backslash escapes, then drops the backslash of each `\|` in a cell, code spans included. Inline code `a\|b` exported as `` `a\|b` `` and read back as `a|b`, and `a\\|b` exported with the pipe unescaped, so the row split and the table broke apart. Each pipe in inline code now gets a backslash of its own (`` `a\\\|b` ``). Code with an odd run of backslashes right before a pipe can't be written as a GFM code span in a cell, so it is written as `<code>` with its punctuation as character references (`<code>a&meta#92;&meta#124;b</code>`), which GitHub renders as that code and which the playground reads back as a code span.

## Test plan

### Before

```
$ vitest run --project unit packages/lexical-playground/__tests__/unit/MarkdownTransformers.test.ts -t "inline code with pipes"
     × round-trips inline code with pipes and backslashes in a cell: "a\\|b"
     × round-trips inline code with pipes and backslashes in a cell: "a\\\\|b"
     × round-trips inline code with pipes and backslashes in a cell: "\\|"
     × round-trips inline code with pipes and backslashes in a cell: "x`\\|`*y*"
      Tests  4 failed | 1 passed | 46 skipped (51)
```

### After

```
$ vitest run --project unit packages/lexical-markdown packages/lexical-playground packages/lexical-mdast packages/lexical-code-core
      Tests  1378 passed (1378)
```

micromark's GFM table parser reads each exported row as one cell holding the original code.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
etrepum pushed a commit to etrepum/lexical 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]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…iding flanking

## Description

The previous commit marked each format marker with sentinel noncharacters in the exported string. Those leaked into the strings that `exportChildren` and `exportFormat` hand to custom transformers, collided with the same characters in code spans, URLs and decorator text, and let the guard rewrite characters that a transformer wrote.

`$exportTextFormat` and the child exporters now return pieces (escaped text, raw output, openers and closers), and `joinPieces` turns them into Markdown wherever a string leaves the exporter: the transformer callbacks and the element exports. Only text this export escaped is written as a reference. When newlines are preserved, a raw backslash before the referenced character goes out as `&meta#92;` so that it does not escape the reference. Unpaired surrogates are left alone, since their references read back as U+FFFD. `escapeLinkPart` shares `toCharacterReference`.

## Test plan

`./node_modules/.bin/vitest run --project unit packages/lexical-markdown`

### Before

The new preserve-newlines case exported `x\&meta#98;**“y”**`, which re-imports as `x&meta#98;`.

### After

```
 Test Files  9 passed (9)
      Tests  866 passed (866)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…d export, and guard raw backslashes before markers

## Description

Export now decides whether a reference is needed with the same `isFlankedBy` rule that import's `isFlanking` uses, instead of a copy of it. When newlines are preserved, a raw backslash right before a marker went out as is and escaped it (`a\**b**` lost the bold); it now goes out as `&meta#92;`. `joinPieces` skips its setup when there are no markers, and the trailing-whitespace scan has an explicit bound.

## Test plan

`./node_modules/.bin/vitest run --project unit packages/lexical-markdown`

### After

```
 Test Files  9 passed (9)
      Tests  871 passed (871)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…pace, and guard raw backslashes before any markup

## Description

The previous commit placed export tags by CommonMark whitespace only, so a tag could land beside U+000B, which markdown-it counts as whitespace (`**\vx\v**` lost the bold there). Export goes back to trimming JavaScript whitespace, which holds CommonMark's and the characters some readers add; import keeps the spec's set. When newlines are preserved, a trailing raw backslash before any following markup (a marker, a reference, a code span, a link) now goes out as `&meta#92;`, in one place, and the reference analysis runs only when there are markers.

## Test plan

`./node_modules/.bin/vitest run --project unit packages/lexical-markdown`

### After

```
 Test Files  9 passed (9)
      Tests  871 passed (871)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
## Description

With `shouldPreserveNewLines`, the guard that writes a raw backslash before a marker as `&meta#92;` did so inside a bare URL too, so `see http://x.com/a\` before bold `b` exported as `see http://x.com/a&#92;**b**`, against the rule that no reference goes inside a bare URL, email or mention. The guard now skips those, as other references do. The importer's recursion into the text on either side of a match is shared by the format and text-match paths.

## Test plan

### Before

```
pnpm vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t "backslash reference inside"
     × writes no backslash reference inside a URL
```

### After

```
pnpm vitest run --project unit packages/lexical-markdown
      Tests  906 passed (906)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 31, 2026
…backslash before raw output, and keep all look-ahead text matches per line

## Description

- Emails: a reference that export writes inside an email (`[email protected]&meta#111;**“x”**`) cut `@lexical/mdast`'s link short at the reference, since micromark finds an email only in the text as written. The `@` of such an email now goes out as `&#64;` too, and `@lexical/mdast` links the whole decoded address, as cmark-gfm does.
- Backslash before raw output: with `shouldPreserveNewLines`, a raw backslash that ends text right before a code span or other raw output starting with ASCII punctuation or a line ending escaped it, so `a\` then code `x` came back as plain text. It now goes out as `&meta#92;`, as it already did before a marker.
- Text matches: import kept the first match per line only for the link syntax, so any other text-match transformer searched what was left of a long line again after each match. It now keeps the match of every transformer whose pattern does not look behind (no `^`, word boundary or lookbehind outside a character class, no `g`, `y` or `v` flag). One helper now shifts a kept match, and copies its named groups.

## Test plan

Command: `pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts`

### Before

With this commit's tests and the previous sources:

```
     × exports formatting beside an email, which both importers read back 20ms
     × exports formatting beside the start of an email, which both importers read back 2ms
     × exports formatting beside an email before a URL, which both importers read back 2ms
     × exports formatting beside a URL that holds an email, which both importers read back 1ms
     × keeps an email that holds a reference whole for @lexical/mdast 2ms
     × spoils a URL for the reference to a backslash before a code span 1ms
     × searches a line once for a text match that it does not hold 42ms
AssertionError: expected 5459 to be less than 520
```

### After

```
 Test Files  1 passed (1)
      Tests  778 passed (778)
```

On 30,000 random lines with custom text-match transformers, import gives the same result with the kept matches as with a fresh search after each match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
x-labb added a commit to x-labb/lexical that referenced this pull request Nov 20, 2026
…lash stays a literal backslash

## Description

Adds a unit test that `a&meta#92;` followed by `b` imports as `a\` and a soft line
break. The character reference is a literal backslash (CommonMark 0.31.2,
section 2.5), not an escape and not a hard break. Before the hard-break fix it
became a hard break and lost the backslash.

## Test plan

### Before

Source files from the base `main`:
`pnpm exec vitest --project unit --no-watch packages/lexical-markdown/src/__tests__/unit/HardLineBreakFromMarkdown.test.ts`

```
 × reads a character reference for a backslash as a literal backslash
AssertionError: expected 'a<hard \>b' to be 'a\<soft>b' // Object.is equality
      Tests  7 failed | 3 passed (10)
```

### After

```
      Tests  10 passed (10)
```

`pnpm exec vitest --project unit --no-watch packages/lexical-markdown packages/lexical-mdast packages/lexical-playground/__tests__/unit`:
57 test files, 1844 tests passed. `tsc -p tsconfig.test.json`, Prettier and
`pnpm run check-changesets` pass. Only unit tests (Node) were run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants