Module org.htmlunit

Class SimpleRange

java.lang.Object
org.htmlunit.html.impl.SimpleRange
All Implemented Interfaces:
Serializable

public class SimpleRange extends Object implements Serializable
Simple implementation of a DOM Range.
Author:
Marc Guillemot, Daniel Gredler, James Phillpotts, Ahmed Ashour, Ronald Brill
See Also:
  • Constructor Details

    • SimpleRange

      public SimpleRange()
      Constructs an empty range without boundary points.
    • SimpleRange

      public SimpleRange(DomNode node)
      Constructs a range enclosing the contents of the specified node.
      Parameters:
      node - the node whose contents will be enclosed by this range
    • SimpleRange

      public SimpleRange(DomNode node, int offset)
      Constructs a collapsed range at the specified offset within a node.
      Parameters:
      node - the container node for start and end boundaries
      offset - the character or node index for both start and end boundaries
    • SimpleRange

      public SimpleRange(DomNode startNode, int startOffset, DomNode endNode, int endOffset)
      Constructs a range with the specified start and end boundary points.
      Parameters:
      startNode - the start container node
      startOffset - the start offset within the start node
      endNode - the end container node
      endOffset - the end offset within the end node
  • Method Details

    • cloneContents

      public DomDocumentFragment cloneContents()
      Duplicates the contents of this range into a document fragment.
      Returns:
      a DomDocumentFragment containing cloned contents of this range
    • cloneRange

      public SimpleRange cloneRange()
      Produces a new SimpleRange with boundary points identical to this range.
      Returns:
      a cloned instance of this range
    • collapse

      public void collapse(boolean toStart)
      Collapses this range onto one of its boundary points.
      Parameters:
      toStart - if true, collapses the range to its start boundary; otherwise collapses it to its end boundary
    • deleteContents

      public void deleteContents()
      Removes the contents of this range from the document tree.
    • extractContents

      public DomDocumentFragment extractContents() throws DOMException
      Moves the contents of this range from the document tree into a new DocumentFragment.
      Returns:
      a DomDocumentFragment containing the extracted contents
      Throws:
      DOMException
    • isCollapsed

      public boolean isCollapsed()
      Indicates whether this range is collapsed (start boundary equals end boundary).
      Returns:
      true if start container equals end container and start offset equals end offset
    • getCommonAncestorContainer

      public DomNode getCommonAncestorContainer()
      Returns the deepest common ancestor container of this range's boundary points.

      Uses a two-pointer algorithm that requires O(1) memory by avoiding collections like HashSet. If the two boundary nodes are at different depths in the tree, switching pointers to the opposite starting node when hitting null aligns their paths so both pointers travel the exact same total distance (depth(start) + depth(end)).

      Returns:
      the lowest common ancestor DomNode, or null if either boundary point is null or they belong to disconnected trees
    • getEndContainer

      public DomNode getEndContainer()
      Returns the node within which this range ends.
      Returns:
      the Node within which this range ends
    • getEndOffset

      public int getEndOffset()
      Returns the offset within the end container.
      Returns:
      offset within the ending node of this
    • getStartContainer

      public DomNode getStartContainer()
      Returns the node within which this range begins.
      Returns:
      the Node within which this range begins
    • getStartOffset

      public int getStartOffset()
      Returns the offset within the start container.
      Returns:
      offset within the starting node of this
    • insertNode

      public void insertNode(DomNode newNode)
      Inserts a node into the Document or DocumentFragment at the start of the Range. If the container is a Text node, this will be split at the start of the Range (as if the Text node's splitText method was performed at the insertion point) and the insertion will occur between the two resulting Text nodes. Adjacent Text nodes will not be automatically merged. If the node to be inserted is a DocumentFragment node, the children will be inserted rather than the DocumentFragment node itself.
      Parameters:
      newNode - The node to insert at the start of the Range
    • selectNode

      public void selectNode(DomNode node)
      Select a node and its contents.
      Parameters:
      node - The node to select.
    • selectNodeContents

      public void selectNodeContents(DomNode node)
      Select the contents within a node.
      Parameters:
      node - Node to select from
    • setEnd

      public void setEnd(DomNode refNode, int offset)
      Sets the end boundary point of this range.
      Parameters:
      refNode - the end container node
      offset - the offset within the end node
    • setStart

      public void setStart(DomNode refNode, int offset)
      Sets the start boundary point of this range.
      Parameters:
      refNode - the start container node
      offset - the offset within the start node
    • surroundContents

      public void surroundContents(DomNode newParent)
      Reparents the contents of the Range to the given node and inserts the node at the position of the start of the Range.

      Per the DOM spec, throws an InvalidStateError if any non-Text node is only partially contained by the range (i.e. the range's start or end boundary splits an element, leaving one boundary tag inside the range and the other outside). A range that spans only Text nodes, or that fully encloses every element it touches, is always valid.

      Parameters:
      newParent - the node to surround the contents with
      Throws:
      DOMException - INVALID_STATE_ERR if any non-Text node is partially contained in this range
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • containedNodes

      public List<DomNode> containedNodes()
      Returns the nodes fully contained within this range in document order.
      Returns:
      a list with all nodes contained in this range