public void SetBaseAndExtent(Node anchorNode, int anchorOffset, Node focusNode, int focusOffset) { if (anchorOffset > anchorNode.nodeLength) { throw new IndexSizeError($"{nameof(anchorOffset)} exceeds {nameof(anchorNode.nodeLength)}"); } if (focusOffset > focusNode.nodeLength) { throw new IndexSizeError($"{nameof(focusOffset)} exceeds {nameof(focusNode.nodeLength)}"); } if (!ReferenceEquals(document, anchorNode.getRootNode()) || !ReferenceEquals(document, focusNode.getRootNode())) { return; } var anchor = new BoundaryPoint(anchorNode, anchorOffset); var focus = new BoundaryPoint(focusNode, focusOffset); var newRange = new Range(document); if (Range.Get_Boundary_Position(anchor, focus) == EBoundaryPosition.Before) { newRange.setStart(anchor.node, anchor.offset); newRange.setEnd(focus.node, focus.offset); } else { newRange.setStart(focus.node, focus.offset); newRange.setEnd(anchor.node, anchor.offset); } _range = newRange; direction = (Range.Get_Boundary_Position(focus, anchor) == EBoundaryPosition.Before) ? ESelectionDirection.Backward : ESelectionDirection.Forward; }
public bool ContainsNode(Node node, bool allowPartialContainment = false) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (_range == null || !ReferenceEquals(document, node.getRootNode())) { return(false); } var first = new BoundaryPoint(node, 0); var last = new BoundaryPoint(node, node.nodeLength); var firstPos = Range.Get_Boundary_Position(_range.start, first); var lastPos = Range.Get_Boundary_Position(_range.end, last); if (!allowPartialContainment) { return((firstPos == EBoundaryPosition.Before || firstPos == EBoundaryPosition.Equal) && (lastPos == EBoundaryPosition.After || lastPos == EBoundaryPosition.Equal)); } else { return((firstPos == EBoundaryPosition.Before || firstPos == EBoundaryPosition.Equal) || (lastPos == EBoundaryPosition.After || lastPos == EBoundaryPosition.Equal)); } }
public void Extend(Node node, int offset = 0) { if (_range == null) { throw new InvalidStateError($"Selection has no set range"); } if (!ReferenceEquals(document, node.getRootNode())) { return; } var oldAnchor = Anchor.Value; var oldFocus = Focus.Value; var newFocus = new BoundaryPoint(node, offset); var newRange = new Range(document); if (!ReferenceEquals(_range.root, node.getRootNode())) { newRange.setStart(node, offset); newRange.setEnd(node, offset); } else if (Range.Get_Boundary_Position(oldAnchor, newFocus) != EBoundaryPosition.After) { newRange.setStart(oldAnchor.node, oldAnchor.offset); newRange.setEnd(newFocus.node, newFocus.offset); } else { newRange.setStart(newFocus.node, newFocus.offset); newRange.setEnd(oldAnchor.node, oldAnchor.offset); } _range = newRange; direction = Range.Get_Boundary_Position(newFocus, oldAnchor) == EBoundaryPosition.Before ? ESelectionDirection.Backward : ESelectionDirection.Forward; }