diff --git a/packages/slate-react/src/utils/find-dom-point.js b/packages/slate-react/src/utils/find-dom-point.js index 6414fc4d3..e6c839a2b 100644 --- a/packages/slate-react/src/utils/find-dom-point.js +++ b/packages/slate-react/src/utils/find-dom-point.js @@ -21,14 +21,17 @@ function findDOMPoint(point, win = window) { for (const text of texts) { const node = text.childNodes[0] - const length = text.hasAttribute('data-slate-length') - ? parseInt(text.getAttribute('data-slate-length'), 10) - : node.textContent.length + const domLength = node.textContent.length + let slateLength = domLength - const end = start + length + if (text.hasAttribute('data-slate-length')) { + slateLength = parseInt(text.getAttribute('data-slate-length'), 10) + } + + const end = start + slateLength if (point.offset <= end) { - const offset = Math.max(0, point.offset - start) + const offset = Math.min(domLength, Math.max(0, point.offset - start)) return { node, offset } }