Skip to content

Workaround for Chrome deleteLineBackward bug - #38

Merged
feedthejim merged 2 commits into
mainfrom
fix-chrome-delete-line-bug
Jan 29, 2021
Merged

feedthejim merged 2 commits into
mainfrom
fix-chrome-delete-line-bug

Conversation

@trueadm

@trueadm trueadm commented Jan 29, 2021

Copy link
Copy Markdown
Collaborator

This is a bizarre issue that only seems to affect Chromium browsers + the reported range from getTargetRanges. Fixes #36.

@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 Jan 29, 2021
@trueadm
trueadm requested a review from feedthejim January 29, 2021 12:16
@feedthejim

Copy link
Copy Markdown

lgtm. Really weird lol.

@feedthejim
feedthejim merged commit 1f23453 into main Jan 29, 2021
@feedthejim
feedthejim deleted the fix-chrome-delete-line-bug branch January 29, 2021 12:23
etrepum pushed a commit to luantaraschi/lexical that referenced this pull request Aug 25, 2026
…eep a character reference in a URL

## Description

Two things are left over from reading the rest of the shapes CommonMark
gives an inline link.

Whitespace may sit on either side of the destination, and the destination
is optional, so the pattern read `\(\s*(?:...)?\s*\)`. With no destination
between them the two runs neighbour each other, and a run of whitespace can
be split between two adjacent `\s*` in as many ways as it is long. Every one
of them is walked before a run that never reaches the closing parenthesis is
given up on, so importing `[x](` followed by n spaces cost O(n squared):
16,000 spaces took 208ms where main and the commit before this branch took
1ms, and 200,000 spaces took over half a minute. Moving the trailing `\s*`
inside the optional group leaves the two runs with the destination between
them, which reads the same 4,536 spellings of an inline link identically
and finishes in milliseconds.

A URL is written raw, `unescapeText` decodes a numeric character reference,
and a CommonMark reader decodes the named ones as well. A URL holding
`a&meta#10;b` therefore came back as `a\nb`, and one holding `a&b` came back
as `a&b` from any other reader. That is the same decoding a line ending in a
URL relies on to survive, so an `&` that begins a character reference now
goes out as `&meta#38;`, which both of them read back as a single `&`. An `&`
that begins nothing is untouched, so a query string keeps its separators. A
title is read back through `unescapeText` too and had the same defect, so it
gets the same treatment.

## Test plan

Ten cases added to `packages/lexical-markdown`: four whose assertion is that
reading 200,000 characters of whitespace finishes inside the time limit, and
six round trips through export and import for a character reference in a URL
and in a title, alongside two `&` that begin nothing.

### Before

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
     × reads a run that never closes 31754ms
     × reads a run in front of a quote 31837ms
     × reads a run of tabs 35856ms
     × reads a run on either side of a destination 31495ms
     × preserves "https://example.com/a
b" 3ms
     × preserves "https://example.com/a
b" 1ms
     × preserves "https://example.com/a&b" 1ms
     × preserves "&meta#38;" 1ms
     × preserves "a&meta#10;b" 1ms
     × preserves "a&b" 1ms
⎯⎯⎯⎯⎯⎯ Failed Tests 10 ⎯⎯⎯⎯⎯⎯⎯
      Tests  10 failed | 490 passed (500)
```

### After

```
$ npx vitest run packages/lexical-markdown/src/__tests__/unit/LexicalMarkdown.test.ts
 ✓ link destination whitespace does not backtrack > reads a run that never closes 6ms
 ✓ link destination whitespace does not backtrack > reads a run in front of a quote 6ms
 ✓ link destination whitespace does not backtrack > reads a run of tabs 57ms
 ✓ link destination whitespace does not backtrack > reads a run on either side of a destination 11ms
 ✓ link destination round trip > preserves "https://example.com/a
b" 1ms
 ✓ link destination round trip > preserves "https://example.com/a
b" 1ms
 ✓ link destination round trip > preserves "https://example.com/a&b" 0ms
 ✓ link destination round trip > preserves "&meta#38;" 0ms
 ✓ link destination round trip > preserves "https://example.com/?a=1&b=2" 1ms
 ✓ link destination round trip > preserves "https://example.com/?a=1&b=2;c=3" 1ms
 ✓ link title round trip > preserves "a&meta#10;b" 1ms
 ✓ link title round trip > preserves "a&b" 1ms
 ✓ link title round trip > preserves "a&b" 1ms
      Tests  500 passed (500)

$ npx vitest run packages/lexical-markdown packages/lexical-link
 Test Files  11 passed (11)
      Tests  694 passed (694)

$ npx tsc --noEmit -p .        # clean
$ npx eslint packages/lexical-markdown/src   # clean
$ npx prettier --check ...     # All matched files use Prettier code style!
$ npx flow check               # 7 warnings, the same 7 as the unmodified tree
```

Checked against `mdast-util-from-markdown` as a CommonMark reference: the
whitespace change reads 4,536 spellings of an inline link with no behavioral
difference at all, and export followed by import returns the URL unchanged
for 70 URLs, 17 of which hold a character reference. Backtracking stays
linear to 512,000 characters, 36ms at the worst generator.

E2E was not exercised; the change is covered by unit tests.

Co-Authored-By: Claude Opus 5 <[email protected]>
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.

cmd + delete goes too far on chromium browsers

3 participants