diff --git a/packages/slate/src/commands/at-range.js b/packages/slate/src/commands/at-range.js
index fcdcd7326..574e2c8da 100644
--- a/packages/slate/src/commands/at-range.js
+++ b/packages/slate/src/commands/at-range.js
@@ -794,7 +794,8 @@ Commands.insertTextAtRange = (change, range, text, marks) => {
const { document } = value
const { start } = range
let key = start.key
- let offset = start.offset
+ const offset = start.offset
+ const path = start.path
const parent = document.getParent(start.key)
if (change.isVoid(parent)) {
@@ -805,10 +806,11 @@ Commands.insertTextAtRange = (change, range, text, marks) => {
if (range.isExpanded) {
change.deleteAtRange(range)
+ const startText = change.value.document.getNode(path)
+
// Update range start after delete
- if (change.value.selection.start.key !== key) {
- key = change.value.selection.start.key
- offset = change.value.selection.start.offset
+ if (startText && startText.key !== key) {
+ key = startText.key
}
}
diff --git a/packages/slate/test/changes/at-range/insert-text-at-range/non-matching-section-and-range.js b/packages/slate/test/changes/at-range/insert-text-at-range/non-matching-section-and-range.js
new file mode 100644
index 000000000..90588d57f
--- /dev/null
+++ b/packages/slate/test/changes/at-range/insert-text-at-range/non-matching-section-and-range.js
@@ -0,0 +1,36 @@
+/** @jsx h */
+
+import { Point, Range } from 'slate'
+
+import h from '../../../helpers/h'
+
+export default function(change) {
+ const { key } = change.value.document.getFirstText()
+ const range = new Range({
+ anchor: new Point({ key, offset: 0 }),
+ focus: new Point({ key, offset: 3 }),
+ })
+ change.insertTextAtRange(range, 'That')
+}
+
+export const input = (
+
+
+ The change will be here.
+
+ The cursor is over here.
+
+
+
+)
+
+export const output = (
+
+
+ That change will be here.
+
+ The cursor is over here.
+
+
+
+)