java.lang.Object
org.htmlunit.html.impl.SimpleRange
- All Implemented Interfaces:
Serializable
Simple implementation of a DOM Range.
- Author:
- Marc Guillemot, Daniel Gredler, James Phillpotts, Ahmed Ashour, Ronald Brill
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty range without boundary points.SimpleRange(DomNode node) Constructs a range enclosing the contents of the specified node.SimpleRange(DomNode node, int offset) Constructs a collapsed range at the specified offset within a node.SimpleRange(DomNode startNode, int startOffset, DomNode endNode, int endOffset) Constructs a range with the specified start and end boundary points. -
Method Summary
Modifier and TypeMethodDescriptionDuplicates the contents of this range into a document fragment.Produces a newSimpleRangewith boundary points identical to this range.voidcollapse(boolean toStart) Collapses this range onto one of its boundary points.Returns the nodes fully contained within this range in document order.voidRemoves the contents of this range from the document tree.booleanMoves the contents of this range from the document tree into a new DocumentFragment.Returns the deepest common ancestor container of this range's boundary points.Returns the node within which this range ends.intReturns the offset within the end container.Returns the node within which this range begins.intReturns the offset within the start container.inthashCode()voidinsertNode(DomNode newNode) Inserts a node into the Document or DocumentFragment at the start of the Range.booleanIndicates whether this range is collapsed (start boundary equals end boundary).voidselectNode(DomNode node) Select a node and its contents.voidselectNodeContents(DomNode node) Select the contents within a node.voidSets the end boundary point of this range.voidSets the start boundary point of this range.voidsurroundContents(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.toString()
-
Constructor Details
-
SimpleRange
public SimpleRange()Constructs an empty range without boundary points. -
SimpleRange
Constructs a range enclosing the contents of the specified node.- Parameters:
node- the node whose contents will be enclosed by this range
-
SimpleRange
Constructs a collapsed range at the specified offset within a node.- Parameters:
node- the container node for start and end boundariesoffset- the character or node index for both start and end boundaries
-
SimpleRange
Constructs a range with the specified start and end boundary points.- Parameters:
startNode- the start container nodestartOffset- the start offset within the start nodeendNode- the end container nodeendOffset- the end offset within the end node
-
-
Method Details
-
cloneContents
Duplicates the contents of this range into a document fragment.- Returns:
- a
DomDocumentFragmentcontaining cloned contents of this range
-
cloneRange
Produces a newSimpleRangewith 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- iftrue, 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
Moves the contents of this range from the document tree into a new DocumentFragment.- Returns:
- a
DomDocumentFragmentcontaining the extracted contents - Throws:
DOMException
-
isCollapsed
public boolean isCollapsed()Indicates whether this range is collapsed (start boundary equals end boundary).- Returns:
trueif start container equals end container and start offset equals end offset
-
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 hittingnullaligns their paths so both pointers travel the exact same total distance (depth(start) + depth(end)).- Returns:
- the lowest common ancestor
DomNode, ornullif either boundary point isnullor they belong to disconnected trees
-
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
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
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
Select a node and its contents.- Parameters:
node- The node to select.
-
selectNodeContents
Select the contents within a node.- Parameters:
node- Node to select from
-
setEnd
Sets the end boundary point of this range.- Parameters:
refNode- the end container nodeoffset- the offset within the end node
-
setStart
Sets the start boundary point of this range.- Parameters:
refNode- the start container nodeoffset- the offset within the start node
-
surroundContents
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
InvalidStateErrorif 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_ERRif any non-Text node is partially contained in this range
-
equals
-
hashCode
public int hashCode() -
toString
-
containedNodes
Returns the nodes fully contained within this range in document order.- Returns:
- a list with all nodes contained in this range
-