mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 07:13:08 -05:00
Fix crash linkifying text in middle of node
We were looking at the selection properties after we had mutated the DOM, and trying to manipulate them based on some numbers cached from before mutating the DOM, which could result in trying to set a negative index for the selection offset. Instead, calculate all our values before we do any mutations. Fixes #430
This commit is contained in:
parent
a8cd95a688
commit
cbde45311a
1 changed files with 6 additions and 6 deletions
|
@ -103,6 +103,8 @@ const linkifyText = (self: Squire, textNode: Text, offset: number): void => {
|
|||
|
||||
const index = searchFrom + match.index;
|
||||
const endIndex = index + match[0].length;
|
||||
const needsSelectionUpdate = selection.startContainer === textNode;
|
||||
const newSelectionOffset = selection.startOffset - endIndex;
|
||||
if (index) {
|
||||
textNode = textNode.splitText(index);
|
||||
}
|
||||
|
@ -123,13 +125,11 @@ const linkifyText = (self: Squire, textNode: Text, offset: number): void => {
|
|||
);
|
||||
link.textContent = data.slice(index, endIndex);
|
||||
textNode.parentNode!.insertBefore(link, textNode);
|
||||
|
||||
const startOffset = selection.startOffset;
|
||||
textNode.data = data.slice(endIndex);
|
||||
if (selection.startContainer === textNode) {
|
||||
const newOffset = startOffset - endIndex;
|
||||
selection.setStart(textNode, newOffset);
|
||||
selection.setEnd(textNode, newOffset);
|
||||
|
||||
if (needsSelectionUpdate) {
|
||||
selection.setStart(textNode, newSelectionOffset);
|
||||
selection.setEnd(textNode, newSelectionOffset);
|
||||
}
|
||||
self.setSelection(selection);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue