public class TextNode extends Node
Modifier and Type | Field and Description |
---|---|
(package private) java.lang.String |
text |
private static java.lang.String |
TEXT_KEY |
attributes, baseUri, childNodes, parentNode, siblingIndex
Constructor and Description |
---|
TextNode(java.lang.String text,
java.lang.String baseUri)
Create a new TextNode representing the supplied (unencoded) text).
|
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 (i.e.
|
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).
|
Attributes |
attributes()
Get all of the element's attributes.
|
static TextNode |
createFromEncoded(java.lang.String encodedText,
java.lang.String baseUri)
Create a new TextNode from HTML encoded (aka escaped) data.
|
private void |
ensureAttributes() |
java.lang.String |
getWholeText()
Get the (unencoded) text of this text node, including any newlines and spaces present in the original.
|
boolean |
hasAttr(java.lang.String attributeKey)
Test if this element has an attribute.
|
boolean |
isBlank()
Test if this text node is blank -- that is, empty or only whitespace (including newlines).
|
(package private) static boolean |
lastCharIsWhitespace(java.lang.StringBuilder sb) |
java.lang.String |
nodeName()
Get the node name of this node.
|
(package private) static java.lang.String |
normaliseWhitespace(java.lang.String text) |
(package private) void |
outerHtmlHead(java.lang.Appendable accum,
int depth,
Document.OutputSettings out)
Get the outer HTML of this node.
|
(package private) void |
outerHtmlTail(java.lang.Appendable accum,
int depth,
Document.OutputSettings out) |
Node |
removeAttr(java.lang.String attributeKey)
Remove an attribute from this element.
|
TextNode |
splitText(int offset)
Split this text node into two nodes at the specified string offset.
|
(package private) static java.lang.String |
stripLeadingWhitespace(java.lang.String text) |
java.lang.String |
text()
Get the text content of this text node.
|
TextNode |
text(java.lang.String text)
Set the text content of this text node.
|
java.lang.String |
toString() |
addChildren, addChildren, after, after, baseUri, before, before, childNode, childNodes, childNodesAsArray, childNodesCopy, childNodeSize, clearAttributes, clone, doClone, ensureChildNodes, equals, getOutputSettings, hasSameValue, html, indent, nextSibling, nodelistChanged, outerHtml, outerHtml, ownerDocument, parent, parentNode, previousSibling, remove, removeChild, reparentChild, replaceChild, replaceWith, root, setBaseUri, setParentNode, setSiblingIndex, siblingIndex, siblingNodes, traverse, unwrap, wrap
private static final java.lang.String TEXT_KEY
java.lang.String text
public TextNode(java.lang.String text, java.lang.String baseUri)
text
- raw textbaseUri
- base uricreateFromEncoded(String, String)
public java.lang.String nodeName()
Node
public java.lang.String text()
getWholeText()
public TextNode text(java.lang.String text)
text
- unencoded textpublic java.lang.String getWholeText()
public boolean isBlank()
public TextNode splitText(int offset)
offset
- string offset point to split node at.void outerHtmlHead(java.lang.Appendable accum, int depth, Document.OutputSettings out) throws java.io.IOException
Node
outerHtmlHead
in class Node
accum
- accumulator to place HTML intojava.io.IOException
- if appending to the given accumulator fails.void outerHtmlTail(java.lang.Appendable accum, int depth, Document.OutputSettings out)
outerHtmlTail
in class Node
public static TextNode createFromEncoded(java.lang.String encodedText, java.lang.String baseUri)
encodedText
- Text containing encoded HTML (e.g. <)baseUri
- Base uristatic java.lang.String normaliseWhitespace(java.lang.String text)
static java.lang.String stripLeadingWhitespace(java.lang.String text)
static boolean lastCharIsWhitespace(java.lang.StringBuilder sb)
private void ensureAttributes()
public java.lang.String attr(java.lang.String attributeKey)
Node
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 Node.absUrl(java.lang.String)
method.
String url = a.attr("abs:href");
attr
in class Node
attributeKey
- The attribute key.Node.attributes()
,
Node.hasAttr(String)
,
Node.absUrl(String)
public Attributes attributes()
Node
attributes
in class Node
public Node attr(java.lang.String attributeKey, java.lang.String attributeValue)
Node
public boolean hasAttr(java.lang.String attributeKey)
Node
public Node removeAttr(java.lang.String attributeKey)
Node
removeAttr
in class Node
attributeKey
- The attribute to remove.public java.lang.String absUrl(java.lang.String attributeKey)
Node
<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 Node.baseUri
, and made
absolute using that.
As an alternate, you can use the Node.attr(java.lang.String)
method with the abs:
prefix, e.g.:
String absUrl = linkEl.attr("abs:href");
absUrl
in class Node
attributeKey
- The attribute keyNode.attr(java.lang.String)
,
URL.URL(java.net.URL, String)