← Back to Pull requests

[lexical] Bug Fix: Backspace at the start of the first block no longer throws in a headless editor #9850

Open
unknown opened this pull request with 437 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 08, 2026 09:26 UTC
Author

Description

In an editor with no root element, such as @lexical/headless running in Node, Bun or JavaScriptCore, editor._window is null. Backspace (DELETE_CHARACTER_COMMAND with isBackward = true) at the start of the first block threw Error: window object not found, because $extendSelectionForDeletion (and RangeSelection.modify) called getDOMSelection(getWindow(editor)), and getWindow throws when there is no window. The model-level $collapseAtStart path was never reached, so a quote at the top of the document was not turned into a paragraph.

Both places now read the editor's own window: editor._window === null ? null : getDOMSelection(editor._window), the same check $internalCreateRangeSelection already uses. With no window there is no DOM selection, so the deletion falls through to the model-only handling. Checking editor._window (instead of passing null to getDOMSelection) also keeps a headless editor in a browser page from falling back to the global window selection. Mounted editors are unaffected.

Closes #9849

Test plan

npx vitest --project unit --no-watch --reporter=verbose packages/lexical-headless/src/__tests__/unit/HeadlessDeleteCharacter.test.ts

Before

 ×  unit  packages/lexical-headless/src/__tests__/unit/HeadlessDeleteCharacter.test.ts > DELETE_CHARACTER_COMMAND in a headless editor (no DOM) > backspace at the start of a quote that is the first block 14ms
 ✓  unit  packages/lexical-headless/src/__tests__/unit/HeadlessDeleteCharacter.test.ts > DELETE_CHARACTER_COMMAND in a headless editor (no DOM) > backspace at the start of a quote that follows a paragraph 4ms
AssertionError: expected [Function] to not throw an error but 'Error: window object not found' was thrown
 Test Files  1 failed (1)
      Tests  1 failed | 1 passed (2)

After

 ✓  unit  packages/lexical/src/__tests__/unit/ParagraphCollapseAtStart.test.ts > ParagraphNode.collapseAtStart > keeps a first paragraph whose content follows a blank text node 97ms
 ✓  unit  packages/lexical/src/__tests__/unit/ParagraphCollapseAtStart.test.ts > ParagraphNode.collapseAtStart > keeps a first paragraph whose only other child is a line break 9ms
 ✓  unit  packages/lexical/src/__tests__/unit/ParagraphCollapseAtStart.test.ts > ParagraphNode.collapseAtStart > control: a blank first paragraph is still collapsed away 13ms
 ✓  unit  packages/lexical/src/__tests__/unit/ParagraphCollapseAtStart.test.ts > ParagraphNode.collapseAtStart > control: an empty first paragraph is still collapsed away 12ms
 ✓  unit  packages/lexical/src/__tests__/unit/ParagraphCollapseAtStart.test.ts > ParagraphNode.collapseAtStart > collapses first QuoteNode at start in a headless editor without window (#9849) 4ms
 ✓  unit  packages/lexical-headless/src/__tests__/unit/HeadlessDeleteCharacter.test.ts > DELETE_CHARACTER_COMMAND in a headless editor (no DOM) > backspace at the start of a quote that is the first block 12ms
 ✓  unit  packages/lexical-headless/src/__tests__/unit/HeadlessDeleteCharacter.test.ts > DELETE_CHARACTER_COMMAND in a headless editor (no DOM) > backspace at the start of a quote that follows a paragraph 3ms
 Test Files  2 passed (2)
      Tests  7 passed (7)

The new headless test runs under // @vitest-environment node, so there is no document or window. prettier --list-different, eslint, tsc, tsc -p tsconfig.test.json and scripts/changesets/check.mjs pass on this branch. Browsers were not exercised.

Diff: 437 lines (+327 / -110) across 5 files Reviewer: potatowagon Review duration: 29977m
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 437 lines diff across 5 files
0.45
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.364
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #9850"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 45.2% (MEDIUM) · Higher = larger blast radius
45.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 24.1% · Safe to merge
24.1%
Repository