Skip to content

[lexical-link] Bug Fix: open middle-clicked links in a new tab - #9078

Merged
etrepum merged 17 commits into
meta:mainfrom
nithin42:fix-clickable-link-middle-click
Aug 30, 2026
Merged

etrepum merged 17 commits into
meta:mainfrom
nithin42:fix-clickable-link-middle-click

Conversation

@nithin42

Copy link
Copy Markdown
Contributor

Description

registerClickableLink decides whether to open a link in a new tab like this:

const isMiddle = event.type === 'auxclick' && event.button === 1;
window.open(url, stores.newTab.peek() || isMiddle || event.metaKey || ... ? '_blank' : '_self');

but the only listeners it registers are click and mouseup:

const onMouseUp = (event: MouseEvent) => {
  if (event.button === 1) {
    onClick(event);
  }
};

return editor.registerRootListener((rootElement) => {
  if (rootElement) {
    return registerEventListeners(rootElement, {click: onClick, mouseup: onMouseUp}, eventOptions);
  }
});

Browsers do not fire click for the middle button, which is exactly why the mouseup listener exists. But the event that reaches onClick through that path has type === 'mouseup', so the event.type === 'auxclick' half of the condition is never satisfied and isMiddle is always false. Middle-clicking a link opens it in the same tab, replacing the page the editor is on, instead of opening a new tab.

No auxclick listener is registered anywhere in the file, so the auxclick check has nothing to match.

Fix

Key off the button alone. onClick is only reached from a real click or from onMouseUp, and onMouseUp already filters to button === 1, so button 1 unambiguously means a middle click here. This also keeps working if a host ever routes an auxclick into the handler.

const isMiddle = event.button === 1;

Test plan

Added packages/lexical-link/src/__tests__/unit/ClickableLinkMiddleClick.test.ts, which mounts an editor containing a link, stubs window.open, and dispatches real mouse events at the rendered anchor:

  • a mouseup with button: 1 must call window.open(url, '_blank') (fails on main, passes with this change)
  • a plain left click must still call window.open(url, '_self') (guards against over-correcting)
npx vitest --project unit --no-watch packages/lexical-link

On main the first test fails with _self where _blank was expected. With this change both pass and the full lexical-link unit suite is green (8 files, 171 tests). prettier and eslint are clean.

registerClickableLink computed isMiddle as `event.type === 'auxclick' && event.button === 1`, but it only registers click and mouseup listeners. Middle clicks reach the handler as mouseup, so isMiddle was always false and the link opened in the same tab.
@meta-cla meta-cla 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 Aug 19, 2026
@vercel

vercel Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 30, 2026 4:51am
lexical-playground Ready Ready Preview Aug 30, 2026 4:51am

Request Review

@etrepum

etrepum commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

This looks like it's failing tests, just restarted them in case it was intermittent but you'll need to fix them if it fails again

Comment on lines -125 to +119
{click: onClick, mouseup: onMouseUp},
{auxclick: onClick, click: onClick},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we really have to worry about supporting default middle click behavior on old versions of iOS with mice plugged in

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to claude the behavior should degrade to the same result:


OS < 18.2: how middle click degrades

You're right that auxclick is Safari/iOS 18.2+, against a documented floor of Safari 15+. It degrades to the browser's own behavior, which produces the same result. With no auxclick listener registered, nothing in the extension handles the middle button, so nothing calls preventDefault() — and the browser opens the anchor's href in a new tab, exactly as if the link weren't in an editor at all.

That fallback is faithful because the anchor already carries the right URL:

LinkNode.updateLinkDOM sets anchor.href = this.sanitizeUrl(this.__url) — the same sanitized string window.open would get.
An unlinked AutoLinkNode renders a , not an anchor, so there's no href for the browser to follow — matching the extension's isUnlinkedAutolink guard.

What a Safari 15–18.1 user loses is only the "don't follow the link while text is selected" guard for the middle button specifically. That guard was already unreliable there: it worked by calling preventDefault() on mouseup, which I verified does not cancel native middle-click navigation.

Compared to what main does on those same versions, this is strictly better — the old mouseup path called window.open(url, '_self'), navigating the editor away in the current tab, and the browser still opened its own tab.

claude and others added 2 commits August 29, 2026 19:55
… one tab, and the link editor stays closed in read-only mode

## Description

