Skip to content

Resolve selection after selection mutates from reconcilation - #97

Merged
trueadm merged 2 commits into
mainfrom
resolve-selection
Feb 16, 2021
Merged

trueadm merged 2 commits into
mainfrom
resolve-selection

Conversation

@trueadm

@trueadm trueadm commented Feb 16, 2021

Copy link
Copy Markdown
Collaborator

Sometimes, when a DOM node that has selection is moved or mutated, it can cause selection offsets to reset. We can detect this and attempt to resolve it so that we recover from these cases.

@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 16, 2021
@trueadm
trueadm merged commit b739385 into main Feb 16, 2021
@trueadm
trueadm deleted the resolve-selection branch February 16, 2021 16:08
acywatson pushed a commit that referenced this pull request Apr 9, 2022
* Resolve selection after selection mutates from reconcilation

* Do not include CSS change
etrepum added a commit to etrepum/lexical that referenced this pull request Oct 27, 2026
…wn syntax

## Description

Markdown export escapes harmless punctuation and emits unnecessary references
around formatting changes. Check the emitted syntax before retaining literal
escapes, and repair actual emphasis and strikethrough delimiter boundaries.
Keep real links and opaque transformer output intact, including document-wide
reference definitions. Use CommonMark Unicode punctuation rules consistently
when importing formatting.

## Test plan

### Before

Exporting bold text around an inline code span produced:

```markdown
**&meta#97;`b`&meta#99;**
```

Plain text exports included `AT\&T`, `a \< b`, and `\[label\]`.

### After

The corresponding exports are `**a`b`c**`, `AT&T`, `a < b`, and `[label]`.
All 15 changed existing output expectations preserve full GFM text,
formatting, link and block semantics.

```
vitest run --project unit --project scripts-unit packages/lexical-markdown scripts/__tests__/unit/treeShakingSource.test.ts --maxWorkers=1
Test Files  10 passed (10)
Tests  1263 passed (1263)

vitest run --project browser packages/lexical-markdown/src/__tests__/browser packages/lexical-playground/src/__tests__/browser/MarkdownInlineShortcuts.test.ts
Test Files  3 passed (3)
Tests  43 passed (43)
```

Chromium used the system browser. Firefox, WebKit, packed npm and www builds
were not rerun locally for this follow-up. Source and test TypeScript checks,
ESLint and Prettier pass.
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…hanges its neighbour

## Description

`joinPieces` decided each marker run against the characters before any reference was written. A one-character piece written as a reference for one run reads as punctuation at both ends, which could stop the run on its other side from opening or closing (`x**&meta#97;***“b”*` lost the bold). Runs beside such a piece are now decided again, through a worklist so each piece is revisited at most twice and the join stays linear.

Decorator text now counts as text a reference can stand in for, U+0000 is left alone like unpaired surrogates (both read back as U+FFFD), and the changeset no longer implies every emoji counts as punctuation.

## Test plan

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

### Before

`[plain 'x', bold 'a', italic '“b”']` exported as `x**&meta#97;***“b”*`, which re-imports with the bold lost.

### After

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to haripetrov-stack/lexical that referenced this pull request Oct 30, 2026
…ctuation, and read astral characters whole

## Description

With Unicode punctuation (and the backslash) counted as punctuation on import, the exporter's intraword markers no longer round-trip when the formatted text starts or ends with punctuation next to a letter: plain `a` then bold `*` exported as `a**\***`, which CommonMark (and now the importer) reads as literal asterisks. Seven MarkdownEscaping tests from meta#9481 failed after merging main.

The exporter now writes the letter beside such a marker as a numeric character reference (`&meta#97;**\***`), whose `&` or `;` is punctuation, so the marker flanks. Flanking checks in import and the typing shortcuts also read the characters around a marker by code point, so an emoji (category So) outside the Basic Multilingual Plane counts as punctuation.

## Changeset

`.changeset/markdown-unicode-punctuation-flanking.md`

## Test plan

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

### Before

```
 Test Files  1 failed | 8 passed (9)
      Tests  7 failed | 844 passed (851)
```

(`MarkdownEscaping.test.ts > preserves existing formatted markers after plain text` and `preserves formatted backslashes next to plain text`, e.g. `expected 'a*****' to be 'a*'`.)

### After

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

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 reference letters around an underscore between letters

## Description

