From 844ca249d0276691436e0b8d8fc2388756b386db Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Mon, 14 Aug 2017 17:31:16 +0100 Subject: [PATCH] Fix bugs in textOffsetsToSelectionState This just had some thinkos in it. Namely the conditionals were slightly wrong and this lead to negative offset selection state being returned, causing vector-im/riot-web#4792 fixes vector-im/riot-web#4792 --- src/RichText.js | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/RichText.js b/src/RichText.js index 9876fcc93f..8d1177cdd2 100644 --- a/src/RichText.js +++ b/src/RichText.js @@ -204,23 +204,27 @@ export function textOffsetsToSelectionState({start, end}: SelectionRange, let selectionState = SelectionState.createEmpty(); for (const block of contentBlocks) { const blockLength = block.getLength(); - if (start !== -1 && start < blockLength) { - selectionState = selectionState.merge({ - anchorKey: block.getKey(), - anchorOffset: start, - }); - start = -1; - } else { - start -= blockLength + 1; // +1 to account for newline between blocks + if (start !== -1) { + if (start < blockLength + 1) { + selectionState = selectionState.merge({ + anchorKey: block.getKey(), + anchorOffset: start, + }); + start = -1; + } else { + start -= blockLength + 1; // +1 to account for newline between blocks + } } - if (end !== -1 && end <= blockLength) { - selectionState = selectionState.merge({ - focusKey: block.getKey(), - focusOffset: end, - }); - end = -1; - } else { - end -= blockLength + 1; // +1 to account for newline between blocks + if (end !== -1) { + if (end < blockLength + 1) { + selectionState = selectionState.merge({ + focusKey: block.getKey(), + focusOffset: end, + }); + end = -1; + } else { + end -= blockLength + 1; // +1 to account for newline between blocks + } } } return selectionState;