`registerClickableLink` listened for `click` and `mouseup`, and asked for
`event.type === 'auxclick'` to decide that a click was a middle click. No
`auxclick` listener was ever registered, so that test could never be true and a
middle click took two wrong turns at once: it opened the URL through
`window.open(url, '_self')` instead of a new tab, and, because
`event.preventDefault()` on `mouseup` does not stop the browser from opening a
middle-clicked link itself, the URL was opened a second time natively.

Registering `auxclick` fixes both -- that event *is* cancelable for the middle
button -- but `auxclick` fires for *every* non-primary button, so following the
link from it unconditionally makes a right click (button 2 in Chromium and
Firefox) or a mouse history button open the link and swallow the context menu.
This registers `auxclick` alongside `click` and returns early for any auxiliary
button other than the middle one.

The playground's floating link editor also popped up in read-only mode. Its own
`$updateLinkEditor` bails out while the editor is not editable, so what appeared
was an empty URL next to Edit and Delete buttons for a document that cannot be
edited. `$updateToolbar` now leaves it closed while the editor is read-only, and
an editable listener closes an open one as soon as the editor is locked.

## Test plan

Both fixes are covered by a new playground e2e suite,
`__tests__/e2e/ClickableLinks.spec.mjs`, which drives real mouse buttons and
counts the tabs the browser actually opens, plus unit coverage of the button
guard in `packages/lexical-link/src/__tests__/unit/ClickableLinkExtension.test.ts`.

### Before

`pnpm run test-e2e-chromium ClickableLinks` against the unmodified sources:

```
Running 7 tests using 2 workers

  x  2 › read-only mode › a middle click opens the link in exactly one tab (17.5s)
  ok 1 › read-only mode › a left click opens the link in exactly one tab (17.7s)
  ok 4 › read-only mode › a right click does not open the link (17.2s)
  x  3 › read-only mode › the link editor does not open (21.1s)
  ok 6 › editable mode › the link editor opens for the link under the caret (15.6s)
  x  5 › read-only mode › locking closes an open link editor (20.6s)
  ok 7 › editable mode › clicking a link does not follow it (16.5s)
```

The middle-click failure is the double open:

```
    Error: expect(received).toEqual(expected) // deep equality

    - Expected  - 0
    + Received  + 1

      Array [
        "https://example.com/target",
    +   "https://example.com/target",
      ]

      159 |       // middle-clicked link itself, so handling the middle button there
      160 |       // opened the URL twice: once natively and once through `window.open`.
    > 161 |       expect(await tabs.settle()).toEqual([LINK_URL]);
```

and the link editor is there in read-only mode:

```
    Error: expect(locator).toHaveCount(expected) failed

    Locator:  locator('.link-editor .link-view')
    Expected: 0
    Received: 1
```

Registering `auxclick` without a button guard trades the middle-click failure
for a right-click one (`--grep "read-only"`):

```
  3 failed
    [chromium] › ... › read-only mode › a right click does not open the link
    [chromium] › ... › read-only mode › the link editor does not open
    [chromium] › ... › read-only mode › locking closes an open link editor
  2 passed (57.5s)
```

### After

```
$ pnpm run test-e2e-chromium ClickableLinks
Running 7 tests using 2 workers

  ok 1 › read-only mode › a left click opens the link in exactly one tab (15.7s)
  ok 2 › read-only mode › a middle click opens the link in exactly one tab (15.8s)
  ok 4 › read-only mode › the link editor does not open (14.6s)
  ok 3 › read-only mode › a right click does not open the link (15.6s)
  ok 5 › read-only mode › locking closes an open link editor (14.3s)
  ok 6 › editable mode › the link editor opens for the link under the caret (14.1s)
  ok 7 › editable mode › clicking a link does not follow it (15.3s)

  7 passed (1.0m)

$ pnpm run test-e2e-webkit ClickableLinks
  7 passed (21.0s)

$ npx vitest --project unit --no-watch packages/lexical-link
 Test Files  10 passed (10)
      Tests  187 passed (187)
```

No regressions in the existing link suites:

```
$ pnpm run test-e2e-chromium Links.spec.mjs AutoLinks.spec.mjs
  1 skipped
  111 passed (13.1m)

$ pnpm run test-e2e-webkit Links.spec.mjs AutoLinks.spec.mjs
  1 skipped
  111 passed (3.4m)
```

