public class Element extends Node
Constructor and Description |
---|
Element(java.lang.String tag)
Create a new, standalone element.
|
Element(Tag tag,
java.lang.String baseUri)
Create a new ReactElement from a tag and a base URI.
|
Element(Tag tag,
java.lang.String baseUri,
Attributes attributes)
Create a new, standalone ReactElement.
|
Modifier and Type | Method and Description |
---|---|
Element |
addClass(java.lang.String className)
Add a class name to this element's
class attribute. |
Element |
after(Node node)
Insert the specified node into the DOM after this node (as a following sibling).
|
Element |
after(java.lang.String html)
Insert the specified HTML into the DOM after this element (as a following sibling).
|
Element |
append(java.lang.String html)
Add inner HTML to this element.
|
Element |
appendChild(Node child)
Add a node child node to this element.
|
Element |
appendElement(java.lang.String tagName)
Create a new element by tag name, and add it as the last child.
|
Element |
appendText(java.lang.String text)
Create and append a new TextNode to this element.
|
Element |
appendTo(Element parent)
Add this element to the supplied parent element, as its next child.
|
Element |
attr(java.lang.String attributeKey,
boolean attributeValue)
Set a boolean attribute value on this element.
|
Element |
attr(java.lang.String attributeKey,
java.lang.String attributeValue)
Set an attribute value on this element.
|
Attributes |
attributes()
Get all of the element's attributes.
|
java.lang.String |
baseUri()
Get the base URI of this node.
|
Element |
before(Node node)
Insert the specified node into the DOM before this node (as a preceding sibling).
|
Element |
before(java.lang.String html)
Insert the specified HTML into the DOM before this element (as a preceding sibling).
|
Element |
child(int index)
Get a child element of this element, by its 0-based index number.
|
int |
childNodeSize()
Get the number of child nodes that this node holds.
|
Elements |
children()
Get this element's child elements.
|
java.lang.String |
className()
Gets the literal value of this element's "class" attribute, which may include multiple class names, space
separated.
|
java.util.Set<java.lang.String> |
classNames()
Get all of the element's class names.
|
Element |
classNames(java.util.Set<java.lang.String> classNames)
Set the element's
class attribute to the supplied class names. |
Element |
clone()
Create a stand-alone, deep copy of this node, and all of its children.
|
java.lang.String |
cssSelector()
Get a CSS selector that will uniquely select this element.
|
java.lang.String |
data()
Get the combined data of this element.
|
java.util.List<DataNode> |
dataNodes()
Get this element's child data nodes.
|
java.util.Map<java.lang.String,java.lang.String> |
dataset()
Get this element's HTML5 custom data attributes.
|
protected Element |
doClone(Node parent) |
protected void |
doSetBaseUri(java.lang.String baseUri)
Set the baseUri for just this node (not its descendants), if this Node tracks base URIs.
|
int |
elementSiblingIndex()
Get the list index of this element in its element sibling list.
|
Element |
empty()
Remove all of the element's child nodes.
|
protected java.util.List<Node> |
ensureChildNodes() |
Element |
firstElementSibling()
Gets the first element sibling of this element.
|
Elements |
getAllElements()
Find all elements under this element (including self, and children of children).
|
Element |
getElementById(java.lang.String id)
Find an element by ID, including or under this element.
|
Elements |
getElementsByAttribute(java.lang.String key)
Find elements that have a named attribute set.
|
Elements |
getElementsByAttributeStarting(java.lang.String keyPrefix)
Find elements that have an attribute name starting with the supplied prefix.
|
Elements |
getElementsByAttributeValue(java.lang.String key,
java.lang.String value)
Find elements that have an attribute with the specific value.
|
Elements |
getElementsByAttributeValueContaining(java.lang.String key,
java.lang.String match)
Find elements that have attributes whose value contains the match string.
|
Elements |
getElementsByAttributeValueEnding(java.lang.String key,
java.lang.String valueSuffix)
Find elements that have attributes that end with the value suffix.
|
Elements |
getElementsByAttributeValueMatching(java.lang.String key,
java.util.regex.Pattern pattern)
Find elements that have attributes whose values match the supplied regular expression.
|
Elements |
getElementsByAttributeValueMatching(java.lang.String key,
java.lang.String regex)
Find elements that have attributes whose values match the supplied regular expression.
|
Elements |
getElementsByAttributeValueNot(java.lang.String key,
java.lang.String value)
Find elements that either do not have this attribute, or have it with a different value.
|
Elements |
getElementsByAttributeValueStarting(java.lang.String key,
java.lang.String valuePrefix)
Find elements that have attributes that start with the value prefix.
|
Elements |
getElementsByClass(java.lang.String className)
Find elements that have this class, including or under this element.
|
Elements |
getElementsByIndexEquals(int index)
Find elements whose sibling index is equal to the supplied index.
|
Elements |
getElementsByIndexGreaterThan(int index)
Find elements whose sibling index is greater than the supplied index.
|
Elements |
getElementsByIndexLessThan(int index)
Find elements whose sibling index is less than the supplied index.
|
Elements |
getElementsByTag(java.lang.String tagName)
Finds elements, including and recursively under this element, with the specified tag name.
|
Elements |
getElementsContainingOwnText(java.lang.String searchText)
Find elements that directly contain the specified string.
|
Elements |
getElementsContainingText(java.lang.String searchText)
Find elements that contain the specified string.
|
Elements |
getElementsMatchingOwnText(java.util.regex.Pattern pattern)
Find elements whose own text matches the supplied regular expression.
|
Elements |
getElementsMatchingOwnText(java.lang.String regex)
Find elements whose text matches the supplied regular expression.
|
Elements |
getElementsMatchingText(java.util.regex.Pattern pattern)
Find elements whose text matches the supplied regular expression.
|
Elements |
getElementsMatchingText(java.lang.String regex)
Find elements whose text matches the supplied regular expression.
|
protected boolean |
hasAttributes()
Check if this Node has an actual Attributes object.
|
boolean |
hasClass(java.lang.String className)
Tests if this element has a class.
|
boolean |
hasText()
Test if this element has any text content (that is not just whitespace).
|
java.lang.String |
html()
Retrieves the element's inner HTML.
|
Element |
html(java.lang.String html)
Set this element's inner HTML.
|
<T extends java.lang.Appendable> |
html(T appendable)
Write this node and its children to the given
Appendable . |
java.lang.String |
id()
Get the
id attribute of this element. |
Element |
insertChildren(int index,
java.util.Collection<? extends Node> children)
Inserts the given child nodes into this element at the specified index.
|
Element |
insertChildren(int index,
Node... children)
Inserts the given child nodes into this element at the specified index.
|
boolean |
is(Evaluator evaluator)
Check if this element matches the given evaluator.
|
boolean |
is(java.lang.String cssQuery)
Check if this element matches the given
Selector CSS query. |
boolean |
isBlock()
Test if this element is a block-level element.
|
Element |
lastElementSibling()
Gets the last element sibling of this element
|
Element |
nextElementSibling()
Gets the next sibling element of this element.
|
java.lang.String |
nodeName()
Get the node name of this node.
|
java.lang.String |
ownText()
Gets the text owned by this element only; does not get the combined text of all children.
|
Element |
parent()
Gets this node's parent node.
|
Elements |
parents()
Get this element's parent and ancestors, up to the document root.
|
Element |
prepend(java.lang.String html)
Add inner HTML into this element.
|
Element |
prependChild(Node child)
Add a node to the start of this element's children.
|
Element |
prependElement(java.lang.String tagName)
Create a new element by tag name, and add it as the first child.
|
Element |
prependText(java.lang.String text)
Create and prepend a new TextNode to this element.
|
Element |
previousElementSibling()
Gets the previous element sibling of this element.
|
Element |
removeClass(java.lang.String className)
Remove a class name from this element's
class attribute. |
Elements |
select(java.lang.String cssQuery)
Find elements that match the
Selector CSS query, with this element as the starting context. |
Element |
selectFirst(java.lang.String cssQuery)
Find the first ReactElement that matches the
Selector CSS query, with this element as the starting context. |
Element |
shallowClone()
Create a stand-alone, shallow copy of this node.
|
Elements |
siblingElements()
Get sibling elements.
|
Tag |
tag()
Get the Tag for this element.
|
java.lang.String |
tagName()
Get the name of the tag for this element.
|
Element |
tagName(java.lang.String tagName)
Change the tag of this element.
|
java.lang.String |
text()
Gets the combined text of this element and all its children.
|
Element |
text(java.lang.String text)
Set the text of this element.
|
java.util.List<TextNode> |
textNodes()
Get this element's child text nodes.
|
Element |
toggleClass(java.lang.String className)
Toggle a class name on this element's
class attribute: if present, remove it; otherwise add it. |
java.lang.String |
toString() |
java.lang.String |
val()
Get the value of a form element (input, textarea, etc).
|
Element |
val(java.lang.String value)
Set the value of a form element (input, textarea, etc).
|
java.lang.String |
wholeText()
Get the (unencoded) text of all children of this element, including any newlines and spaces present in the
original.
|
Element |
wrap(java.lang.String html)
Wrap the supplied HTML around this element.
|
absUrl, addChildren, addChildren, attr, childNode, childNodes, childNodesAsArray, childNodesCopy, clearAttributes, equals, filter, hasAttr, hasParent, hasSameValue, indent, nextSibling, outerHtml, outerHtml, ownerDocument, parentNode, previousSibling, remove, removeAttr, removeChild, reparentChild, replaceChild, replaceWith, root, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes, traverse, unwrap
public Element(java.lang.String tag)
tag
- tag namepublic Element(Tag tag, java.lang.String baseUri, Attributes attributes)
tag
- tag of this elementbaseUri
- the base URIattributes
- initial attributesappendChild(Node)
,
appendElement(String)
public Element(Tag tag, java.lang.String baseUri)
tag
- element tagbaseUri
- the base URI of this element. It is acceptable for the base URI to be an empty
string, but not null.Tag.valueOf(String, ParseSettings)
protected java.util.List<Node> ensureChildNodes()
ensureChildNodes
in class Node
protected boolean hasAttributes()
Node
hasAttributes
in class Node
public Attributes attributes()
Node
attributes
in class Node
public java.lang.String baseUri()
Node
protected void doSetBaseUri(java.lang.String baseUri)
Node
doSetBaseUri
in class Node
baseUri
- new URIpublic int childNodeSize()
Node
childNodeSize
in class Node
public java.lang.String nodeName()
Node
public java.lang.String tagName()
div
public Element tagName(java.lang.String tagName)
<span>
to a <div>
with
el.tagName("div");
.tagName
- new tag name for this elementpublic Tag tag()
public boolean isBlock()
<div> == true
or an inline element
<p> == false
).public java.lang.String id()
id
attribute of this element.public Element attr(java.lang.String attributeKey, java.lang.String attributeValue)
public Element attr(java.lang.String attributeKey, boolean attributeValue)
true
sets the attribute value to "" and
marks the attribute as boolean so no value is written out. Setting to false
removes the attribute
with the same key if it exists.attributeKey
- the attribute keyattributeValue
- the attribute valuepublic java.util.Map<java.lang.String,java.lang.String> dataset()
E.g., the element <div data-package="jsoup" data-language="Java" class="group">...
has the dataset
package=jsoup, language=java
.
This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected in the other map.
You can find elements that have data attributes using the [^data-]
attribute key prefix selector.
key=value
custom data attributes.public final Element parent()
Node
public Elements parents()
public Element child(int index)
Note that an element can have both mixed Nodes and Elements as children. This method inspects a filtered list of children that are elements, and the index is based on that filtered list.
index
- the index number of the element to retrieveIndexOutOfBoundsException
Node.childNode(int)
public Elements children()
This is effectively a filter on Node.childNodes()
to get ReactElement nodes.
Node.childNodes()
public java.util.List<TextNode> textNodes()
This is effectively a filter on Node.childNodes()
to get Text nodes.
<p>One <span>Two</span> Three <br> Four</p>
with the p
element selected:
p.text()
= "One Two Three Four"
p.ownText()
= "One Three Four"
p.children()
= Elements[<span>, <br>]
p.childNodes()
= List<Node>["One ", <span>, " Three ", <br>, " Four"]
p.textNodes()
= List<TextNode>["One ", " Three ", " Four"]
public java.util.List<DataNode> dataNodes()
This is effectively a filter on Node.childNodes()
to get Data nodes.
data()
public Elements select(java.lang.String cssQuery)
Selector
CSS query, with this element as the starting context. Matched elements
may include this element, or any of its children.
This method is generally more powerful to use than the DOM-type getElementBy*
methods, because
multiple filters can be combined, e.g.:
el.select("a[href]")
- finds links (a
tags with href
attributes)
el.select("a[href*=example.com]")
- finds links pointing to example.com (loosely)
See the query syntax documentation in Selector
.
public Element selectFirst(java.lang.String cssQuery)
Selector
CSS query, with this element as the starting context.
This is effectively the same as calling element.select(query).first()
, but is more efficient as query
execution stops on the first hit.
cssQuery
- cssQuery a Selector
CSS-like querynull
if there is no match.public boolean is(java.lang.String cssQuery)
Selector
CSS query.cssQuery
- a Selector
CSS querypublic boolean is(Evaluator evaluator)
evaluator
- an element evaluatorpublic Element appendChild(Node child)
child
- node to add.public Element appendTo(Element parent)
parent
- element to which this element will be appendedpublic Element prependChild(Node child)
child
- node to add.public Element insertChildren(int index, java.util.Collection<? extends Node> children)
index
- 0-based index to insert children at. Specify 0
to insert at the start, -1
at the
endchildren
- child nodes to insertpublic Element insertChildren(int index, Node... children)
index
- 0-based index to insert children at. Specify 0
to insert at the start, -1
at the
endchildren
- child nodes to insertpublic Element appendElement(java.lang.String tagName)
tagName
- the name of the tag (e.g. div
).parent.appendElement("h1").attr("id", "header").text("Welcome");
public Element prependElement(java.lang.String tagName)
tagName
- the name of the tag (e.g. div
).parent.prependElement("h1").attr("id", "header").text("Welcome");
public Element appendText(java.lang.String text)
text
- the unencoded text to addpublic Element prependText(java.lang.String text)
text
- the unencoded text to addpublic Element append(java.lang.String html)
html
- HTML to add inside this element, after the existing HTMLhtml(String)
public Element prepend(java.lang.String html)
html
- HTML to add inside this element, before the existing HTMLhtml(String)
public Element before(java.lang.String html)
before
in class Node
html
- HTML to add before this elementafter(String)
public Element before(Node node)
before
in class Node
node
- to add before this elementafter(Node)
public Element after(java.lang.String html)
after
in class Node
html
- HTML to add after this elementbefore(String)
public Element after(Node node)
after
in class Node
node
- to add after this elementbefore(Node)
public Element empty()
public Element wrap(java.lang.String html)
public java.lang.String cssSelector()
If the element has an ID, returns #id; otherwise returns the parent (if any) CSS selector, followed by '>', followed by a unique selector for the element (tag.class.class:nth-child(n)).
public Elements siblingElements()
public Element nextElementSibling()
div
contains two p
s,
the nextElementSibling
of the first p
is the second p
.
This is similar to Node.nextSibling()
, but specifically finds only Elements
previousElementSibling()
public Element previousElementSibling()
nextElementSibling()
public Element firstElementSibling()
public int elementSiblingIndex()
public Element lastElementSibling()
public Elements getElementsByTag(java.lang.String tagName)
tagName
- The tag name to search for (case insensitively).public Element getElementById(java.lang.String id)
Note that this finds the first matching ID, starting with this element. If you search down from a different
starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
use getElementById(String)
id
- The ID to search for.public Elements getElementsByClass(java.lang.String className)
Elements can have multiple classes (e.g. <div class="header round first">
. This method
checks each class, so you can find the above with el.getElementsByClass("header");
.
className
- the name of the class to search for.hasClass(String)
,
classNames()
public Elements getElementsByAttribute(java.lang.String key)
key
- name of the attribute, e.g. href
public Elements getElementsByAttributeStarting(java.lang.String keyPrefix)
data-
to find elements
that have HTML5 datasets.keyPrefix
- name prefix of the attribute e.g. data-
public Elements getElementsByAttributeValue(java.lang.String key, java.lang.String value)
key
- name of the attributevalue
- value of the attributepublic Elements getElementsByAttributeValueNot(java.lang.String key, java.lang.String value)
key
- name of the attributevalue
- value of the attributepublic Elements getElementsByAttributeValueStarting(java.lang.String key, java.lang.String valuePrefix)
key
- name of the attributevaluePrefix
- start of attribute valuepublic Elements getElementsByAttributeValueEnding(java.lang.String key, java.lang.String valueSuffix)
key
- name of the attributevalueSuffix
- end of the attribute valuepublic Elements getElementsByAttributeValueContaining(java.lang.String key, java.lang.String match)
key
- name of the attributematch
- substring of value to search forpublic Elements getElementsByAttributeValueMatching(java.lang.String key, java.util.regex.Pattern pattern)
key
- name of the attributepattern
- compiled regular expression to match against attribute valuespublic Elements getElementsByAttributeValueMatching(java.lang.String key, java.lang.String regex)
key
- name of the attributeregex
- regular expression to match against attribute values. You can use embedded flags (such as (?i) and (?m) to control regex options.public Elements getElementsByIndexLessThan(int index)
index
- 0-based indexpublic Elements getElementsByIndexGreaterThan(int index)
index
- 0-based indexpublic Elements getElementsByIndexEquals(int index)
index
- 0-based indexpublic Elements getElementsContainingText(java.lang.String searchText)
searchText
- to look for in the element's texttext()
public Elements getElementsContainingOwnText(java.lang.String searchText)
searchText
- to look for in the element's own textownText()
public Elements getElementsMatchingText(java.util.regex.Pattern pattern)
pattern
- regular expression to match text againsttext()
public Elements getElementsMatchingText(java.lang.String regex)
regex
- regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options.text()
public Elements getElementsMatchingOwnText(java.util.regex.Pattern pattern)
pattern
- regular expression to match text againstownText()
public Elements getElementsMatchingOwnText(java.lang.String regex)
regex
- regular expression to match text against. You can use embedded flags (such as (?i) and (?m) to control regex options.ownText()
public Elements getAllElements()
public java.lang.String text()
For example, given HTML <p>Hello <b>there</b> now! </p>
, p.text()
returns "Hello there now!"
if you don't want the text to be normalized.
,
ownText()
,
textNodes()
public java.lang.String wholeText()
text()
public java.lang.String ownText()
For example, given HTML <p>Hello <b>there</b> now!</p>
, p.ownText()
returns "Hello now!"
,
whereas p.text()
returns "Hello there now!"
.
Note that the text within the b
element is not returned, as it is not a direct child of the p
element.
text()
,
textNodes()
public Element text(java.lang.String text)
text
- unencoded textpublic boolean hasText()
public java.lang.String data()
script
tag. Note that data is NOT the
text of the element. Use text()
to get the text that would be visible to a user, and data()
for the contents of scripts, comments, CSS styles, etc.dataNodes()
public java.lang.String className()
<div class="header gray">
returns, "header gray
")public java.util.Set<java.lang.String> classNames()
<div class="header gray">
,
returns a set of two elements "header", "gray"
. Note that modifications to this set are not pushed to
the backing class
attribute; use the classNames(java.util.Set)
method to persist them.public Element classNames(java.util.Set<java.lang.String> classNames)
class
attribute to the supplied class names.classNames
- set of classespublic boolean hasClass(java.lang.String className)
className
- name of class to check forpublic Element addClass(java.lang.String className)
class
attribute.className
- class name to addpublic Element removeClass(java.lang.String className)
class
attribute.className
- class name to removepublic Element toggleClass(java.lang.String className)
class
attribute: if present, remove it; otherwise add it.className
- class name to togglepublic java.lang.String val()
public Element val(java.lang.String value)
value
- value to setpublic java.lang.String html()
<div>
with one empty <p>
, would return
<p></p>
. (Whereas Node.outerHtml()
would return <div><p></p></div>
.)Node.outerHtml()
public <T extends java.lang.Appendable> T html(T appendable)
Appendable
.public Element html(java.lang.String html)
html
- HTML to parse and set into this elementappend(String)
public Element clone()
Node
The cloned node may be adopted into another Document or node structure using appendChild(org.jsoup.nodes.Node)
.
clone
in class Node
Node.shallowClone()
public Element shallowClone()
Node
shallowClone
in class Node
Node.clone()