Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
Thanks for picking this up! The hit-test approach is a good fit for #7758: it follows the direction on the issue (clicks around a rule should become a NodeSelection of the rule, the way the arrow keys do), and the new e2e cases fail on main and pass with the change in both Chromium and Firefox. One behaviour regression needs fixing before this can merge. The other two points are optional.
1. A drag released in the rule's margin loses its selection (packages/lexical-extension/src/HorizontalRuleExtension.ts, the new branch in the CLICK_COMMAND handler)
Browsers fire click on the common ancestor of the mousedown and mouseup targets. A drag that starts in text and ends in the gap above or below a rule therefore reaches this handler as a click on the root, with the mouseup's coordinates. The hit test finds the rule and replaces the range the user just dragged out with a NodeSelection of the rule.
To reproduce in the playground: type Above, insert a rule, type Below, then drag from the start of Above and let go in the gap just above the rule. On main, "Above" stays selected. With this change only the rule is selected, so pressing Backspace deletes the rule instead of "Above". The same happens for a drag from "Below" released above the rule, and for a drag released below it.
One guard fixes it. When the gesture already produced a non-collapsed range, leave the click alone:
const selection = $getSelection();
const ruleAtClick =
event.shiftKey ||
($isRangeSelection(selection) && !selection.isCollapsed())
? null
: $getHorizontalRuleAtClick(editor, event);A plain click still works with this guard, even right after a range was selected, because mousedown collapses the selection before click fires. With it applied, the whole HorizontalRule.spec.mjs still passes in Chromium and Firefox. Please also add an e2e case for it in the new describe block: mouse.down() at the start of "Above", mouse.up() at getGapPoint(page, 'above'), then expect a non-collapsed RangeSelection and no hrSelected class.
2. Optional: assert that the shift-click range spans the rule (HorizontalRule.spec.mjs, "Shift-clicking the gap extends the range selection")
The test checks only that the selection is non-collapsed, so a shift-click that extended within "Above" would pass too. Checking that the focus lands past the rule, or that the selected text includes it, would make the test match its comment.
3. Optional: use getComposedEventTarget(event) ($getHorizontalRuleAtClick)
AGENTS.md's "Shadow DOM and iframe realm safety" table asks library code to read the event target through getComposedEventTarget(event) rather than event.target. Lint does not enforce that row. It works today because Lexical's click listener sits on the root, inside the same shadow tree, and the existing branch a few lines above reads event.target the same way, so switching both would be a small consistency fix.
The #6775 behaviour change (a click between two rules now selects the nearer rule, and Enter then adds a paragraph between them) reads as intentional and consistent with the issue, so I'm not raising it as a problem.
| .poll(() => | ||
| getPageOrFrame(page).evaluate(() => { | ||
| const sel = window.lexicalEditor.getEditorState()._selection; | ||
| return sel !== null && 'anchor' in sel && !sel.isCollapsed(); | ||
| }), | ||
| ) | ||
| .toBe(true); | ||
| }); |
There was a problem hiding this comment.
Optional: this passes for any non-collapsed range, including one that only extends within "Above". Asserting that the focus lands past the rule (or the selected text includes it) would match the comment above. A sibling case that drags from "Above" and releases in the gap would cover the regression in HorizontalRuleExtension.ts.
| editor: LexicalEditor, | ||
| event: MouseEvent, | ||
| ): null | HorizontalRuleNode { | ||
| const parent = isDOMNode(event.target) |
There was a problem hiding this comment.
Optional: AGENTS.md's realm-safety table asks for getComposedEventTarget(event) instead of event.target (lint can't enforce it). It works today because the click listener is on the root inside the same shadow tree, so this is a consistency nit, same as the existing branch at line 233.
…rule when clicking the empty space above or below it A click in the gap above or below a horizontal rule (its margins) lands on the parent element, so the CLICK_COMMAND handler, which only checked for the <hr> itself, ignored it. Where the browser then puts the caret differs between Chromium and Firefox, so the handler now hit tests the click against each child rule's box extended by its margins and selects the nearest one, matching the arrow keys. Shift-click still extends the range, and a click below a rule that is the last block still inserts a paragraph. Selecting the rule from the click handler builds on meta#8967 by @LeSingh1. Signed-off-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The browser fires the click of a drag on the common ancestor, at the release point, so releasing a drag in the gap beside a rule replaced the dragged range with a NodeSelection of the rule. Leave the click alone when the gesture already made a non-collapsed range. Add an e2e case for it, and assert that the shift-click range spans the rule. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
e0fa25f to
3912773
Compare
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Thanks for the review. Fixed in the latest push:
I left — Ved Thakar |
There was a problem hiding this comment.
Thanks, this looks good. Hit-testing the rules' own margin boxes makes a click in the gap behave like the arrow keys, as suggested on #7758, and it no longer depends on where each browser puts the caret. The shift-click and drag guards keep range selections intact. The gap tests fail on main and pass here in Chromium and Firefox, and the drag test fails if the !selection.isCollapsed() guard is removed, so it covers that case.
Suggestions (non-blocking):
- The new shift-click test fails in Firefox (
packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs:869-882). In Firefox the shift-click focus lands atroot:1, just before the rule, so the range never includes thehorizontalruleand the poll times out. This happens onmaintoo, so it comes from Firefox's own caret placement, not from this PR. But the extended e2e workflow runs this spec on Firefox once a PR is approved, so it will need adjusting before it lands. The test's point, that a shift-click isn't replaced by a NodeSelection, holds in both browsers if the test asserts a non-collapsedRangeSelectioninstead ('anchor' in sel && !sel.isCollapsed()). The Before section of the description says Firefox shows the same 3 failures and 3 passes, but onmainthis test fails in Firefox too, so it's 4 and 2 there. - Changeset
Verify:paragraph (.changeset/hr-gap-click-selects-rule.md). CONTRIBUTING.md ("Changesets") and AGENTS.md ask a fix that changes editing behavior to include aVerify:paragraph, so it's listed under Behavior changes. Clicks beside a rule, including between two adjacent rules, now select the rule instead of placing a caret, which is worth flagging to people upgrading. A suggested line is inline. - Zero-size rules (
packages/lexical-extension/src/HorizontalRuleExtension.ts:169-178). A rule that isn't laid out has an all-zero rect, so a click at (0, 0) matches it. A user can't click there, but jsdom reports every rect as zero andelement.click()sends (0, 0). WithHorizontalRuleExtension, callingrootElement.click()in a unit test now selects the first rule. Skipping rules whose rect has zero width and height avoids surprising downstream jsdom tests.
The earlier optional notes still apply: getComposedEventTarget(event) at line 151, per the realm-safety table in AGENTS.md, and tightening the drag test's assertion.
| await expect | ||
| .poll(() => | ||
| getPageOrFrame(page).evaluate(() => | ||
| window.lexicalEditor.getEditorState().read(() => { | ||
| const sel = window.lexicalEditor.getEditorState()._selection; | ||
| return ( | ||
| sel !== null && | ||
| 'anchor' in sel && | ||
| sel.getNodes().some(n => n.getType() === 'horizontalrule') | ||
| ); | ||
| }), | ||
| ), | ||
| ) | ||
| .toBe(true); |
There was a problem hiding this comment.
This assertion fails in Firefox, on main as well as here. Firefox puts the shift-click focus at root:1, just before the rule (anchor Above:5, focus root offset 1), so getNodes() never includes the horizontalrule and the poll times out. Chromium puts the focus at Below:0. The extended e2e workflow runs this spec in Firefox once the PR is approved.
The point of the test, that a shift-click isn't replaced by a NodeSelection, holds in both browsers if it checks for a non-collapsed range instead:
const sel = window.lexicalEditor.getEditorState()._selection;
return sel !== null && 'anchor' in sel && !sel.isCollapsed();(and the comment above would then say the range is kept rather than that it spans the rule).
| '@lexical/extension': patch | ||
| --- | ||
|
|
||
| [fix] A click in the empty space above or below a horizontal rule selects the rule |
There was a problem hiding this comment.
Optional: CONTRIBUTING.md ("Changesets") asks a fix that changes editing behavior to add a Verify: paragraph, so it lists under Behavior changes. Clicks between two adjacent rules no longer leave a caret between them, which is worth flagging:
| [fix] A click in the empty space above or below a horizontal rule selects the rule | |
| [fix] A click in the empty space above or below a horizontal rule selects the rule | |
| Verify: clicks in the space above or below a horizontal rule, including between two adjacent rules, now select the rule instead of placing a caret; check tests that click beside a rule. |
| if (!dom || !view) { | ||
| continue; | ||
| } | ||
| const rect = dom.getBoundingClientRect(); |
There was a problem hiding this comment.
Optional: a rule that isn't laid out has an all-zero rect, so a click at (0, 0) passes the bounds check. A user can't click there, but jsdom reports every rect as zero and element.click() sends (0, 0): with this extension, rootElement.click() in a jsdom test now selects the first rule instead of keeping the caret. Skipping empty rects avoids that:
| const rect = dom.getBoundingClientRect(); | |
| const rect = dom.getBoundingClientRect(); | |
| if (rect.width === 0 && rect.height === 0) { | |
| // Not laid out (display: none, or jsdom): nothing to click beside. | |
| continue; | |
| } |
…tal rule e2e test Firefox extends the range of a shift-click in the gap without the rule among its nodes, so asserting that the range contains the rule failed there. Assert a non-collapsed range again, as before. Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Correction to my note above: I reverted the stricter shift-click assertion (that the range contains the rule). It passed in Chromium but failed in Firefox on CI, where a shift-click in the gap extends the range without the rule among its nodes, so the test asserts a non-collapsed range again, as it did when this was reviewed. The new drag-release test passed in Firefox on that CI run. — Ved Thakar |
Description
This solves issue #7758: clicking the empty space above or below a horizontal rule now selects the rule, which is what the arrow keys already do from those spots.
Current behavior. A horizontal rule is a block decorator with vertical margins. A click in the margin above or below it doesn't land on the
<hr>. It lands on the parent element (the root in the playground), and the caret ends up wherever the browser decides. Onmainin Chromium and Firefox:root:1), drawn as a block cursor, so it looks like it is on the rule (the recording in the issue)@etrepum's expected behaviour on the issue is that clicks should be handled "in the basically same way that the arrow keys are… there are just 'empty' locations between the divider and any adjacent block element node". That covers both sides of the rule.
Root cause. The
CLICK_COMMANDhandler inHorizontalRuleExtensiononly selects the rule whenevent.targetresolves to theHorizontalRuleNode. A click in the gap targets the parent element, so the handler returnsfalse.Why a hit test, not the caret. #8967 selected the rule from the caret the click produced. It was folded into #9053, and its HR part was dropped with this note: the caret offset in that blank space "is precisely what browsers disagree about… so selecting the rule reliably needs a hit test against the rules' own geometry, which is a different change and belongs in its own PR." This is that PR. The handler doesn't look at the caret at all, so the result doesn't depend on where Chromium, Firefox or WebKit put it.
What this changes.
<hr>),$getHorizontalRuleAtClickchecks that element's childHorizontalRuleNodes. It takes each rule'sgetBoundingClientRect(), extends it by the rule's computed margins, and selects the rule whose box contains the click. If two rules' margins overlap, the one nearer to the click wins.ClickAfterLastBlockExtensionis enabled, as it is in the playground. That extension claims the click and stops Lexical's click handling. Without it, the click selects the rule.<hr>and shift-click toggling are unchanged, and so are clicks inside text.Behaviour change for #6775 (intentional). A click between two adjacent rules used to leave a root caret between them. It now selects the nearer rule, which is consistent with the gap next to any other block. Adding content between two rules still works: with the rule selected, Enter inserts a paragraph after it. The #6775 test checks this now, as well as the original point of #6775 (the click gives a selection, not
null).This likely also fixes #6689 (clicking just above the left part of a rule selects
root), which is the same click as the "above" case here. I haven't marked it as closed.The deprecated
@lexical/react/LexicalHorizontalRuleNodeis not changed. The playground andHorizontalRuleExtensionusers get the fix.Credit. Selecting the rule from the click handler, and the e2e test shape, build on #8967 by @LeSingh1.
Closes #7758
Test plan
New e2e tests in
packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs, under "Clicking the empty space beside a horizontal rule (#7758)". Each click point is computed from the middle of the gap itself, not a fixed pixel offset:NodeSelectionof thehorizontalrule, plus thehrSelectedclass)RangeSelectionacross the rule)Updated "Clicking between consecutive block decorators creates selection (#6775)": it now expects the nearer rule to be selected, then presses Enter and checks that a paragraph sits between the two rules (
hr + p + hr).Before
On
mainwith the tests applied and the source change reverted. I ran this locally in Chromium and Firefox:Firefox gives the same 3 failures and 3 passes. The 3 passing cases are behaviour this PR keeps.
After
On current
main:HorizontalRule.spec.mjspasses in full, including the new Bug: HorizontalRule lexical playground #7758 tests and the updated Bug: Can't continue typing in the editor when it has only horizontal rules nodes. #6775 test (all three fail onmain).pnpm run test-unit: all 9,580 tests passtsc,pnpm run flow(0 errors), and eslint/prettier on the changed files: cleanAuthor: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code