Firefox could not be exercised: in this environment Playwright's Firefox needs
over a minute to load the playground, so `page.goto` times out for every spec,
including untouched ones such as `Lock.spec.mjs`. The browser behaviour the fix
turns on was checked in Firefox directly instead -- a middle click emits
`auxclick` with `button: 1` and a right click emits `auxclick` with `button: 2`,
the same as Chromium (WebKit emits no `auxclick` for the right button at all).

Co-Authored-By: Claude <[email protected]>
…ts-u8fv48' into fix-clickable-link-middle-click
claude and others added 3 commits August 29, 2026 20:12
…ithout auxclick

## Description

`registerClickableLink` relies on `auxclick` for the middle button, which
Safari only gained in 18.2 while Lexical supports Safari 15+. Document what
happens on the versions in between: nothing is registered for the middle
button, so the browser opens the anchor's own href -- the same URL LinkNode
already sanitized into it, and no href at all on an unlinked AutoLinkNode --
in a new tab. The user-visible result is identical.

## Test plan

Verified by temporarily registering only `{click: onClick}` (no `auxclick`
listener, standing in for Safari below 18.2) and re-running the read-only e2e
suite: a middle click still opens exactly one new tab, and a right click still
opens none.

### Before

Undocumented; nothing in the source said what a Safari 15-18.1 user gets.

### After

```
$ pnpm run test-e2e-chromium ClickableLinks --grep "read-only"   # click-only build
Running 5 tests using 2 workers
  ok 1 > read-only mode > a left click opens the link in exactly one tab (19.9s)
  ok 2 > read-only mode > a middle click opens the link in exactly one tab (20.4s)
  ok 3 > read-only mode > a right click does not open the link (17.0s)
  ok 4 > read-only mode > the link editor does not open (17.1s)
  ok 5 > read-only mode > locking closes an open link editor (15.8s)
  5 passed (1.1m)
```

WebKit behaves the same way -- the tab opens -- though Playwright cannot read
the URL of a tab the browser opened itself there, so the middle-click
assertion reports `""` rather than the target URL in that simulated build.
With `auxclick` registered as shipped, the suite is green:

```
$ pnpm run test-e2e-webkit ClickableLinks
  7 passed (26.1s)

$ npx vitest --project unit --no-watch packages/lexical-link/src/__tests__/unit/ClickableLinkExtension.test.ts
 Test Files  1 passed (1)
      Tests  6 passed (6)
```

Co-Authored-By: Claude <[email protected]>
…ts-u8fv48' into fix-clickable-link-middle-click
claude and others added 2 commits August 29, 2026 20:57
…e playground's own origin

## Description

`ClickableLinks.spec.mjs` is the only spec in the suite that opens real browser
tabs, and it pointed the link at `https://example.com/target`. A cross-origin
popup makes WebKit swap it into a brand new WebContent process, and on macOS CI
that took the whole browser down mid-test:

```
[pid=2495][err] com.apple.WebKit.WebContent.Development[5438:22087] CoreText note: ...
[pid=2495][err] pw_run.sh: line 7: 2517 Segmentation fault: 11
Error: mouse.click: Target page, context or browser has been closed
```

The target is now `http://localhost:${E2E_PORT}/link-target`, same origin as
the playground, still served by the test's own route rather than the dev server
or the network. Opened tabs are also closed as they are collected, so a tab
does not hold a renderer process for the rest of the worker's run, and the
right-click test dismisses the native context menu it may have raised.

Same-origin has a second benefit: WebKit could not report the URL of a
cross-origin tab the browser opened itself (a native middle click), so that
assertion read `""` there; same-origin it reads the real URL.

## Test plan

### Before

macOS CI, WebKit:

```
  1) [webkit] > read-only mode > a left click opens the link in exactly one tab

    Error: mouse.click: Target page, context or browser has been closed
```

### After

Not reproducible on Linux WebKit either before or after, so this is verified as
"still catches both regressions, and stable under repetition" rather than as a
fix for the crash itself.

```
$ pnpm run test-e2e-webkit ClickableLinks --repeat-each=5
  35 passed (1.5m)

$ pnpm run test-e2e-chromium ClickableLinks
  7 passed (1.1m)
```

Both regressions are still caught. Against the `mouseup` registration, WebKit
now reports the doubled open with real URLs instead of empty strings:

```
    Array [
      "http://localhost:3000/link-target",
  +   "http://localhost:3000/link-target",
    ]
```

and against an unguarded `auxclick` registration, Chromium still fails the
right-click test:

```
  - Array []
  + Array [
  +   "http://localhost:3000/link-target",
  + ]
```

