private static Node toNode(Element aInElement) {
   int i = aInElement.siblingIndex();
   Node lNode = aInElement.parent().childNode(i);
   if (!lNode.nodeName().equals(aInElement.tagName())) {
     throw new RuntimeException(lNode.nodeName() + " != " + aInElement.tagName());
   }
   return lNode;
 }
Exemplo n.º 2
0
 public static boolean isEmpty(Node node, boolean doFilter) {
   return node == null
       || node.nodeName().equals("#comment")
       || node.nodeName().equals("#data")
       || node.nodeName().equals("style")
       || node.nodeName().equals("script")
       || isHidden(node)
       || (doFilter && isFiltered(node))
       || (node.nodeName().equals("#text") && CommonUtil.isEmpty(node.toString(), true));
 }
Exemplo n.º 3
0
  // update doc tree according to open stack emlement
  void updateDoc() {
    Node root = (Node) doc;
    Stack<Node> shadow = new Stack<Node>();

    // get html Element
    Node rightMost = null;
    for (int i = 0; i < root.childNodesAsArray().length; i++) {
      if (root.childNode(i).nodeName().endsWith("html")) {
        rightMost = root.childNode(i);
        break;
      }
    }

    shadow.push(rightMost);
    // get body Element, considering body1
    while (rightMost.childNodesAsArray().length > 0) {
      rightMost = rightMost.childNodesAsArray()[rightMost.childNodesAsArray().length - 1];
      shadow.push(rightMost);
    }
    shadow.push(rightMost);

    // pop out the elements those are supposed to be complete
    while (shadow.size() > stack.size()) shadow.pop();

    while (shadow.size() > 0) {
      Node current = shadow.pop();
      // System.out.println("stack.peekLast().nodeName(): "+stack.peekLast().nodeName());
      // System.out.println("current.nodeName()         : "+current.nodeName());
      if (stack.peekLast().nodeName().equals("body")
          || stack.peekLast().nodeName().equals("head")
          || current.nodeName().equals("body")
          || current.nodeName().equals("head")) break;

      if (current.nodeName().equals(stack.peekLast().nodeName())) {
        stack.pollLast();
        ((Element) current).onlyStartTag = true;
      } else {
        System.out.println(
            Thread.currentThread().getName() + " err: stack doesn't match with shadow stack");
      }
    }

    // FileWriter file = null;
    // try {
    // file = new FileWriter(Thread.currentThread().getName()+"doc.html");
    // } catch (IOException e) {
    // e.printStackTrace();
    // }
    // PrintWriter out = new PrintWriter(file);
    // out.println(doc);
    // out.close();
  }
Exemplo n.º 4
0
  private void checkFormFields(PageCompilingContext pc, Node element) {
    if (null == pc.form) return;

    String action = pc.form.attr("action");

    // Only look at contextual uris (i.e. hosted by us).
    // TODO - relative, not starting with '/'
    if (null == action || (!action.startsWith("/"))) return;

    final PageBook.Page page = pageBook.get(action);

    // Only look at pages we actually have registered.
    if (null == page) {
      pc.warnings.add(
          CompileError.in(element.outerHtml())
              .near(line(element))
              .causedBy(CompileErrors.UNRESOLVABLE_FORM_ACTION));

      return;
    }

    // If we're inside a form do a throw-away compile against the target page.
    if ("input".equals(element.nodeName()) || "textarea".equals(element.nodeName())) {
      String name = element.attr("name");

      // Skip submits and buttons.
      if (skippable(element.attr("type"))) return;

      // TODO Skip empty?
      if (null == name) {
        pc.warnings.add(
            CompileError.in(element.outerHtml())
                .near(line(element))
                .causedBy(CompileErrors.FORM_MISSING_NAME));

        return;
      }

      // Compile expression path.
      try {
        new MvelEvaluatorCompiler(page.pageClass()).compile(name);

      } catch (ExpressionCompileException e) {
        // TODO Very hacky, needed to strip out xmlns attribution.
        pc.warnings.add(
            CompileError.in(element.outerHtml())
                .near(element.siblingIndex()) // TODO - line number
                .causedBy(CompileErrors.UNRESOLVABLE_FORM_BINDING, e));
      }
    }
  }
Exemplo n.º 5
0
  @Test
  public void designIsSerializedWithCorrectPrefixesAndPackageNames() throws IOException {
    ByteArrayOutputStream out = serializeDesign(ctx);

    // Check the mapping from prefixes to package names using the html tree
    String[] expectedPrefixes = {"my"};
    String[] expectedPackageNames = {"com.addon.mypackage"};
    int index = 0;

    Document doc = Jsoup.parse(out.toString("UTF-8"));
    Element head = doc.head();
    for (Node child : head.childNodes()) {
      if ("meta".equals(child.nodeName())) {
        String name = child.attributes().get("name");
        if ("package-mapping".equals(name)) {
          String content = child.attributes().get("content");
          String[] parts = content.split(":");
          assertEquals("Unexpected prefix.", expectedPrefixes[index], parts[0]);
          assertEquals("Unexpected package name.", expectedPackageNames[index], parts[1]);
          index++;
        }
      }
    }
    assertEquals("Unexpected number of prefix - package name pairs.", 1, index);
  }
Exemplo n.º 6
0
 // hit when the node is first seen
 public void head(Node node, int depth) {
   String name = node.nodeName();
   if (node instanceof TextNode) {
     append(((TextNode) node).text()); // TextNodes carry all user-readable text in the DOM.
   } else if (name.equals("li")) append("<li>");
   else if (name.equals("ul")) {
     append("<ul>");
   }
 }
