Пример #1
0
  /**
   * Get the combined data of this element. Data is e.g. the inside of a {@code script} tag.
   *
   * @return the data, or empty string if none
   * @see #dataNodes()
   */
  public String data() {
    StringBuilder sb = new StringBuilder();

    for (Node childNode : childNodes) {
      if (childNode instanceof DataNode) {
        DataNode data = (DataNode) childNode;
        sb.append(data.getWholeData());
      } else if (childNode instanceof Element) {
        Element element = (Element) childNode;
        String elementData = element.data();
        sb.append(elementData);
      }
    }
    return sb.toString();
  }
Пример #2
0
  /**
   * Gets the combined text of this element and all its children.
   *
   * <p>For example, given HTML {@code <p>Hello <b>there</b> now!</p>}, {@code p.text()} returns
   * {@code "Hello there now!"}
   *
   * @return unencoded text, or empty string if none.
   * @see #ownText()
   * @see #textNodes()
   */
  public String text() {
    final StringBuilder accum = new StringBuilder();
    new NodeTraversor(
            new NodeVisitor() {
              public void head(Node node, int depth) {
                if (node instanceof TextNode) {
                  TextNode textNode = (TextNode) node;
                  appendNormalisedText(accum, textNode);
                } else if (node instanceof Element) {
                  Element element = (Element) node;
                  if (accum.length() > 0
                      && (element.isBlock() || element.tag.getName().equals("br"))
                      && !TextNode.lastCharIsWhitespace(accum)) accum.append(" ");
                }
              }

              public void tail(Node node, int depth) {}
            })
        .traverse(this);
    return accum.toString().trim();
  }
Пример #3
0
 /**
  * Gets the text owned by this element only; does not get the combined text of all children.
  *
  * <p>For example, given HTML {@code <p>Hello <b>there</b> now!</p>}, {@code p.ownText()} returns
  * {@code "Hello now!"}, whereas {@code p.text()} returns {@code "Hello there now!"}. Note that
  * the text within the {@code b} element is not returned, as it is not a direct child of the
  * {@code p} element.
  *
  * @return unencoded text, or empty string if none.
  * @see #text()
  * @see #textNodes()
  */
 public String ownText() {
   StringBuilder sb = new StringBuilder();
   ownText(sb);
   return sb.toString().trim();
 }
Пример #4
0
 /**
  * Retrieves the element's inner HTML. E.g. on a {@code <div>} with one empty {@code <p>}, would
  * return {@code <p></p>}. (Whereas {@link #outerHtml()} would return {@code <div><p></p></div>}.)
  *
  * @return String of HTML.
  * @see #outerHtml()
  */
 public String html() {
   StringBuilder accum = new StringBuilder();
   html(accum);
   return accum.toString().trim();
 }