File tree Expand file tree Collapse file tree 1 file changed +22
-4
lines changed
packages/text-annotator/src/utils Expand file tree Collapse file tree 1 file changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,31 @@ export const trimRangeToContainer = (
4
4
) : Range => {
5
5
const trimmedRange = range . cloneRange ( ) ;
6
6
7
- // If the start is outside the container - set it to the start of the container
8
- if ( ! container . contains ( trimmedRange . startContainer ) ) {
7
+ const containsStart = container . contains ( trimmedRange . startContainer ) ;
8
+ const containsEnd = container . contains ( trimmedRange . endContainer ) ;
9
+
10
+ /**
11
+ * If both the start and the end are outside the container -
12
+ * collapse such a range as irrelevant
13
+ */
14
+ if ( ! containsStart && ! containsEnd ) {
15
+ trimmedRange . collapse ( ) ;
16
+ return trimmedRange ;
17
+ }
18
+
19
+ /**
20
+ * If the range starts outside the container -
21
+ * trim it to the start of the container
22
+ */
23
+ if ( ! containsStart ) {
9
24
trimmedRange . setStart ( container , 0 ) ;
10
25
}
11
26
12
- // If the end is outside the container - set it to the end of the container
13
- if ( ! container . contains ( trimmedRange . endContainer ) ) {
27
+ /**
28
+ * If the range ends outside the container -
29
+ * trim it to the end of the container
30
+ */
31
+ if ( ! containsEnd ) {
14
32
trimmedRange . setEnd ( container , container . childNodes . length ) ;
15
33
}
16
34
You can’t perform that action at this time.
0 commit comments