From 4d0ccc8e81e23f024c05e1af4b8cbdde721fbe38 Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Thu, 7 Mar 2019 16:01:59 -0700 Subject: [PATCH] Fix Edge positioning with newlines (#2622) * Fix Edge positioning with newlines Edge has a [bug][1] where `Range.prototype.toString()` will convert `\n` into `\r\n`. The bug causes a loop when slate-react attempts to reposition its cursor to match the native position. This change avoids calling `Range.prototype.toString()` by cloning the content nodes and measuring `textContent` on those instead. [1]: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/ * Add COMPAT message --- packages/slate-react/src/utils/find-point.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/slate-react/src/utils/find-point.js b/packages/slate-react/src/utils/find-point.js index 76dafc527..3b2d191c7 100644 --- a/packages/slate-react/src/utils/find-point.js +++ b/packages/slate-react/src/utils/find-point.js @@ -51,7 +51,12 @@ function findPoint(nativeNode, nativeOffset, editor) { range.setStart(textNode, 0) range.setEnd(nearestNode, nearestOffset) node = textNode - offset = range.toString().length + + // COMPAT: Edge has a bug where Range.prototype.toString() will convert \n + // into \r\n. The bug causes a loop when slate-react attempts to reposition + // its cursor to match the native position. Use textContent.length instead. + // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/10291116/ + offset = range.cloneContents().textContent.length } else { // For void nodes, the element with the offset key will be a cousin, not an // ancestor, so find it by going down from the nearest void parent.