Skip to content

Commit a462c66

Browse files
Added execution abortion on a missing anchorNode
1 parent f7a5ee9 commit a462c66

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

packages/text-annotator/src/SelectionHandler.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,25 @@ export const SelectionHandler = (
7777
const onSelectionChange = debounce((evt: Event) => {
7878
const sel = document.getSelection();
7979

80-
// This is to handle cases where the selection is "hijacked" by another element
81-
// in a not-annotatable area. A rare case in theory. But rich text editors
82-
// will like Quill do it...
83-
if (sel?.anchorNode && isNotAnnotatable(sel.anchorNode)) {
80+
/**
81+
* In iOS when a user clicks on a button, the `selectionchange` event is fired.
82+
* However, the generated selection is empty and the `anchorNode` is `null`.
83+
* That doesn't give us information about whether the selection is in the annotatable area
84+
* or whether the previously selected text was dismissed.
85+
* Therefore - we should bail out from such a range processing.
86+
*
87+
* @see https://github.com/recogito/text-annotator-js/pull/164#issuecomment-2416961473
88+
*/
89+
if (!sel?.anchorNode) {
90+
return;
91+
}
92+
93+
/**
94+
* This is to handle cases where the selection is "hijacked" by another element
95+
* in a not-annotatable area. A rare case in theory. But rich text editors
96+
* will like Quill do it...
97+
*/
98+
if (isNotAnnotatable(sel.anchorNode)) {
8499
currentTarget = undefined;
85100
return;
86101
}

0 commit comments

Comments
 (0)