Co-Authored-By: Claude <[email protected]>
…ts-u8fv48' into fix-clickable-link-middle-click
## Description

Playwright was pinned at `^1.60.0`, whose browser set includes `webkit-2287`.
That WebKit build has been segfaulting its content process on the macOS e2e
jobs, taking the page down mid-test:

```
[pid=15242][err] pw_run.sh: line 7: 15258 Segmentation fault: 11
Error: mouse.click: Target page, context or browser has been closed
```

Nothing in the test code causes that — it is the browser process dying — so
this moves to the current stable release and the browser builds that come with
it:

| browser | 1.60.0 | 1.62.1 |
| --- | --- | --- |
| webkit | 2287 | 2336 |
| firefox | 1522 | 1538 |
| chromium | 1223 | 1234 |

All eight pins move together — the root, `dev-examples/shadow-dom`,
`dev-examples/shadow-dom-web-component`, `examples/nextjs-code-shiki`,
`examples/extension-sveltekit-ssr-hydration` and the astro integration
fixture — so the monorepo resolves to one Playwright and downloads one browser
set. Nothing else needs to change for CI: `setup-playwright` derives its cache
key from `pnpm-lock.yaml` and installs with `pnpm exec playwright install`, so
both the key and the downloaded revisions follow the lockfile.

Note that the 1.61 and 1.62 release notes do not claim a fix for this crash;
the newer WebKit build is the remedy available, not a documented one.

## Test plan

### Before

```
$ pnpm exec playwright --version
Version 1.60.0
$ node -e "const b=require('playwright-core/browsers.json');console.log(b.browsers.filter(x=>x.name==='webkit')[0].revision)"
2287
```

### After

```
$ pnpm exec playwright --version
Version 1.62.1
$ node -e "const b=require('playwright-core/browsers.json');console.log(b.browsers.filter(x=>x.name==='webkit')[0].revision)"
2336

$ pnpm run ci-check
CI-CHECK EXIT=0

$ pnpm run test-unit
 Test Files  294 passed (294)
      Tests  5547 passed | 1 skipped (5548)

$ VITEST_BROWSER=chromium npx vitest --project browser --no-watch
 Test Files  22 passed (22)
      Tests  143 passed | 1 skipped (144)

$ VITEST_BROWSER=firefox npx vitest --project browser --no-watch
 Test Files  21 passed | 1 skipped (22)
      Tests  140 passed | 4 skipped (144)
```

The Chromium e2e sweep is still running at the time of this commit; the run on
1.60.0 before this change was 826 passed, 31 skipped.

WebKit could not be exercised: its Playwright build does not launch in this
container for want of system libraries, and the crash this addresses is on
macOS in any case, so only CI can confirm it is gone.
claude and others added 3 commits August 30, 2026 03:09
## Description

Playwright was pinned at `^1.60.0`, whose browser set includes `webkit-2287`.
That WebKit build has been segfaulting its content process on the macOS e2e
jobs, taking the page down mid-test:

```
[pid=15242][err] pw_run.sh: line 7: 15258 Segmentation fault: 11
Error: mouse.click: Target page, context or browser has been closed
```

Nothing in the test code causes that — it is the browser process dying — so
this moves to the current stable release and the browser builds that come with
it:

| browser | 1.60.0 | 1.62.1 |
| --- | --- | --- |
| webkit | 2287 | 2336 |
| firefox | 1522 | 1538 |
| chromium | 1223 | 1234 |

All eight pins move together — the root, `dev-examples/shadow-dom`,
`dev-examples/shadow-dom-web-component`, `examples/nextjs-code-shiki`,
`examples/extension-sveltekit-ssr-hydration` and the astro integration
fixture — so the monorepo resolves to one Playwright and downloads one browser
set. Nothing else needs to change for CI: `setup-playwright` derives its cache
key from `pnpm-lock.yaml` and installs with `pnpm exec playwright install`, so
both the key and the downloaded revisions follow the lockfile.

Note that the 1.61 and 1.62 release notes do not claim a fix for this crash;
the newer WebKit build is the remedy available, not a documented one.

## Test plan

### Before

```
$ pnpm exec playwright --version
Version 1.60.0
$ node -e "const b=require('playwright-core/browsers.json');console.log(b.browsers.filter(x=>x.name==='webkit')[0].revision)"
2287
```

### After

