Class TextNode

java.lang.Object
org.jsoup.nodes.Node
org.jsoup.nodes.TextNode
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
CDataNode

public class TextNode
extends Node
A text node.
Author:
Jonathan Hedley, jonathan@hedley.net
  • Constructor Details

  • Method Details

    • nodeName

      public String nodeName()
      Description copied from class: Node
      Get the node name of this node. Use for debugging purposes and not logic switching (for that, use instanceof).
      Specified by:
      nodeName in class Node
      Returns:
      node name
    • text

      public String text()
      Get the text content of this text node.
      Returns:
      Unencoded, normalised text.
      See Also:
      getWholeText()
    • text

      public TextNode text​(String text)
      Set the text content of this text node.
      Parameters:
      text - unencoded text
      Returns:
      this, for chaining
    • getWholeText

      public String getWholeText()
      Get the (unencoded) text of this text node, including any newlines and spaces present in the original.
      Returns:
      text
    • isBlank

      public boolean isBlank()
      Test if this text node is blank -- that is, empty or only whitespace (including newlines).
      Returns:
      true if this document is empty or only whitespace, false if it contains any text content.
    • splitText

      public TextNode splitText​(int offset)
      Split this text node into two nodes at the specified string offset. After splitting, this node will contain the original text up to the offset, and will have a new text node sibling containing the text after the offset.
      Parameters:
      offset - string offset point to split node at.
      Returns:
      the newly created text node containing the text after the offset.
    • toString

      public String toString()
      Description copied from class: Node
      Gets this node's outer HTML.
      Overrides:
      toString in class Node
      Returns:
      outer HTML.
      See Also:
      Node.outerHtml()
    • clone

      public TextNode clone()
      Description copied from class: Node
      Create a stand-alone, deep copy of this node, and all of its children. The cloned node will have no siblings or parent node. As a stand-alone object, any changes made to the clone or any of its children will not impact the original node.

      The cloned node may be adopted into another Document or node structure using Element.appendChild(Node).

      Overrides:
      clone in class Node
      Returns:
      a stand-alone cloned node, including clones of any children
      See Also:
      Node.shallowClone()
    • createFromEncoded

      public static TextNode createFromEncoded​(String encodedText)
      Create a new TextNode from HTML encoded (aka escaped) data.
      Parameters:
      encodedText - Text containing encoded HTML (e.g. <)
      Returns:
      TextNode containing unencoded data (e.g. <)
    • hasAttributes

      protected final boolean hasAttributes()
      Description copied from class: Node
      Check if this Node has an actual Attributes object.
      Specified by:
      hasAttributes in class Node
    • attributes

      public final Attributes attributes()
      Description copied from class: Node
      Get all of the element's attributes.
      Specified by:
      attributes in class Node
      Returns:
      attributes (which implements iterable, in same order as presented in original HTML).
    • attr

      public String attr​(String key)
      Description copied from class: Node
      Get an attribute's value by its key. Case insensitive

      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.

      E.g.:
      String url = a.attr("abs:href");
      Overrides:
      attr in class Node
      Parameters:
      key - The attribute key.
      Returns:
      The attribute, or empty string if not present (to avoid nulls).
      See Also:
      Node.attributes(), Node.hasAttr(String), Node.absUrl(String)
    • attr

      public Node attr​(String key, String value)
      Description copied from class: Node
      Set an attribute (key=value). If the attribute already exists, it is replaced. The attribute key comparison is case insensitive. The key will be set with case sensitivity as set in the parser settings.
      Overrides:
      attr in class Node
      Parameters:
      key - The attribute key.
      value - The attribute value.
      Returns:
      this (for chaining)
    • hasAttr

      public boolean hasAttr​(String key)
      Description copied from class: Node
      Test if this Node has an attribute. Case insensitive.
      Overrides:
      hasAttr in class Node
      Parameters:
      key - The attribute key to check.
      Returns:
      true if the attribute exists, false if not.
    • removeAttr

      public Node removeAttr​(String key)
      Description copied from class: Node
      Remove an attribute from this node.
      Overrides:
      removeAttr in class Node
      Parameters:
      key - The attribute to remove.
      Returns:
      this (for chaining)
    • absUrl

      public String absUrl​(String key)
      Description copied from class: Node
      Get an absolute URL from a URL attribute that may be relative (such as an <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");

      Overrides:
      absUrl in class Node
      Parameters:
      key - The attribute key
      Returns:
      An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or could not be made successfully into a URL.
      See Also:
      Node.attr(java.lang.String), URL(java.net.URL, String)
    • baseUri

      public String baseUri()
      Description copied from class: Node
      Get the base URI that applies to this node. Will return an empty string if not defined. Used to make relative links absolute.
      Specified by:
      baseUri in class Node
      Returns:
      base URI
      See Also:
      Node.absUrl(java.lang.String)
    • doSetBaseUri

      protected void doSetBaseUri​(String baseUri)
      Description copied from class: Node
      Set the baseUri for just this node (not its descendants), if this Node tracks base URIs.
      Specified by:
      doSetBaseUri in class Node
      Parameters:
      baseUri - new URI
    • childNodeSize

      public int childNodeSize()
      Description copied from class: Node
      Get the number of child nodes that this node holds.
      Specified by:
      childNodeSize in class Node
      Returns:
      the number of child nodes that this node holds.
    • empty

      public Node empty()
      Description copied from class: Node
      Delete all this node's children.
      Specified by:
      empty in class Node
      Returns:
      this node, for chaining
    • ensureChildNodes

      protected List<Node> ensureChildNodes()
      Specified by:
      ensureChildNodes in class Node
    • doClone

      protected org.jsoup.nodes.LeafNode doClone​(Node parent)
      Overrides:
      doClone in class Node