Exemplo n.º 7
0
 public static boolean matches(HtmlNode reference, Node test) {
   if (test == null) {
     return false;
   }
   if (!CommonUtil.isEmpty(reference.id)) {
     return reference.id.equalsIgnoreCase(test.attr("id"));
   }
   if (!CommonUtil.isEmpty(reference.name)) {
     return reference.name.equalsIgnoreCase(test.attr("name"));
   }
   List<String[]> toMatch = new ArrayList<String[]>();
   toMatch.add(new String[] {reference.tagName, test.nodeName()});
   toMatch.add(new String[] {reference.type, test.attr("type")});
   toMatch.add(new String[] {reference.value, test.attr("value")});
   toMatch.add(new String[] {reference.title, test.attr("title")});
   toMatch.add(new String[] {reference.role, test.attr("role")});
   toMatch.add(new String[] {reference.alt, test.attr("alt")});
   toMatch.add(new String[] {reference.href, test.attr("href")});
   if (test instanceof Element) {
     toMatch.add(
         new String[] {
           CommonUtil.strip(reference.innerText, false),
           CommonUtil.strip(((Element) test).text(), false)
         });
   }
   String refClassesString = CommonUtil.toString(reference.classes, " ");
   Collection<String> refClasses =
       new HashSet<String>(Arrays.asList(refClassesString.toLowerCase().split("\\s")));
   Collection<String> testClasses =
       new HashSet<String>(Arrays.asList(test.attr("class").toLowerCase().split("\\s")));
   for (String[] pair : toMatch) {
     if (reference.any) {
       if (!CommonUtil.isEmpty(pair[0]) && pair[0].equalsIgnoreCase(pair[1])) {
         return true;
       }
     } else {
       if (!CommonUtil.isEmpty(pair[0]) && !pair[0].equalsIgnoreCase(pair[1])) {
         return false;
       }
     }
   }
   if (!refClasses.isEmpty()) {
     for (String testClass : testClasses) {
       if (reference.any) {
         if (refClasses.contains(testClass)) {
           return true;
         }
       } else {
         if (!refClasses.contains(testClass)) {
           return false;
         }
       }
     }
   }
   return !reference.any;
 }
Exemplo n.º 8
0
 private static void removeComments(Node node) {
   for (int i = 0; i < node.childNodes().size(); ) {
     Node child = node.childNode(i);
     if (child.nodeName().equals("#comment")) child.remove();
     else {
       removeComments(child);
       i++;
     }
   }
 }
Exemplo n.º 9
0
 // hit when all of the node's children (if any) have been visited
 public void tail(Node node, int depth) {
   String name = node.nodeName();
   if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5", "a", "span")) append("<br>");
   if (name.equals("ul")) {
     append("</ul>");
   }
   if (name.equals("li")) {
     append("</li>");
   }
 }
Exemplo n.º 10
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;
 }
Exemplo n.º 11
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;
 }
Exemplo n.º 12
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());
    }
  }
Exemplo n.º 13
0
 public static boolean isContent(Node node, HtmlNode matchResult, HtmlNode matchParent) {
   if (matchParent != null) {
     return matches(matchParent, node);
   }
   if (matchResult != null) {
     for (Node child : node.childNodes()) {
       if (matches(matchResult, child)) {
         return true;
       }
     }
     return false;
   }
   for (int i = 0; i < content.length; i++) {
     if (node.nodeName().equalsIgnoreCase(content[i])) {
       return true;
     }
   }
   return false;
 }
  // hit when the node is first seen
  public void head(Node node, int depth) {
    String name = node.nodeName();
    if (name.equals("li")) append('\n');
    else if (node.toString().startsWith("<select")) {
      append1("[SELECT]");
      append(tab);
    } else if (node.outerHtml().startsWith("<option")) {
      // append1(node.attr("value")+":");append1(" ");
      TextNodeVisitor textVisitor = new TextNodeVisitor();
      node.traverse(textVisitor);
      append1("{" + textVisitor.toString() + "}");

    } else if (node.outerHtml().startsWith("<input")) {
      if (node.attr("type").equals("input")) append1("[INPUT]" + node.attr("maxLength"));
    } else if (node.outerHtml().startsWith("<span")) {
      TextNodeVisitor textVisitor = new TextNodeVisitor();
      node.traverse(textVisitor);
      append1(":" + textVisitor.toString() + " ");
    }
  }
 public boolean isMainDiv(Node node) {
   return node instanceof Element
       && node.nodeName().equals("div")
       && node.hasAttr("class")
       && node.attr("class").contains("v-app");
 }
Exemplo n.º 16
0
 public void tail(Node node, int depth) {
   if (!node.nodeName().equals("#text")) // saves a void hit.
   node.outerHtmlTail(accum, depth, out);
 }
Exemplo n.º 17
0
 public void tail(Node source, int depth) {
   if (source instanceof Element && whitelist.isSafeTag(source.nodeName())) {
     destination = destination.parent(); // would have descended, so pop destination stack
   }
 }
Exemplo n.º 18
0
 // hit when all of the node's children (if any) have been visited
 public void tail(Node node, int depth) {
   String name = node.nodeName();
   if (name.equals("br")) append("\n");
   else if (StringUtil.in(name, "p", "h1", "h2", "h3", "h4", "h5")) append("\n\n");
   else if (name.equals("a")) append(String.format(" <%s>", node.absUrl("href")));
 }