diff --git a/packages/slate/src/changes/at-range.js b/packages/slate/src/changes/at-range.js
index ca5e5bc11..b98c5e7c1 100644
--- a/packages/slate/src/changes/at-range.js
+++ b/packages/slate/src/changes/at-range.js
@@ -863,8 +863,8 @@ Changes.insertTextAtRange = (change, range, text, marks, options = {}) => {
}
// PERF: Unless specified, don't normalize if only inserting text.
- if (normalize !== undefined) {
- normalize = range.isExpanded
+ if (normalize === undefined) {
+ normalize = range.isExpanded && marks.size !== 0
}
change.insertTextByKey(key, offset, text, marks, { normalize: false })
@@ -877,7 +877,10 @@ Changes.insertTextAtRange = (change, range, text, marks, options = {}) => {
const normalizeAncestor = ancestors.findLast(n =>
change.value.document.getDescendant(n.key)
)
- change.normalizeNodeByKey(normalizeAncestor.key)
+ // it is possible that normalizeAncestor doesn't return any node
+ // on that case fallback to startKey to be normalized
+ const normalizeKey = normalizeAncestor ? normalizeAncestor.key : startKey
+ change.normalizeNodeByKey(normalizeKey)
}
}
diff --git a/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js b/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js
new file mode 100644
index 000000000..25a25d2d4
--- /dev/null
+++ b/packages/slate/test/changes/at-current-range/insert-text/expanded-with-mark.js
@@ -0,0 +1,40 @@
+/** @jsx h */
+
+import h from '../../../helpers/h'
+
+/*
+ * This test makes sure a normalization happens on insertText on expandedRange
+ */
+
+export default function(change) {
+ change.insertText('a')
+ change.insertText('b')
+}
+
+export const input = (
+
+
+
+
+ lorem
+
+ ipsum
+
+
+ ipsum
+
+
+
+)
+
+export const output = (
+
+
+
+
+ ab
+
+
+
+
+)