Java Code Examples for org.jsoup.nodes.Element#addClass()

The following examples show how to use org.jsoup.nodes.Element#addClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Add the class name to every matched element's {@code class} attribute.
 @param className class name to add
 @return this
 */
public Elements addClass(String className) {
    for (Element element : this) {
        element.addClass(className);
    }
    return this;
}
 
Example 2
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Add the class name to every matched element's {@code class} attribute.
 @param className class name to add
 @return this
 */
public Elements addClass(String className) {
    for (Element element : this) {
        element.addClass(className);
    }
    return this;
}
 
Example 3
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 Add the class name to every matched element's {@code class} attribute.
 @param className class name to add
 @return this
 */
public Elements addClass(String className) {
    for (Element element : this) {
        element.addClass(className);
    }
    return this;
}
 
Example 4
Source File: TruncateHtmlFilter.java    From jinjava with Apache License 2.0 5 votes vote down vote up
@Override
public void tail(Node node, int depth) {
  if (node instanceof Element) {
    Element el = (Element) node;
    if (StringUtils.isBlank(el.text())) {
      el.addClass("__deleteme");
    }
  }
}
 
Example 5
Source File: Elements.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 Add the class name to every matched element's {@code class} attribute.
 @param className class name to add
 @return this
 */
public Elements addClass(String className) {
    for (Element element : contents) {
        element.addClass(className);
    }
    return this;
}
 
Example 6
Source File: NavBuilder.java    From rebuild with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 渲染导航菜單
 *
 * @param item
 * @param activeNav
 * @return
 */
public String renderNavItem(JSONObject item, String activeNav) {
    final boolean isUrlType = "URL".equals(item.getString("type"));
    String navName = item.getString("value");
    String navUrl = item.getString("value");

    boolean isOutUrl = isUrlType && navUrl.startsWith("http");
    if (isUrlType) {
        navName = "nav_url-" + navName.hashCode();
        if (isOutUrl) {
            navUrl = ServerListener.getContextPath() + "/commons/url-safe?url=" + CodecUtils.urlEncode(navUrl);
        } else {
            navUrl = ServerListener.getContextPath() + navUrl;
        }
    } else if (NAV_FEEDS.equals(navName)) {
        navName = "nav_entity-Feeds";
        navUrl = ServerListener.getContextPath() + "/feeds/home";
    } else if (NAV_FILEMRG.equals(navName)) {
        navName = "nav_entity-Attachment";
        navUrl = ServerListener.getContextPath() + "/files/home";
    } else {
        navName = "nav_entity-" + navName;
        navUrl = ServerListener.getContextPath() + "/app/" + navUrl + "/list";
    }

    String navIcon = StringUtils.defaultIfBlank(item.getString("icon"), "texture");
    String navText = item.getString("text");

    JSONArray subNavs = null;
    if (activeNav != null) {
        subNavs = item.getJSONArray("sub");
        if (subNavs == null || subNavs.isEmpty()) {
            subNavs = null;
        }
    }

    StringBuilder navHtml = new StringBuilder()
            .append(String.format("<li class=\"%s\"><a href=\"%s\" target=\"%s\"><i class=\"icon zmdi zmdi-%s\"></i><span>%s</span></a>",
                    navName + (subNavs == null ? StringUtils.EMPTY : " parent"),
                    subNavs == null ? navUrl : "###",
                    isOutUrl ? "_blank" : "_self",
                    navIcon,
                    navText));
    if (subNavs != null) {
        StringBuilder subHtml = new StringBuilder()
                .append("<ul class=\"sub-menu\"><li class=\"title\">")
                .append(navText)
                .append("</li><li class=\"nav-items\"><div class=\"content\"><ul class=\"sub-menu-ul\">");

        for (Object o : subNavs) {
            JSONObject subNav = (JSONObject) o;
            subHtml.append(renderNavItem(subNav, null));
        }
        subHtml.append("</ul></div></li></ul>");
        navHtml.append(subHtml);
    }
    navHtml.append("</li>");

    if (activeNav != null) {
        Document navBody = Jsoup.parseBodyFragment(navHtml.toString());
        for (Element nav : navBody.select("." + activeNav)) {
            nav.addClass("active");
            if (activeNav.startsWith("nav_entity-")) {
                Element navParent = nav.parent();
                if (navParent != null && navParent.hasClass("sub-menu-ul")) {
                    navParent.parent().parent().parent().parent().addClass("open active");
                }
            }
        }
        return navBody.selectFirst("li").outerHtml();
    }
    return navHtml.toString();
}
 
Example 7
Source File: HtmlWriterTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void alternatingRows() throws IOException {
  OutputStream baos = new ByteArrayOutputStream();
  ElementCreator elementCreator =
      (elementName, column, row) -> {
        Element element = new Element(elementName);
        if (elementName.equals("tr") && row != null) {
          return element.addClass(row % 2 == 0 ? "even" : "odd");
        }
        return element;
      };
  table
      .write()
      .usingOptions(HtmlWriteOptions.builder(baos).elementCreator(elementCreator).build());
  String output = baos.toString();
  assertEquals(
      "<table>"
          + LINE_END
          + " <thead>"
          + LINE_END
          + "  <tr>"
          + LINE_END
          + "   <th>v</th>"
          + LINE_END
          + "   <th>v2</th>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + " </thead>"
          + LINE_END
          + " <tbody>"
          + LINE_END
          + "  <tr class=\"even\">"
          + LINE_END
          + "   <td>1</td>"
          + LINE_END
          + "   <td>1</td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + "  <tr class=\"odd\">"
          + LINE_END
          + "   <td>2</td>"
          + LINE_END
          + "   <td>2</td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + "  <tr class=\"even\">"
          + LINE_END
          + "   <td></td>"
          + LINE_END
          + "   <td></td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + " </tbody>"
          + LINE_END
          + "</table>",
      output);
}
 
Example 8
Source File: HtmlWriterTest.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
@Test
public void alternatingRows() throws IOException {
  OutputStream baos = new ByteArrayOutputStream();
  ElementCreator elementCreator =
      (elementName, column, row) -> {
        Element element = new Element(elementName);
        if (elementName.equals("tr") && row != null) {
          return element.addClass(row % 2 == 0 ? "even" : "odd");
        }
        return element;
      };
  table
      .write()
      .usingOptions(HtmlWriteOptions.builder(baos).elementCreator(elementCreator).build());
  String output = baos.toString();
  assertEquals(
      "<table>"
          + LINE_END
          + " <thead>"
          + LINE_END
          + "  <tr>"
          + LINE_END
          + "   <th>v</th>"
          + LINE_END
          + "   <th>v2</th>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + " </thead>"
          + LINE_END
          + " <tbody>"
          + LINE_END
          + "  <tr class=\"even\">"
          + LINE_END
          + "   <td>1</td>"
          + LINE_END
          + "   <td>1</td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + "  <tr class=\"odd\">"
          + LINE_END
          + "   <td>2</td>"
          + LINE_END
          + "   <td>2</td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + "  <tr class=\"even\">"
          + LINE_END
          + "   <td></td>"
          + LINE_END
          + "   <td></td>"
          + LINE_END
          + "  </tr>"
          + LINE_END
          + " </tbody>"
          + LINE_END
          + "</table>",
      output);
}