```
$ pnpm exec playwright --version
Version 1.62.1
$ node -e "const b=require('playwright-core/browsers.json');console.log(b.browsers.filter(x=>x.name==='webkit')[0].revision)"
2336

$ pnpm run ci-check
CI-CHECK EXIT=0

$ pnpm run test-unit
 Test Files  294 passed (294)
      Tests  5547 passed | 1 skipped (5548)

$ VITEST_BROWSER=chromium npx vitest --project browser --no-watch
 Test Files  22 passed (22)
      Tests  143 passed | 1 skipped (144)

$ VITEST_BROWSER=firefox npx vitest --project browser --no-watch
 Test Files  21 passed | 1 skipped (22)
      Tests  140 passed | 4 skipped (144)
```

The Chromium e2e sweep is still running at the time of this commit; the run on
1.60.0 before this change was 826 passed, 31 skipped.

WebKit could not be exercised: its Playwright build does not launch in this
container for want of system libraries, and the crash this addresses is on
macOS in any case, so only CI can confirm it is gone.
…r Firefox 152

## Description

Playwright 1.62.1 ships Firefox 152 (`firefox-1538`, up from `firefox-1522`),
and two table-selection specs fail on it. Both were confirmed against
unmodified sources, and both pass on Playwright 1.60.0 and fail on 1.62.1, so
the browser is the only variable. They have different causes and get different
treatment.

`Selection.spec.mjs` "Selecting table cell then dragging to outside of table"
is a synthetic-input artifact. Dumping the live DOM selection showed it
collapsed inside the cell's own text -- the drag never started, because Firefox
152 does not begin a drag-selection from a single `mousemove` that teleports
from the cell to (500, 500). Moving in `steps` starts it, and matches what a
real mouse does; a one-jump drag was never realistic input.

`Tables.spec.mjs` "Range-select from below nested table into it" is not a test
bug. The shift-click now selects from the start of "before" to the end of
"after" -- the entire outer cell -- which is verbatim the symptom already
carrying `test.fixme(browserName === 'firefox', 'Erroneously selects the
entire outer cell')` on a sibling test in the same block. It is not input
dispatch either: hover-then-shift-click fails identically. Firefox 152 extended
an existing nested-table shift-selection gap in `@lexical/table` to a third
case in that block (two of the four were already fixme-d for Firefox), so this
is marked to match its siblings rather than papered over. The underlying gap is
a real Firefox-only defect and wants its own change, where a fix to nested
table selection can be judged across browsers on its own merits.

## Test plan

### Before

Playwright 1.62.1, Firefox 152, against unmodified product sources:

```
  2 failed
    [firefox] > Selection > Selecting table cell then dragging to outside of table should select entire table
    [firefox] > Tables > nested table shift-selection tests > Range-select from below nested table into it selects the entire table, but not the outer table
```

The drag test's selection was collapsed where typing had left the caret, so no
drag happened at all:

```
DEBUG-SEL {
 "anchor": "... th.PlaygroundEditorTheme__tableCellHeader > p > span > #text(\"Foo\")",
 "anchorOffset": 3,
 "focus":  "... th.PlaygroundEditorTheme__tableCellHeader > p > span > #text(\"Foo\")",
 "focusOffset": 3,
 "isCollapsed": true
}
```

and the nested-table shift-click spanned the whole outer cell:

```
DEBUG-SEL {
 "anchor": "... td.PlaygroundEditorTheme__tableCell > p > span > #text(\"before\")",
 "anchorOffset": 0,
 "focus":  "... td.PlaygroundEditorTheme__tableCell > p > span > #text(\"after\")",
 "focusOffset": 5,
 "isCollapsed": false,
 "selectedCells": 4
}
```

The same two tests on Playwright 1.60.0 / Firefox 1522, same sources:

```
  2 passed (8.8s)
```

### After

```
$ pnpm run test-e2e-firefox -g "Selecting table cell then dragging to outside of table"
  1 passed (6.2s)
```

A full-suite Firefox run is in progress to catch anything else the browser bump
moved; this message covers the two failures reported so far.

Co-Authored-By: Claude <[email protected]>
…ts-u8fv48' into fix-clickable-link-middle-click
@etrepum
etrepum added this pull request to the merge queue Aug 30, 2026
Merged via the queue into meta:main with commit d2f1fb6 Aug 30, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 914b310f Deployed Aug 30, 2026 by vercel[bot]
Preview – lexical-playground — 914b310f Deployed Aug 30, 2026 by vercel[bot]
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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants