Skip to content

Commit fe5a410

Browse files
committed
Read Aloud: Fix splitRanges() handling of collapsed ranges
1 parent bdeb785 commit fe5a410

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/dom/common/lib/range.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,20 @@ export function splitRanges(
356356
if (containedEnd) {
357357
let after = range.cloneRange();
358358
after.setStart(splitAtRange.endContainer, splitAtRange.endOffset);
359-
if (!after.collapsed) splitRanges.push(after);
359+
if (!after.collapsed) {
360+
splitRanges.push(after);
361+
// When splitAtRange is collapsed and falls in a gap between
362+
// ranges, both containedStart and containedEnd are true for
363+
// the range after the gap. The 'middle' portion (from
364+
// splitAt.start to splitAt.end) is also collapsed and gets
365+
// skipped, leaving splitIndex at -1. Without this fix,
366+
// startIndex would be newRanges.length + (-1), incorrectly
367+
// pointing to the previous range. Point to 'after' instead,
368+
// which is the first range at/after the split point.
369+
if (containedStart && splitIndex === -1) {
370+
splitIndex = splitRanges.length - 1;
371+
}
372+
}
360373
}
361374

362375
if (containedStart) {

0 commit comments

Comments
 (0)