Ejemplo n.º 1
0
 public static int nearestBlock(Node node) {
   int nearest = 0;
   Node parent = node.parent();
   while (parent != null) {
     ++nearest;
     if (NodeUtil.isProximityBlock(parent.nodeName())) {
       return nearest;
     }
     parent = parent.parent();
   }
   return Integer.MAX_VALUE;
 }
Ejemplo n.º 2
0
 public static boolean isItem(Node node, HtmlNode matchResult, HtmlNode matchParent) {
   if (matchResult != null) {
     return matches(matchResult, node);
   }
   if (matchParent != null) {
     return node.parent() != null && matches(matchParent, node.parent());
   }
   for (int i = 0; i < items.length; i++) {
     if (node.nodeName().equalsIgnoreCase(items[i])) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 3
0
    public void head(Node source, int depth) {
      if (source instanceof Element) {
        Element sourceEl = (Element) source;

        if (whitelist.isSafeTag(sourceEl.tagName())) { // safe, clone and copy safe attrs
          ElementMeta meta = createSafeElement(sourceEl);
          Element destChild = meta.el;
          destination.appendChild(destChild);

          numDiscarded += meta.numAttribsDiscarded;
          destination = destChild;
        } else if (source
            != root) { // not a safe tag, so don't add. don't count root against discarded.
          numDiscarded++;
        }
      } else if (source instanceof TextNode) {
        TextNode sourceText = (TextNode) source;
        TextNode destText = new TextNode(sourceText.getWholeText(), source.baseUri());
        destination.appendChild(destText);
      } else if (source instanceof DataNode && whitelist.isSafeTag(source.parent().nodeName())) {
        DataNode sourceData = (DataNode) source;
        DataNode destData = new DataNode(sourceData.getWholeData(), source.baseUri());
        destination.appendChild(destData);
      } else { // else, we don't care about comments, xml proc instructions, etc
        numDiscarded++;
      }
    }
Ejemplo n.º 4
0
  public List<Node> findSiblings(Node node) {
    Preconditions.checkNotNull(node);

    Node parent = node.parent();
    if (null == parent) return null;

    return parent.childNodes();
  }
Ejemplo n.º 5
0
  static void markVisible(Node node) {
    if (node != null) {
      if (node.nodeName().equals("select")) {
        node.traverse(
            new NodeVisitor() {
              @Override
              public void tail(Node n, int d) {}

              @Override
              public void head(Node n, int d) {
                n.attr("class", hiddenMarker.matcher(n.attr("class")).replaceAll(""));
              }
            });
      }
      node.attr("class", hiddenMarker.matcher(node.attr("class")).replaceAll(""));
      markVisible(node.parent());
    }
  }