Skip to content

Commit 4961e20

Browse files
Added empty text node content check before processing
1 parent 1c34de0 commit 4961e20

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

packages/text-annotator/src/SelectionHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
splitAnnotatableRanges,
1414
rangeToSelector,
1515
isMac,
16-
isWhitespaceOrEmpty,
16+
isRangeWhitespaceOrEmpty,
1717
trimRangeToContainer,
1818
isNotAnnotatable
1919
} from './utils';
@@ -150,7 +150,7 @@ export const SelectionHandler = (
150150
selectionRanges.map(r => trimRangeToContainer(r, container));
151151

152152
// The selection should be captured only within the annotatable container
153-
if (containedRanges.every(r => isWhitespaceOrEmpty(r))) return;
153+
if (containedRanges.every(r => isRangeWhitespaceOrEmpty(r))) return;
154154

155155
const annotatableRanges = containedRanges.flatMap(r => splitAnnotatableRanges(container, r.cloneRange()));
156156

packages/text-annotator/src/utils/getHighlightClientRects.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
import { isNodeWhitespaceOrEmpty } from './isWhitespaceOrEmpty';
2+
13
export const getHighlightClientRects = (range: Range) => {
24
const textNodes: Text[] = [];
35

46
// Get all text nodes inside the range's commonAncestorContainer
57
const it = document.createNodeIterator(range.commonAncestorContainer, NodeFilter.SHOW_TEXT);
68

7-
// Filter text nodes that intersect the range. Note that we could
8-
// also include this filter into the node iterator directly. But
9-
// The while loop is faster (!) - possibly due to function call overhead.
9+
/*
10+
Filter text nodes that intersect the range. Note that we could
11+
also include this filter in the node iterator directly.
12+
But the while loop is faster (!) - possibly due to a function call overhead.
13+
*/
1014
let currentNode: Text | undefined;
1115

1216
while ((currentNode = it.nextNode() as Text)) {
13-
if (range.intersectsNode(currentNode))
17+
if (range.intersectsNode(currentNode) && !isNodeWhitespaceOrEmpty(currentNode)) {
1418
textNodes.push(currentNode);
19+
}
1520
}
1621

1722
if (textNodes.length < 2) {
18-
// Trivial case: selection inside a single text
19-
// node, or empty (shouldn't happen!) - no need
20-
// to create our own ranges.
23+
/*
24+
Trivial case: selection is inside a single text node
25+
or empty (shouldn't happen!) - no need to create our own ranges.
26+
*/
2127
return Array.from(range.getClientRects());
2228
} else {
2329
const first = textNodes[0];
@@ -50,4 +56,4 @@ export const getHighlightClientRects = (range: Range) => {
5056
...Array.from(lastRange.getClientRects())
5157
];
5258
}
53-
}
59+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const whitespaceOrEmptyRegex = /^\s*$/;
22

3-
export const isWhitespaceOrEmpty = (range: Range): boolean => whitespaceOrEmptyRegex.test(range.toString())
3+
export const isRangeWhitespaceOrEmpty = (range: Range): boolean => whitespaceOrEmptyRegex.test(range.toString())
4+
5+
export const isNodeWhitespaceOrEmpty = (node: Node): boolean => whitespaceOrEmptyRegex.test(node.textContent || '')

0 commit comments

Comments
 (0)