Skip to content

Commit

Permalink
Fixed processing of the range totally outside the container
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksandr-danylchenko committed Sep 27, 2024
1 parent 8ad2afe commit dbdcf6e
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions packages/text-annotator/src/utils/trimRangeToContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@ export const trimRangeToContainer = (
): Range => {
const trimmedRange = range.cloneRange();

// If the start is outside the container - set it to the start of the container
if (!container.contains(trimmedRange.startContainer)) {
const containsStart = container.contains(trimmedRange.startContainer);
const containsEnd = container.contains(trimmedRange.endContainer);

/**
* If both the start and the end are outside the container -
* collapse such a range as irrelevant
*/
if (!containsStart && !containsEnd) {
trimmedRange.collapse();
return trimmedRange;
}

/**
* If the range starts outside the container -
* trim it to the start of the container
*/
if (!containsStart) {
trimmedRange.setStart(container, 0);
}

// If the end is outside the container - set it to the end of the container
if (!container.contains(trimmedRange.endContainer)) {
/**
* If the range ends outside the container -
* trim it to the end of the container
*/
if (!containsEnd) {
trimmedRange.setEnd(container, container.childNodes.length);
}

Expand Down

0 comments on commit dbdcf6e

Please sign in to comment.