From bd3cb52e135f9a612519dedb5437ded26ca585d3 Mon Sep 17 00:00:00 2001 From: Gerard Rovira Date: Fri, 30 Oct 2026 16:57:52 -0400 Subject: [PATCH 1/2] [lexical-y] Chore: Vary the inline content of the paragraphs StructuralCommands acts on MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description The structural command cells varied the container a command runs in, but the selected paragraph always held plain text, so a change that splits the paragraph's inline content when it turns it into another block passed every cell. Review of #9590 found one: `OverflowNode` returns false from `isInline()`, though the structure normalization treats it as inline, so with the caret inside an overflow, `$insertList` under `NodeStructureExtension` made a list of the overflow alone. `StructuralCommands.test.ts` gets a family for that dimension. The commands that turn the selected paragraph into another block (`$insertList` of each type, `$wrapNodes` into a quote, and the markdown shortcuts `# `, `- `, `> `) run on a paragraph `abc ` + an inline shape + ` end`, for each of the catalog's 23 inline shapes (text in its modes, tabs, line breaks, hashtags and other text subclasses, links, marks, a link inside a mark, overflow, inline decorators), with the caret before the inline content, the caret inside its own text (shapes that have one), or the whole paragraph selected (the shortcuts are typed at the start), in the root, a container that declares nothing, one that declares `children: 'block'` and a shadow root, unbound with `NodeStructureExtension` and bound with `@lexical/y`: 2,760 cells. Each checks the model rather than another tree: the block the command makes holds the paragraph's whole inline content, its text and the same inline nodes in order, with nothing split out; a shortcut that does not act (in a container that declares nothing, or a declared one without #9590) leaves the paragraph whole. Code highlight nodes live only in code blocks, so they are not among the shapes. The family finds a bug on main in the deprecated `$wrapNodes`: with the caret inside an overflow, or inside a link inside a mark (or the whole paragraph selected), it splits the paragraph in three and drops the overflow or the mark. Those 24 cells are `test.fails` naming #9612. Everything else passes on main. ## Test plan ``` pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts pnpm run ci-check ``` ### Before New cells. Without the `test.fails` marks, on main `be5afea`: ``` × $wrapNodes in a quote, caret inside it, on a paragraph with link/in-mark in root [unbound, NodeStructureExtension] (8 cells each: caret inside a link in a mark, the whole paragraph with one, caret inside an overflow) Tests 24 failed | 2736 passed (2760) ``` On #9590 at `f31df5d`, also the overflow cells of `$insertList` with the caret inside it (18). ### After On main `be5afea`: ``` Test Files 15 passed (15) Tests 6076 passed | 164 expected fail (6240) $ pnpm run ci-check Found 0 errors ``` --- .../unit/matrix/StructuralCommands.test.ts | 235 ++++++++++++++++++ 1 file changed, 235 insertions(+) diff --git a/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts b/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts index 88c555a29267..a30ad5da3d8f 100644 --- a/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts +++ b/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts @@ -92,6 +92,7 @@ import { catalogAppExtension, catalogHeadlessConfig, DEFAULT_CONFIG, + NODE_SHAPES, } from '../../catalog'; import {$createDownstreamComponent} from '../../catalog/downstream'; @@ -699,3 +700,237 @@ describe('structural commands in block containers, where structure is enforced', } } }); + +// The same commands where they turn the selected paragraph into another +// block (a list item, a heading, a quote), with the paragraph's inline +// content varied over the catalog's inline shapes: text in its modes, tabs, +// line breaks, hashtags and other text subclasses, links, marks, overflow, +// and inline decorators. Whatever the container, the block the command +// makes holds all of the paragraph's inline content, in order: its text, +// and the same inline nodes, with nothing split out into another block +// (where a command does not act, the paragraph keeps it). +// (Code highlight nodes live only in code blocks, so they are not here.) +const INLINE_SHAPES = NODE_SHAPES.filter(s => s.$inline !== undefined); +const INLINE_CTXS = CTXS.filter( + c => !c.nested && ['root', 'plain', 'declared', 'shadow'].includes(c.kind), +); +const INLINE_MODES: Mode[] = ['unbound, NodeStructureExtension', 'extension']; + +/** + * Where the selection is when the command runs: a caret in the text before + * the inline content, a caret inside the inline content's own text (for + * shapes that have a text node of their own), or the whole paragraph. + */ +type Where = 'caret before it' | 'caret inside it' | 'the whole paragraph'; +const WHERE: Where[] = [ + 'caret before it', + 'caret inside it', + 'the whole paragraph', +]; + +/** The inline content's own text node, between "abc " and " end". */ +function $innerText(): TextNode | undefined { + return $blockOfAbc()! + .getAllTextNodes() + .find( + n => + !n.getTextContent().includes('abc') && + !n.getTextContent().includes(' end') && + n.getTextContentSize() > 0, + ); +} + +/** Selects as `where` says; false where the shape has no such place. */ +function $select(where: Where): boolean { + if (where === 'caret before it') { + $caret('abc', 1); + return true; + } + if (where === 'caret inside it') { + const inner = $innerText(); + if (inner === undefined) { + return false; + } + const at = Math.min(1, inner.getTextContentSize()); + inner.select(at, at); + return true; + } + const last = $text(' end')!; + const selection = $text('abc')!.select(0, 0); + selection.focus.set(last.getKey(), last.getTextContentSize(), 'text'); + return true; +} + +type BlockCmd = { + name: string; + /** Whether `node` is the block the command makes. */ + made: (node: LexicalNode) => boolean; + run: () => void; + /** The shortcuts are typed at the paragraph's start; they take no range. */ + type?: string; +}; + +const BLOCK_CMDS: BlockCmd[] = [ + ...(['bullet', 'number', 'check'] as const).map( + (type): BlockCmd => ({ + made: n => + $isListItemNode(n) && + $isListNode(n.getParent()) && + ( + n.getParent() as ElementNode & {getListType(): string} + ).getListType() === type, + name: `$insertList('${type}')`, + run: () => $insertList(type), + }), + ), + { + made: n => $isQuoteNode(n), + name: '$wrapNodes in a quote', + run: () => { + const selection = $getSelection(); + if ($isRangeSelection(selection)) { + $wrapNodes(selection, () => $createQuoteNode()); + } + }, + }, + ...( + [ + ['# ', (n: LexicalNode) => $isHeadingNode(n)], + ['- ', (n: LexicalNode) => $isListItemNode(n)], + ['> ', (n: LexicalNode) => $isQuoteNode(n)], + ] as const + ).map( + ([typed, made]): BlockCmd => ({ + made, + name: `markdown "${typed}"`, + run: () => $caret('abc', 0), + type: typed, + }), + ), +]; + +/** The inline content of a block: its text and its inline node types. */ +function $inlineContent(block: ElementNode, typed = ''): string { + const types: string[] = []; + const walk = (n: LexicalNode) => { + if (n !== block && n.getType() !== 'text') { + types.push(n.getType()); + } + if ($isElementNode(n)) { + n.getChildren().forEach(walk); + } + }; + walk(block); + const text = block.getTextContent(); + return JSON.stringify({ + text: + typed !== '' && text.startsWith(typed) ? text.slice(typed.length) : text, + types, + }); +} + +/** The block element holding the text "abc". */ +function $blockOfAbc(): ElementNode | null { + let node: LexicalNode | null = $text('abc') ?? null; + while (node !== null && !($isElementNode(node) && !node.isInline())) { + node = node.getParent(); + } + return node; +} + +describe("structural commands keep the selected paragraph's inline content together", () => { + for (const shape of INLINE_SHAPES) { + for (const cmd of BLOCK_CMDS) { + for (const where of cmd.type === undefined ? WHERE : WHERE.slice(0, 1)) { + for (const ctx of INLINE_CTXS) { + for (const mode of INLINE_MODES) { + // The deprecated $wrapNodes takes an OverflowNode (not inline by + // isInline(), though it is) and a link inside a mark for blocks of + // their own: it splits the paragraph and drops the overflow or + // the mark (#9612). + const failsUntilFixed = + cmd.name === '$wrapNodes in a quote' && + ((shape.name === 'overflow/text' && + where === 'caret inside it') || + (shape.name === 'link/in-mark' && where !== 'caret before it')); + (failsUntilFixed ? test.fails : test)( + `${cmd.name}, ${where}, on a paragraph with ${shape.name} in ${ctxName(ctx)} [${mode}]`, + async () => { + const $inner = () => [ + $createParagraphNode().append( + $createTextNode('abc '), + ...shape.$inline!(), + $createTextNode(' end'), + ), + ]; + const opened = open(mode, ctx, $inner); + const {editor} = opened; + try { + await Promise.resolve(); + const before = editor.read(() => + $inlineContent($blockOfAbc()!), + ); + let placed = true; + editor.update( + () => { + placed = cmd.type !== undefined || $select(where); + if (placed) { + cmd.run(); + } + }, + {discrete: true}, + ); + if (!placed) { + // The shape has no text of its own to put the caret in. + return; + } + for (const ch of cmd.type ?? '') { + editor.update( + () => { + const selection = $getSelection(); + if ($isRangeSelection(selection)) { + selection.insertText(ch); + } + }, + {discrete: true}, + ); + } + await Promise.resolve(); + expect(opened.errors).toEqual([]); + const result = editor.read(() => { + const block = $blockOfAbc(); + if (block === null) { + return 'no block holds "abc"'; + } + // Where a shortcut does not act (in a container that declares + // nothing, and in a declared one without the rule), the + // paragraph stays as it was. + // A command given a selection always makes its block. + const made = + cmd.made(block) || + cmd.made(block.getParent() as LexicalNode) || + (cmd.type !== undefined && $isParagraphNode(block)) + ? block + : null; + if (made === null) { + return `"abc" is in a ${block.getType()}, not the made block`; + } + // A shortcut that did not act leaves its typed text. + return cmd.type !== undefined && + !cmd.made(made) && + !cmd.made(made.getParent() as LexicalNode) + ? $inlineContent(made, cmd.type) + : $inlineContent(made); + }); + expect(result).toBe(before); + } finally { + opened.dispose(); + } + }, + ); + } + } + } + } + } +}); From 9ce3bd9b7fde181c3c70ca1332b4082ac280886e Mon Sep 17 00:00:00 2001 From: Gerard Rovira Date: Fri, 30 Oct 2026 17:11:03 -0400 Subject: [PATCH 2/2] [lexical-y] Chore: Put the caret inside the inline content by offset, and skip only the cells that cannot apply ## Description Review found that the "caret inside it" cells of the inline content family returned early, without running the command, for 64 cells that should have run: the caret was placed in a text node that held neither sentinel, but normalization merges plain text (`text/normal`) and the text around a line break into the sentinels' nodes, so no such node was found. The caret is now placed by offset, one character into the inline content, in the leaves as they are after normalization, and a cell where it cannot be placed fails instead of passing. The cells that cannot apply, the caret inside an inline decorator (which has no text node; 128 cells), are `test.skip` with the reason in their names. The whole-paragraph selection now runs from the paragraph's first text node to its last. ## Test plan ``` pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts pnpm run ci-check ``` ### Before ``` Tests 2736 passed | 24 expected fail (2760) (192 "caret inside" cells returned early) ``` ### After On main `be5afea`, the 64 cells that used to return early run and pass: ``` Tests 2608 passed | 24 expected fail | 128 skipped (2760) ``` On #9590 at `f31df5d`, the 18 overflow cells of `$insertList` still fail; at `0caffb6`, they pass. ``` $ pnpm run ci-check Found 0 errors ``` --- .../unit/matrix/StructuralCommands.test.ts | 63 +++++++++++-------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts b/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts index a30ad5da3d8f..8c6cbea1444b 100644 --- a/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts +++ b/packages/lexical-y/src/__tests__/unit/matrix/StructuralCommands.test.ts @@ -728,16 +728,32 @@ const WHERE: Where[] = [ 'the whole paragraph', ]; -/** The inline content's own text node, between "abc " and " end". */ -function $innerText(): TextNode | undefined { - return $blockOfAbc()! - .getAllTextNodes() - .find( - n => - !n.getTextContent().includes('abc') && - !n.getTextContent().includes(' end') && - n.getTextContentSize() > 0, - ); +/** + * Puts the caret one character into the inline content: at offset 5 of the + * paragraph's text, "abc " being 4 characters. Located by offset in the + * leaves as they are after normalization, since the shape's text can merge + * with the text around it. False where that character is not in a text + * node (an inline decorator's text). + */ +function $caretInside(): boolean { + let offset = 'abc '.length + 1; + const walk = (n: LexicalNode): boolean => { + if ($isElementNode(n)) { + return n.getChildren().some(walk); + } + const size = n.getTextContentSize(); + if (offset < size || (offset === size && $isTextNode(n))) { + if (!$isTextNode(n)) { + return false; + } + n.select(offset, offset); + offset = -1; + return true; + } + offset -= size; + return false; + }; + return walk($blockOfAbc()!) && offset === -1; } /** Selects as `where` says; false where the shape has no such place. */ @@ -747,16 +763,13 @@ function $select(where: Where): boolean { return true; } if (where === 'caret inside it') { - const inner = $innerText(); - if (inner === undefined) { - return false; - } - const at = Math.min(1, inner.getTextContentSize()); - inner.select(at, at); - return true; + return $caretInside(); } - const last = $text(' end')!; - const selection = $text('abc')!.select(0, 0); + const block = $blockOfAbc()!; + const texts = block.getAllTextNodes(); + const first = texts[0]; + const last = texts[texts.length - 1]; + const selection = first.select(0, 0); selection.focus.set(last.getKey(), last.getTextContentSize(), 'text'); return true; } @@ -853,8 +866,11 @@ describe("structural commands keep the selected paragraph's inline content toget ((shape.name === 'overflow/text' && where === 'caret inside it') || (shape.name === 'link/in-mark' && where !== 'caret before it')); - (failsUntilFixed ? test.fails : test)( - `${cmd.name}, ${where}, on a paragraph with ${shape.name} in ${ctxName(ctx)} [${mode}]`, + // An inline decorator has no text node to put the caret in. + const skip = + where === 'caret inside it' && shape.kind === 'inline-decorator'; + (skip ? test.skip : failsUntilFixed ? test.fails : test)( + `${cmd.name}, ${where}, on a paragraph with ${shape.name} in ${ctxName(ctx)} [${mode}]${skip ? ' (skipped: no text node inside the decorator)' : ''}`, async () => { const $inner = () => [ $createParagraphNode().append( @@ -880,10 +896,7 @@ describe("structural commands keep the selected paragraph's inline content toget }, {discrete: true}, ); - if (!placed) { - // The shape has no text of its own to put the caret in. - return; - } + expect(placed).toBe(true); for (const ch of cmd.type ?? '') { editor.update( () => {