Exemplo n.º 1
0
 /**
  * Create and prepend a new TextNode to this element.
  *
  * @param text the unencoded text to add
  * @return this element
  */
 public Element prependText(String text) {
   TextNode node = new TextNode(text, baseUri());
   prependChild(node);
   return this;
 }
 private Element extend(Element sup, Element sub) {
   // Get the child elements for both the sup (super) element and the sub
   // (extended) element.
   Elements subElements = sub.children();
   Elements supElements = sup.children().clone();
   // For each element in the sub group,
   loop:
   for (Element e : subElements) {
     // If it's overridden, delete it from sup.
     if (e.hasAttr(Language.OVERRIDE_ATTRIBUTE)) {
       for (Element el : supElements) {
         if (el.attr(Language.IDENTIFICATION_ATTRIBUTE)
             .equals(e.attr(Language.IDENTIFICATION_ATTRIBUTE))) {
           supElements.remove(el);
           continue loop;
         }
       }
       // Fail silently if no element is found to override.
       continue loop;
     } else if (Language.isOverridden(e)) {
       // Some elements are automatically overridden if they exist.
       for (Element el : supElements) {
         if (el.tagName().equals(e.tagName())) {
           supElements.remove(el);
           continue loop;
         }
       }
       // Fail silently if no element is found to override.
       continue loop;
     } else if (e.tagName().equals("meta")) { // If this is a meta tag,
       if (e.hasAttr("name")) { // If it's got a name,
         Elements metaThatMatch =
             supElements.select(
                 "meta[name=\""
                     + e.attr("name")
                     + "\"]"); // Find and override the meta tag in supElements with that name.
         if (metaThatMatch.size() == 1) {
           supElements.remove(supElements.indexOf(metaThatMatch.first()));
         }
       } else if (e.hasAttr("http-equiv")) { // If it's got a http-equiv,
         Elements metaThatMatch =
             supElements.select(
                 "meta[http-equiv=\""
                     + e.attr("http-equiv")
                     + "\"]"); // Find and override the meta tag in supElements with that
                               // http-equiv.
         if (metaThatMatch.size() == 1) {
           supElements.remove(supElements.indexOf(metaThatMatch.first()));
         }
       }
     } else {
       // If it's not overridden but does correspond to an element,
       // recursively extend it.
       for (Element el : supElements) {
         if (el.hasAttr(Language.IDENTIFICATION_ATTRIBUTE)
             && el.attr(Language.IDENTIFICATION_ATTRIBUTE)
                 .equals(e.attr(Language.IDENTIFICATION_ATTRIBUTE))) {
           Element temp = extend(el.clone(), e.clone()).clone();
           e.replaceWith(temp);
           supElements.remove(el);
           continue loop;
         }
       }
     }
   }
   // Add the elements from the sup to the beginning of sub. This is where
   // the real extension happens.
   Collections.reverse(supElements);
   for (Element e : supElements) {
     sub.prependChild(e.clone());
   }
   return sub;
 }
Exemplo n.º 3
0
 /**
  * Create a new element by tag name, and add it as the first child.
  *
  * @param tagName the name of the tag (e.g. {@code div}).
  * @return the new element, to allow you to add content to it, e.g.: {@code
  *     parent.prependElement("h1").attr("id", "header").text("Welcome");}
  */
 public Element prependElement(String tagName) {
   Element child = new Element(Tag.valueOf(tagName), baseUri());
   prependChild(child);
   return child;
 }