diff --git a/.changeset/orange-jeans-sing.md b/.changeset/orange-jeans-sing.md
new file mode 100644
index 0000000000..79390c51c7
--- /dev/null
+++ b/.changeset/orange-jeans-sing.md
@@ -0,0 +1,5 @@
+---
+'slate': patch
+---
+
+Fix mergeNodes moving node into parent sibling
diff --git a/packages/slate/src/transforms/general.ts b/packages/slate/src/transforms/general.ts
index 4c05d305b6..dea89c243b 100644
--- a/packages/slate/src/transforms/general.ts
+++ b/packages/slate/src/transforms/general.ts
@@ -150,7 +150,18 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}
}
- if (prev) {
+ let preferNext = false
+ if (prev && next) {
+ if (Path.equals(next[1], path)) {
+ preferNext = !Path.hasPrevious(next[1])
+ } else {
+ preferNext =
+ Path.common(prev[1], path).length <
+ Path.common(next[1], path).length
+ }
+ }
+
+ if (prev && !preferNext) {
point.path = prev[1]
point.offset = prev[0].text.length
} else if (next) {
diff --git a/packages/slate/test/normalization/text/merge-adjacent-empty-after-nested.tsx b/packages/slate/test/normalization/text/merge-adjacent-empty-after-nested.tsx
new file mode 100644
index 0000000000..644c4bc249
--- /dev/null
+++ b/packages/slate/test/normalization/text/merge-adjacent-empty-after-nested.tsx
@@ -0,0 +1,28 @@
+/** @jsx jsx */
+import { jsx } from '../..'
+
+export const input = (
+
+
+
+
+
+
+
+
+
+
+
+)
+export const output = (
+
+
+
+
+
+
+
+
+
+
+)
diff --git a/packages/slate/test/normalization/text/merge-adjacent-empty-after.tsx b/packages/slate/test/normalization/text/merge-adjacent-empty-after.tsx
new file mode 100644
index 0000000000..93ea45dbeb
--- /dev/null
+++ b/packages/slate/test/normalization/text/merge-adjacent-empty-after.tsx
@@ -0,0 +1,24 @@
+/** @jsx jsx */
+import { jsx } from '../..'
+
+export const input = (
+
+
+
+
+
+
+
+
+
+)
+export const output = (
+
+
+
+
+
+
+
+
+)
diff --git a/packages/slate/test/operations/remove_node/cursor-nested.tsx b/packages/slate/test/operations/remove_node/cursor-nested.tsx
new file mode 100644
index 0000000000..123e804ca3
--- /dev/null
+++ b/packages/slate/test/operations/remove_node/cursor-nested.tsx
@@ -0,0 +1,35 @@
+/** @jsx jsx */
+import { jsx } from 'slate-hyperscript'
+
+export const input = (
+
+
+
+
+
+
+
+
+
+
+
+)
+export const operations = [
+ {
+ type: 'remove_node',
+ path: [1, 0, 0],
+ node: { text: '' },
+ },
+]
+export const output = (
+
+
+
+
+
+
+
+
+
+
+)
diff --git a/packages/slate/test/operations/remove_node/cursor.tsx b/packages/slate/test/operations/remove_node/cursor.tsx
new file mode 100644
index 0000000000..f97aa7e14d
--- /dev/null
+++ b/packages/slate/test/operations/remove_node/cursor.tsx
@@ -0,0 +1,31 @@
+/** @jsx jsx */
+import { jsx } from 'slate-hyperscript'
+
+export const input = (
+
+
+
+
+
+
+
+
+
+)
+export const operations = [
+ {
+ type: 'remove_node',
+ path: [1, 0],
+ node: { text: '' },
+ },
+]
+export const output = (
+
+
+
+
+
+
+
+
+)
diff --git a/packages/slate/test/transforms/mergeNodes/path/text-hanging-nested.tsx b/packages/slate/test/transforms/mergeNodes/path/text-hanging-nested.tsx
new file mode 100644
index 0000000000..c9a2885906
--- /dev/null
+++ b/packages/slate/test/transforms/mergeNodes/path/text-hanging-nested.tsx
@@ -0,0 +1,28 @@
+/** @jsx jsx */
+import { Transforms, Text } from 'slate'
+import { jsx } from '../../..'
+
+export const input = (
+
+ one
+
+
+
+
+
+
+
+)
+export const run = editor => {
+ Transforms.mergeNodes(editor, { at: [1, 0, 1], match: Text.isText })
+}
+export const output = (
+
+ one
+
+
+
+
+
+
+)
diff --git a/packages/slate/test/transforms/mergeNodes/path/text-hanging.tsx b/packages/slate/test/transforms/mergeNodes/path/text-hanging.tsx
new file mode 100644
index 0000000000..0ffd85d243
--- /dev/null
+++ b/packages/slate/test/transforms/mergeNodes/path/text-hanging.tsx
@@ -0,0 +1,24 @@
+/** @jsx jsx */
+import { Transforms, Text } from 'slate'
+import { jsx } from '../../..'
+
+export const input = (
+
+ one
+
+
+
+
+
+)
+export const run = editor => {
+ Transforms.mergeNodes(editor, { at: [1, 1], match: Text.isText })
+}
+export const output = (
+
+ one
+
+
+
+
+)