Пример #1
0
  void outerHtmlHead(StringBuilder accum, int depth, Document.OutputSettings out) {
    if (accum.length() > 0
        && out.prettyPrint()
        && (tag.formatAsBlock()
            || (parent() != null && parent().tag().formatAsBlock())
            || out.outline())) indent(accum, depth, out);
    accum.append("<").append(tagName());
    attributes.html(accum, out);

    if (childNodes.isEmpty() && tag.isSelfClosing()) accum.append(" />");
    else accum.append(">");
  }
Пример #2
0
 private String constructDeckNameFilter(String string) {
   final String[] split = string.trim().split(" ");
   string = "";
   for (int length = split.length, i = 0; i < length; ++i) {
     final String s = split[i];
     String s2 = "+";
     final StringBuilder append = new StringBuilder().append(string);
     if (string.isEmpty()) {
       s2 = "";
     }
     string = append.append(s2).append(s).toString();
   }
   return string;
 }
Пример #3
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();
  }
Пример #4
0
  private static void appendNormalisedText(StringBuilder accum, TextNode textNode) {
    String text = textNode.getWholeText();

    if (!preserveWhitespace(textNode.parent())) {
      text = TextNode.normaliseWhitespace(text);
      if (TextNode.lastCharIsWhitespace(accum)) text = TextNode.stripLeadingWhitespace(text);
    }
    accum.append(text);
  }
Пример #5
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();
  }
Пример #6
0
 void outerHtmlTail(StringBuilder accum, int depth, Document.OutputSettings out) {
   if (!(childNodes.isEmpty() && tag.isSelfClosing())) {
     if (out.prettyPrint()
         && (!childNodes.isEmpty()
             && (tag.formatAsBlock()
                 || (out.outline()
                     && (childNodes.size() > 1
                         || (childNodes.size() == 1
                             && !(childNodes.get(0) instanceof TextNode)))))))
       indent(accum, depth, out);
     accum.append("</").append(tagName()).append(">");
   }
 }
Пример #7
0
 private static void appendWhitespaceIfBr(Element element, StringBuilder accum) {
   if (element.tag.getName().equals("br") && !TextNode.lastCharIsWhitespace(accum))
     accum.append(" ");
 }
Пример #8
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();
 }
Пример #9
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();
 }