The importer's rule of 3 had been changed to refuse a pairing only when both the original and the remaining run lengths refuse it. That let a leftover `**` from a mixed `***` run pair with a later `*`, so Lexical's own export of italic `)`, bold `(` and bold italic `)`, `*)***(*)***`, re-imported as three italics and a literal `**`. Main read it back exactly. This restores main's remaining-length rule (meta#4895). Split text still gets the character beyond a cut delimiter run, and that walk now stops at an escaped delimiter.

The exporter decided whether a character reference helps by flanking alone, which ignores that `_` cannot open or close between two letters. With underscore italics, plain `a`, italic `b` and plain `c` exported as `a_b_c` and imported as plain text. The decision now uses the same emphasis rules as import, so it exports `&meta#97;_b_&meta#99;` and round-trips.

The importer also uses the shared `isEscaped` from utils instead of two local copies.

## Test plan

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t "between punctuation"
       × round-trips italic then bold italic between punctuation
      Tests  1 failed | 734 skipped (735)
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t "underscore italic between letters"
     × writes references around an underscore italic between letters
      Tests  1 failed | 735 skipped (736)
```

### After

```
$ npx vitest run packages/lexical-markdown packages/lexical-mdast scripts/__tests__/unit/treeShakingSource.test.ts
 Test Files  18 passed (18)
      Tests  1571 passed (1571)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 30, 2026
…eaders such as @lexical/mdast also format

## Description

Export decided where to write a character reference by CommonMark's flanking rules alone. micromark, which `@lexical/mdast` imports with, reads a character outside the Basic Multilingual Plane one UTF-16 unit at a time, so an emoji beside a marker is a letter to it. Shapes that CommonMark formats then came back with literal markers from `@lexical/mdast`: plain `😀` then bold `“b”` exported as `😀**“b”**`, and italic `😀 😀` then bold `a“` as `*😀 😀***&meta#97;“**`, whose `**` micromark cannot open.

Export now requires each marker to open and close for both readers. An emoji beside a marker can take a reference too (`&#128512;**“b”**`), and when a reference written for the closing side of a run that also opens changes what its opener sees, the run is decided again. Exporting 10,000 random paragraphs and importing them with `@lexical/mdast`, misread paragraphs fall from 2,015 (main) and 1,008 (previous head) to 318, none of them newly broken, and round trips through `@lexical/markdown` gain no failures.

## Test plan

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t "both importers"
     × exports [["😀",0],["“b”",1]] so that both importers read it back
     × exports [["a",1],["😀",2]] so that both importers read it back
      Tests  2 failed | 737 skipped (739)
```

### After

```
$ npx vitest run packages/lexical-markdown packages/lexical-mdast scripts/__tests__/unit/treeShakingSource.test.ts
 Test Files  18 passed (18)
      Tests  1574 passed (1574)
```

Co-Authored-By: Claude Opus 5.5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Oct 31, 2026
…ling the link

## Description

Export wrote no character reference inside a bare URL or `www.` host, or for the letter right before a scheme, so formatting that needed one there was lost. It also wrote markers and code spans right after a URL, which GFM readers (micromark for `@lexical/mdast`, and cmark-gfm) take into the link, so the formatting came back as literal markers inside a link: `**www.x.co**a` and `see https://x.com/a**“x”**`.

Export now writes the reference, and where a URL that GFM readers would link would hold one, or would take in the markers, code span or other raw output after it, it also writes the `.` of `www.` or the `:` after the scheme as a reference (`**www&#46;x.co**a`, `see https&#58;//x.com/&meta#97;**“x”**`). No reader then links it, and the text and formats come through in `@lexical/markdown`, `@lexical/mdast` and cmark-gfm. A reference that already lands in the prefix spoils the link by itself, and a URL that a reference before it would start is spoiled the same way. Emails and `mailto:` addresses need nothing: both readers find them in the decoded text and take in no format.

A URL is found as GFM readers start one: `www.` only at the start of a run or after `*_~(`, a scheme after anything but a letter. Raw output keeps its letters and `*_~(` for that scan, so a URL runs on into it as it does for the readers. This also fixes plain text such as `[hello]](https://lexical.dev)` before a link, whose URL GFM readers ran on into the link.

## 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 `MarkdownExport.ts`:

