Modifier and Type | Class and Description |
---|---|
private static class |
Node.OuterHtmlVisitor |
Modifier and Type | Field and Description |
---|---|
(package private) static java.util.List<Node> |
EmptyNodes |
(package private) static java.lang.String |
EmptyString |
(package private) Node |
parentNode |
(package private) int |
siblingIndex |
Modifier | Constructor and Description |
---|---|
protected |
Node()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
java.lang.String |
absUrl(java.lang.String attributeKey)
Get an absolute URL from a URL attribute that may be relative (such as an
<a href> or
<img src> ). |
protected void |
addChildren(int index,
Node... children) |
protected void |
addChildren(Node... children) |
private void |
addSiblingHtml(int index,
java.lang.String html) |
Node |
after(Node node)
Insert the specified node into the DOM after this node (as a following sibling).
|
Node |
after(java.lang.String html)
Insert the specified HTML into the DOM after this node (as a following sibling).
|
java.lang.String |
attr(java.lang.String attributeKey)
Get an attribute's value by its key.
|
Node |
attr(java.lang.String attributeKey,
java.lang.String attributeValue)
Set an attribute (key=value).
|
abstract Attributes |
attributes()
Get each of the element's attributes.
|
int |
attributesSize()
Get the number of attributes that this Node has.
|
abstract java.lang.String |
baseUri()
Get the base URI that applies to this node.
|
Node |
before(Node node)
Insert the specified node into the DOM before this node (as a preceding sibling).
|
Node |
before(java.lang.String html)
Insert the specified HTML into the DOM before this node (as a preceding sibling).
|
Node |
childNode(int index)
Get a child node by its 0-based index.
|
java.util.List<Node> |
childNodes()
Get this node's children.
|
protected Node[] |
childNodesAsArray() |
java.util.List<Node> |
childNodesCopy()
Returns a deep copy of this node's children.
|
abstract int |
childNodeSize()
Get the number of child nodes that this node holds.
|
Node |
clearAttributes()
Clear (remove) each of the attributes in this node.
|
Node |
clone()
Create a stand-alone, deep copy of this node, and all of its children.
|
protected Node |
doClone(Node parent) |
protected abstract void |
doSetBaseUri(java.lang.String baseUri)
Set the baseUri for just this node (not its descendants), if this Node tracks base URIs.
|
abstract Node |
empty()
Delete all this node's children.
|
protected abstract java.util.List<Node> |
ensureChildNodes() |
boolean |
equals(java.lang.Object o)
Check if this node is the same instance of another (object identity test).
|
Node |
filter(NodeFilter nodeFilter)
Perform a depth-first filtering through this node and its descendants.
|
Node |
firstChild()
Gets the first child node of this node, or
null if there is none. |
Node |
forEachNode(java.util.function.Consumer<? super Node> action)
Perform the supplied action on this Node and each of its descendants, during a depth-first traversal.
|
Node |
forEachNode(Consumer<? super Node> action)
Deprecated.
Use
forEachNode(Consumer) instead. |
private Element |
getDeepChild(Element el) |
boolean |
hasAttr(java.lang.String attributeKey)
Test if this Node has an attribute.
|
protected abstract boolean |
hasAttributes()
Check if this Node has an actual Attributes object.
|
int |
hashCode()
Provides a hashCode for this Node, based on its object identity.
|
boolean |
hasParent()
Checks if this node has a parent.
|
boolean |
hasSameValue(java.lang.Object o)
Check if this node has the same content as another node.
|
<T extends java.lang.Appendable> |
html(T appendable)
Write this node and its children to the given
Appendable . |
protected void |
indent(java.lang.Appendable accum,
int depth,
Document.OutputSettings out) |
(package private) boolean |
isEffectivelyFirst()
Test if this node is the first child, or first following blank text.
|
(package private) static boolean |
isNode(Node node,
java.lang.String normalName)
Test if this node is not null and has the supplied normal name.
|
(package private) boolean |
isNode(java.lang.String normalName)
Test if this node has the supplied normal name.
|
Node |
lastChild()
Gets the last child node of this node, or
null if there is none. |
Node |
nextSibling()
Get this node's next sibling.
|
(package private) void |
nodelistChanged() |
abstract java.lang.String |
nodeName()
Get the node name of this node.
|
java.lang.String |
normalName()
Get the normalized name of this node.
|
java.lang.String |
outerHtml()
Get the outer HTML of this node.
|
protected void |
outerHtml(java.lang.Appendable accum) |
(package private) abstract void |
outerHtmlHead(java.lang.Appendable accum,
int depth,
Document.OutputSettings out)
Get the outer HTML of this node.
|
(package private) abstract void |
outerHtmlTail(java.lang.Appendable accum,
int depth,
Document.OutputSettings out) |
Document |
ownerDocument()
Gets the Document associated with this Node.
|
Node |
parent()
Gets this node's parent node.
|
Node |
parentNode()
Gets this node's parent node.
|
Node |
previousSibling()
Get this node's previous sibling.
|
private void |
reindexChildren(int start) |
void |
remove()
Remove (delete) this node from the DOM tree.
|
Node |
removeAttr(java.lang.String attributeKey)
Remove an attribute from this node.
|
protected void |
removeChild(Node out) |
protected void |
reparentChild(Node child) |
protected void |
replaceChild(Node out,
Node in) |
void |
replaceWith(Node in)
Replace this node in the DOM with the supplied node.
|
Node |
root()
Get this node's root node; that is, its topmost ancestor.
|
void |
setBaseUri(java.lang.String baseUri)
Update the base URI of this node and all of its descendants.
|
protected void |
setParentNode(Node parentNode) |
protected void |
setSiblingIndex(int siblingIndex) |
Node |
shallowClone()
Create a stand-alone, shallow copy of this node.
|
int |
siblingIndex()
Get the list index of this node in its node sibling list.
|
java.util.List<Node> |
siblingNodes()
Retrieves this node's sibling nodes.
|
Range |
sourceRange()
Get the source range (start and end positions) in the original input source that this node was parsed from.
|
java.lang.String |
toString()
Gets this node's outer HTML.
|
Node |
traverse(NodeVisitor nodeVisitor)
Perform a depth-first traversal through this node and its descendants.
|
Node |
unwrap()
Removes this node from the DOM, and moves its children up into the node's parent.
|
Node |
wrap(java.lang.String html)
Wrap the supplied HTML around this node.
|
static final java.util.List<Node> EmptyNodes
static final java.lang.String EmptyString
@Nullable Node parentNode
int siblingIndex
protected Node()
public abstract java.lang.String nodeName()
public java.lang.String normalName()
nodeName()
.
For an Element, will be the lower-cased tag name.protected abstract boolean hasAttributes()
public boolean hasParent()
shallowClone()
. In such cases, parent()
will return null
.public java.lang.String attr(java.lang.String attributeKey)
To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs
,
which is a shortcut to the absUrl(java.lang.String)
method.
String url = a.attr("abs:href");
attributeKey
- The attribute key.attributes()
,
hasAttr(String)
,
absUrl(String)
public abstract Attributes attributes()
public int attributesSize()
public Node attr(java.lang.String attributeKey, java.lang.String attributeValue)
attributeKey
- The attribute key.attributeValue
- The attribute value.public boolean hasAttr(java.lang.String attributeKey)
attributeKey
- The attribute key to check.public Node removeAttr(java.lang.String attributeKey)
attributeKey
- The attribute to remove.public Node clearAttributes()
public abstract java.lang.String baseUri()
absUrl(java.lang.String)
protected abstract void doSetBaseUri(java.lang.String baseUri)
baseUri
- new URIpublic void setBaseUri(java.lang.String baseUri)
baseUri
- base URI to setpublic java.lang.String absUrl(java.lang.String attributeKey)
<a href>
or
<img src>
).
E.g.: String absUrl = linkEl.absUrl("href");
If the attribute value is already absolute (i.e. it starts with a protocol, like
http://
or https://
etc), and it successfully parses as a URL, the attribute is
returned directly. Otherwise, it is treated as a URL relative to the element's baseUri()
, and made
absolute using that.
As an alternate, you can use the attr(java.lang.String)
method with the abs:
prefix, e.g.:
String absUrl = linkEl.attr("abs:href");
attributeKey
- The attribute keyattr(java.lang.String)
,
URL(java.net.URL, String)
protected abstract java.util.List<Node> ensureChildNodes()
public Node childNode(int index)
index
- index of child nodeIndexOutOfBoundsException
if the index is out of bounds.public java.util.List<Node> childNodes()
public java.util.List<Node> childNodesCopy()
public abstract int childNodeSize()
protected Node[] childNodesAsArray()
public abstract Node empty()
@Nullable public Node parent()
hasParent()
@Nullable public final Node parentNode()
public Node root()
this
.@Nullable public Document ownerDocument()
public void remove()
public Node before(java.lang.String html)
html
- HTML to add before this nodeafter(String)
public Node before(Node node)
node
- to add before this nodeafter(Node)
public Node after(java.lang.String html)
html
- HTML to add after this nodebefore(String)
public Node after(Node node)
node
- to add after this nodebefore(Node)
private void addSiblingHtml(int index, java.lang.String html)
public Node wrap(java.lang.String html)
html
- HTML to wrap around this node, e.g. <div class="head"></div>
. Can be arbitrarily deep. If
the input HTML does not parse to a result starting with an Element, this will be a no-op.@Nullable public Node unwrap()
For example, with the input html:
<div>One <span>Two <b>Three</b></span></div>
element.unwrap()
on the span
element will result in the html:
<div>One Two <b>Three</b></div>
"Two "
TextNode
being returned.remove()
,
wrap(String)
void nodelistChanged()
public void replaceWith(Node in)
in
- the node that will replace the existing node.protected void setParentNode(Node parentNode)
protected void removeChild(Node out)
protected void addChildren(Node... children)
protected void addChildren(int index, Node... children)
protected void reparentChild(Node child)
private void reindexChildren(int start)
public java.util.List<Node> siblingNodes()
node.parent.childNodes()
, but does not
include this node (a node is not a sibling of itself).@Nullable public Node nextSibling()
@Nullable public Node previousSibling()
public int siblingIndex()
Element.elementSiblingIndex()
protected void setSiblingIndex(int siblingIndex)
@Nullable public Node firstChild()
null
if there is none. This could be any Node type, such as an
Element, TextNode, Comment, etc. Use Element.firstElementChild()
to get the first Element child.Element.firstElementChild()
,
lastChild()
@Nullable public Node lastChild()
null
if there is none.Element.lastElementChild()
,
firstChild()
public Node traverse(NodeVisitor nodeVisitor)
nodeVisitor
- the visitor callbacks to perform on each nodepublic Node forEachNode(java.util.function.Consumer<? super Node> action)
action
- the function to perform on the nodeElement.forEach(Consumer)
@Deprecated public Node forEachNode(Consumer<? super Node> action)
forEachNode(Consumer)
instead.public Node filter(NodeFilter nodeFilter)
nodeFilter
- the filter callbacks to perform on each nodepublic java.lang.String outerHtml()
p
element, may return <p>Para</p>
.Element.html()
,
Element.text()
protected void outerHtml(java.lang.Appendable accum)
abstract void outerHtmlHead(java.lang.Appendable accum, int depth, Document.OutputSettings out) throws java.io.IOException
accum
- accumulator to place HTML intojava.io.IOException
- if appending to the given accumulator fails.abstract void outerHtmlTail(java.lang.Appendable accum, int depth, Document.OutputSettings out) throws java.io.IOException
java.io.IOException
public <T extends java.lang.Appendable> T html(T appendable)
Appendable
.appendable
- the Appendable
to write to.Appendable
, for chaining.public Range sourceRange()
Parser.setTrackPosition(boolean)
,
Element.endSourceRange()
static boolean isNode(@Nullable Node node, java.lang.String normalName)
final boolean isNode(java.lang.String normalName)
final boolean isEffectivelyFirst()
public java.lang.String toString()
toString
in class java.lang.Object
outerHtml()
protected void indent(java.lang.Appendable accum, int depth, Document.OutputSettings out) throws java.io.IOException
java.io.IOException
public boolean equals(@Nullable java.lang.Object o)
For an node value equality check, see hasSameValue(Object)
equals
in class java.lang.Object
o
- other object to compare tohasSameValue(Object)
public int hashCode()
hashCode
in class java.lang.Object
public boolean hasSameValue(@Nullable java.lang.Object o)
o
- other object to compare topublic Node clone()
The cloned node may be adopted into another Document or node structure using Element.appendChild(Node)
.
clone
in class java.lang.Object
shallowClone()
public Node shallowClone()
clone()