isNodeInSelectionRange function

bool isNodeInSelectionRange(
  1. List<CaretStop> stops,
  2. Cursor cursor,
  3. String nodeId
)

Checks whether nodeId (with its two caret stops at offset 0 and 1) falls within the current selection range defined by cursor.

Implementation

bool isNodeInSelectionRange(
  List<CaretStop> stops,
  Cursor cursor,
  String nodeId,
) {
  if (cursor.isCollapsed) return false;
  final anchorIdx = findStopIndex(stops, cursor.anchorId, cursor.anchorOffset);
  final focusIdx = findStopIndex(stops, cursor.focusId, cursor.focusOffset);
  final node0Idx = findStopIndex(stops, nodeId, 0);
  final node1Idx = findStopIndex(stops, nodeId, 1);
  if (anchorIdx < 0 || focusIdx < 0 || node0Idx < 0 || node1Idx < 0) return false;
  final lo = anchorIdx < focusIdx ? anchorIdx : focusIdx;
  final hi = anchorIdx < focusIdx ? focusIdx : anchorIdx;
  return lo <= node0Idx && node1Idx <= hi;
}