diff --git a/.changeset/little-plums-behave.md b/.changeset/little-plums-behave.md new file mode 100644 index 0000000000..0583c15d76 --- /dev/null +++ b/.changeset/little-plums-behave.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Fix positions iteration when starting inside an inline void node diff --git a/packages/slate/src/interfaces/editor.ts b/packages/slate/src/interfaces/editor.ts index 055a845be7..dd0cb7f87c 100644 --- a/packages/slate/src/interfaces/editor.ts +++ b/packages/slate/src/interfaces/editor.ts @@ -1341,6 +1341,9 @@ export const Editor: EditorInterface = { // then we will iterate over their content. if (!voids && editor.isVoid(node)) { yield Editor.start(editor, path) + // It's possible the start of the range we're iterating over is in a void, in which case + // we want to make sure we don't incorrectly yield the start of a subsequent text node for unit !== 'offset' + isNewBlock = false continue } diff --git a/packages/slate/test/interfaces/Editor/positions/range/start-in-void.tsx b/packages/slate/test/interfaces/Editor/positions/range/start-in-void.tsx new file mode 100644 index 0000000000..5fdf97b66b --- /dev/null +++ b/packages/slate/test/interfaces/Editor/positions/range/start-in-void.tsx @@ -0,0 +1,31 @@ +/** @jsx jsx */ +import { Editor } from 'slate' +import { jsx } from '../../../..' + +export const input = ( + + + one + + + + two + + +) +export const test = editor => { + return Array.from( + Editor.positions(editor, { + at: { + anchor: { path: [0, 1, 0], offset: 0 }, + focus: { path: [0, 2], offset: 2 }, + }, + unit: 'character', + }) + ) +} +export const output = [ + { path: [0, 1, 0], offset: 0 }, + { path: [0, 2], offset: 1 }, + { path: [0, 2], offset: 2 }, +]