Repository navigation
Workaround for Chrome deleteLineBackward bug - #38
Merged
Merged
Conversation
|
lgtm. Really weird lol. |
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a bizarre issue that only seems to affect Chromium browsers + the reported range from
getTargetRanges. Fixes #36.