```
     × can export "[hello]](https://lexical.dev)[world](https://lexical.dev)" 17ms
     × exports formatting beside a `www.` host, which both importers read back 2ms
     × exports formatting beside an email, whose references readers decode first, which both importers read back 1ms
     × exports formatting beside a URL, which both importers read back 1ms
     × exports formatting beside an `xmpp:` address, whose references readers decode first, which both importers read back 1ms
     × exports formatting beside a `www.` host after `_`, which both importers read back 1ms
     × exports formatting beside a URL with a Unicode host, which both importers read back 1ms
     × exports formatting beside a URL that holds an emoji, which both importers read back 1ms
     × exports formatting beside the start of a `www.` host, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of a URL, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of `mailto:`, which its reference spoils, which both importers read back 1ms
     × exports formatting beside the start of an email, which both importers read back 1ms
     × exports formatting beside a URL that a reference before it would start, which both importers read back 1ms
     × exports formatting beside `mailto:`, which a reference before it does not change, which both importers read back 1ms
     × exports formatting beside a URL right after a code span, which both importers read back 1ms
     × exports formatting beside an email before a URL, which both importers read back 1ms
     × exports formatting beside a URL that would take in a code span, which both importers read back 1ms
     × exports formatting beside a URL that runs on through formats, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in markers, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in italic, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in markers after `:`, which both importers read back 1ms
     × exports formatting beside a `www.` host that would take in a code span, which both importers read back 1ms
     × exports formatting beside a URL that would take in strikethrough, which both importers read back 1ms
     × exports formatting beside a URL that holds a `www.` host, which both importers read back 1ms
     × exports formatting beside a URL that would take in the backslash of an escape at its end, which both importers read back 1ms
     × exports formatting beside a URL that holds an email, which both importers read back 1ms
     × spoils a URL for the reference to a backslash before a marker 1ms
     × spoils a URL for the reference to a tag character before its marker 1ms
⎯⎯⎯⎯⎯⎯ Failed Tests 28 ⎯⎯⎯⎯⎯⎯⎯
 Test Files  1 failed (1)
      Tests  28 failed | 747 passed (775)
```

### After

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

The new expectations were also checked by hand against cmark-gfm. On 80,000 random paragraphs of URL fragments and formats, against the previous commit, no export reads back worse in `@lexical/markdown`, `@lexical/mdast` or cmark-gfm, and about 1,700 in 20,000 more read back right in each GFM reader.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ZedingZhang added a commit to ZedingZhang/lexical that referenced this pull request Nov 5, 2026
…ted text round trips

## Description

meta#9430 fixed backslash handling in emphasis flanking and added the matching exporter protection. Rebase this PR onto current `main` and retain the remaining regression coverage for meta#9407 in the existing punctuation test tables.

- Assert the text and format of each imported text node for emphasis before a backslash and after an escaped backslash.
- Cover intraword underscores, escaped emphasis markers, and Markdown exports that preserve emphasis and the hard line break.
- Add document → Markdown → document cases for plain `a` followed by bold `_x`, italic `*x`, strikethrough `~x`, and bold `\x`. Assert the exported numeric character reference and the restored text-node formats.

## Changeset

This PR adds 13 regression cases to the existing unit tests. The strict changeset coverage check reports `coverage: no published code changed`, which qualifies for the test-only exemption in CONTRIBUTING.md.

## Test plan

### Before

Verified that the new tests detect the two regressions with temporary, separately applied mutations. Each mutation was restored before the final suite run.

Excluding the backslash from punctuation classification and running the three new emphasis import cases produced the following output.

```text
Test Files  1 failed (1)
     Tests  3 failed | 805 skipped (808)
```

Bypassing numeric character reference generation in `joinPieces` and running the four new document round-trip cases produced the following output.

```text
Test Files  1 failed (1)
     Tests  4 failed | 804 skipped (808)
AssertionError: expected 'a**\_x**' to be '&meta#97;**\_x**' // Object.is equality
```

Both checks used `pnpm exec vitest run --project unit packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts -t <selected case names> --reporter=verbose`.

### After

```text
pnpm exec vitest run --project unit packages/lexical-markdown packages/lexical-mdast
Test Files  18 passed (18)
     Tests  1461 passed (1461)

pnpm run tsc-test
$ tsc -p ./tsconfig.test.json

pnpm exec eslint packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts

pnpm exec prettier --check packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
Checking formatting...
All matched files use Prettier code style!

pnpm run check-changesets -- --base upstream/main --coverage error
Changeset check passed: 100 pending changeset(s) valid; coverage: no published code changed.
```

Local validation covered the unit suites and static checks on macOS with Node 26.0.0 and pnpm 11.24.0. Browser and E2E coverage is delegated to the PR's CI matrix.
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.

2 participants