congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
NodeSelector.select
Code IndexAdd Tabnine to your IDE (free)

How to use
select
method
in
jodd.lagarto.dom.NodeSelector

Best Java code snippets using jodd.lagarto.dom.NodeSelector.select (Showing top 20 results out of 315)

origin: oblac/jodd

/**
 * Process selectors and keep adding results.
 */
protected void processSelectors(final List<Node> results, final List<CssSelector> selectors) {
  List<Node> selectedNodes = select(rootNode, selectors);
  for (Node selectedNode : selectedNodes) {
    if (!results.contains(selectedNode)) {
      results.add(selectedNode);
    }
  }
}
origin: oblac/jodd

@Test
void testTwoHtml() throws IOException {
  File file = new File(testDataRoot, "two.html");
  String htmlContent = FileUtil.readString(file);
  Document document = new LagartoDOMBuilder().parse(htmlContent);
  Node html = new NodeSelector(document).select("html").get(0);
  assertNotNull(html);
  Node body = new NodeSelector(html).selectFirst("body");
  Element h1 = body.getFirstChildElement();
  assertEquals("h1", h1.getNodeName());
  Node comment1 = body.getFirstChild().getNextSibling();
  assertEquals(Node.NodeType.COMMENT, comment1.getNodeType());
  Element p = (Element) new NodeSelector(body).selectFirst("p");
  assertEquals(h1, p.getPreviousSiblingElement());
  assertEquals(h1, comment1.getNextSiblingElement());
  assertNull(comment1.getNextSiblingName());
  // check if filter works just for sub elements
  List<Node> p_ems = new NodeSelector(p).select("em");
  assertEquals(1, p_ems.size());
  Element script = (Element) new NodeSelector(html).selectFirst("script");
  assertEquals("text/javascript", script.getAttribute("type"));
  assertTrue(document.check());
}
origin: oblac/jodd

/**
 * Selects nodes using CSS3 selector query and returns the very first one.
 */
public Node selectFirst(final String query) {
  List<Node> selectedNodes = select(query);
  if (selectedNodes.isEmpty()) {
    return null;
  }
  return selectedNodes.get(0);
}
origin: oblac/jodd

/**
 * Selects nodes using {@link NodeFilter node filter} and return the very first one.
 */
public Node selectFirst(final NodeFilter nodeFilter) {
  List<Node> selectedNodes = select(nodeFilter);
  if (selectedNodes.isEmpty()) {
    return null;
  }
  return selectedNodes.get(0);
}
origin: oblac/jodd

/**
 * Selects nodes using CSS3 selector query.
 */
public List<Node> select(final String query) {
  Collection<List<CssSelector>> selectorsCollection = CSSelly.parse(query);
  return select(selectorsCollection);
}
origin: oblac/jodd

  @Override
  public boolean match(final Node node, final List<List<CssSelector>> selectors) {
    List<Node> matchedNodes = new NodeSelector(node).select(selectors);
    return !matchedNodes.isEmpty();
  }
}
origin: oblac/jodd

/**
 * Reduce the set of matched elements to those that have a descendant that
 * matches the selector or DOM element.
 */
public Jerry has(final String cssSelectors) {
  List<Node> result = new NodeList(nodes.length);
  if (nodes.length > 0) {
    for (Node node : nodes) {
      NodeSelector nodeSelector = createNodeSelector(node);
      List<Node> selectedNodes = nodeSelector.select(cssSelectors);
      if (!selectedNodes.isEmpty()) {
        result.add(node);
      }
    }
  }
  return new Jerry(this, result);
}
origin: oblac/jodd

/**
 *  Gets the descendants of each element in the current set of matched elements,
 *  filtered by a selector.
 */
public Jerry find(final String cssSelector) {
  final List<Node> result = new NodeList();
  if (nodes.length > 0) {
    for (Node node : nodes) {
      NodeSelector nodeSelector = createNodeSelector(node);
      List<Node> filteredNodes = nodeSelector.select(cssSelector);
      result.addAll(filteredNodes);
    }
  }
  return new Jerry(this, result);
}
origin: oblac/jodd

/**
 * Reduces the set of matched elements to those that match the selector.
 */
public Jerry filter(final String cssSelectors) {
  List<Node> result = new NodeList(nodes.length);
  if (nodes.length > 0) {
    for (Node node : nodes) {
      Node parentNode = node.getParentNode();
      if (parentNode == null) {
        continue;
      }
      NodeSelector nodeSelector = createNodeSelector(parentNode);
      List<Node> selectedNodes = nodeSelector.select(cssSelectors);
      for (Node selected : selectedNodes) {
        if (node == selected) {
          result.add(node);
        }
      }
    }
  }
  return new Jerry(this, result);
}
origin: oblac/jodd

@Test
void testTags() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("div");
  assertEquals(5, nodes.size());
  nodes = nodeSelector.select("body");
  assertEquals(1, nodes.size());
  assertEquals("body", nodes.get(0).getNodeName());
  nodes = nodeSelector.select("p");
  assertEquals(4, nodes.size());
}
origin: oblac/jodd

@Test
void testPseudoFunctions() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("p#text > em:nth-child(2n+1)");
  assertEquals(2, nodes.size());
  nodes = nodeSelector.select("p#text  em:nth-child(2n+1)");
  assertEquals(4, nodes.size());
  nodes = nodeSelector.select("p#text > em:nth-last-child(2n+1)");
  assertEquals(1, nodes.size());
  assertEquals("lina", (nodes.get(0)).getAttribute("id"));
  nodes = nodeSelector.select("p#text em:nth-last-child(2n+1)");
  assertEquals(2, nodes.size());
  nodes = nodeSelector.select("p#text em:nth-of-type(odd)");
  assertEquals(4, nodes.size());
  nodes = nodeSelector.select("p#text em:nth-of-type(even)");
  assertEquals(1, nodes.size());
  nodes = nodeSelector.select("p#text em:nth-last-of-type(odd)");
  assertEquals(4, nodes.size());
  nodes = nodeSelector.select("p#text em:nth-last-of-type(even)");
  assertEquals(1, nodes.size());
}
origin: oblac/jodd

@Test
void testMoreTags() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("div b");
  assertEquals(1, nodes.size());
  assertEquals("b", nodes.get(0).getNodeName());
  assertEquals("p", nodes.get(0).getParentNode().getNodeName());
  nodes = nodeSelector.select("p b");
  assertEquals(4, nodes.size());
  nodes = nodeSelector.select("div div");
  assertEquals(3, nodes.size());
  nodes = nodeSelector.select("div div div");
  assertEquals(2, nodes.size());
  nodes = nodeSelector.select("* div div div");
  assertEquals(2, nodes.size());
}
origin: oblac/jodd

@Test
void testIdClass() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("div#fiona");
  assertEquals(1, nodes.size());
  assertEquals("fiona", nodes.get(0).getAttribute("id"));
  nodes = nodeSelector.select("div#fiona div#jodd");
  assertEquals(1, nodes.size());
  assertEquals("jodd", nodes.get(0).getAttribute("id"));
  nodes = nodeSelector.select("div.k1");
  assertEquals(1, nodes.size());
  nodes = nodeSelector.select("div.k2");
  assertEquals(2, nodes.size());
  nodes = nodeSelector.select("div.k1.k2");
  assertEquals(1, nodes.size());
  nodes = nodeSelector.select(".k1.k2");
  assertEquals(1, nodes.size());
  nodes = nodeSelector.select("p em");
  assertEquals(5, nodes.size());
  nodes = nodeSelector.select("p * em");
  assertEquals(2, nodes.size());
}
origin: oblac/jodd

@Test
void testDuplicatesRemoval() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("div div");
  assertEquals(3, nodes.size());
}
origin: oblac/jodd

@Test
void testCombinators() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select("p#text > span");
  assertEquals(1, nodes.size());
  Node spanNode = nodes.get(0);
  assertEquals("spanner", (spanNode.getChild(0)).getNodeValue());
  nodes = nodeSelector.select("p#text > em");
  assertEquals(3, nodes.size());
  nodes = nodeSelector.select("p#text > em#oleg + em");
  assertEquals(0, nodes.size());
  nodes = nodeSelector.select("p#text > em#oleg + span");
  assertEquals(1, nodes.size());
  assertEquals("spanner", (nodes.get(0).getChild(0)).getNodeValue());
  nodes = nodeSelector.select("p#text > em#oleg ~ em");
  assertEquals(1, nodes.size());
  assertEquals("lina", nodes.get(0).getAttribute(0).getValue());
}
origin: oblac/jodd

@Test
void testCollectionOfSelectors() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<CssSelector> selectors1 = new CSSelly("body").parse();
  List<CssSelector> selectors2 = new CSSelly("p").parse();
  List<List<CssSelector>> collection = new ArrayList<>();
  collection.add(selectors1);
  collection.add(selectors2);
  List<Node> nodes = nodeSelector.select(collection);
  assertEquals(5, nodes.size());
  assertEquals("body", nodes.get(0).nodeName);
}
origin: oblac/jodd

@Test
void testNodeSelector() throws IOException {
  NodeSelector nodeSelector = createNodeFilter();
  List<Node> nodes = nodeSelector.select(new NodeFilter() {
    @Override
    public boolean accept(Node node) {
      if (node.getNodeType() != Node.NodeType.ELEMENT) {
        return false;
      }
      if ("ema".equals(node.getAttribute("id"))) {
        return true;
      }
      if ("lina".equals(node.getAttribute("id"))) {
        return true;
      }
      return false;
    }
  });
  assertEquals(2, nodes.size());
}
origin: oblac/jodd

@Test
void test250() {
  String html = "<html>\n" +
    "  <body>\n" +
    "    <a href=\"/go?to=foobar&index=null\" title=\"Choice 1\">link</a>\n" +
    "  </body>\n" +
    "</html>";
  LagartoDOMBuilder domBuilder = new LagartoDOMBuilder();
  NodeSelector nodeSelector = new NodeSelector(domBuilder.parse(html));
  List<Node> selectedNodes = nodeSelector.select("a[title='Choice 1']");
  System.out.println();
  assertEquals("/go?to=foobar&index=null", selectedNodes.get(0).getAttribute("href"));
}
origin: oblac/jodd

@Test
void testGroupOfSelectors() throws IOException {
  File file = new File(testDataRoot, "one.html");
  String htmlContent = FileUtil.readString(file);
  Document document = new LagartoDOMBuilder().parse(htmlContent);
  List<Node> nodes = new NodeSelector(document).select("em, b, b");
  assertEquals(9, nodes.size());
  assertTrue(document.check());
}

origin: oblac/jodd

@Test
void testClassWithTabs() throws IOException {
  File file = new File(testDataRoot, "class-tabs.html");
  String htmlContent = FileUtil.readString(file);
  Document document = new LagartoDOMBuilder().parse(htmlContent);
  List<Node> nodes = new NodeSelector(document).select(".hey");
  assertEquals(1, nodes.size());
  Node n = nodes.get(0);
  assertEquals("div", n.getNodeName());
  assertEquals("jodd", n.getAttribute("id"));
}
jodd.lagarto.domNodeSelectorselect

Javadoc

Selects nodes using CSS3 selector query.

Popular methods of NodeSelector

  • <init>
  • filter
    Filter nodes.
  • selectAndAdd
    Selects single node for single selector and appends it to the results.
  • walk
  • walkDescendantsIteratively
    Walks over the child notes, maintaining the tree order and not using recursion.
  • processSelectors
    Process selectors and keep adding results.
  • createCSSelly
    Creates CSSelly instance for parsing files.
  • selectFirst
    Selects nodes using NodeFilter and return the very first one.

Popular in Java

  • Making http requests using okhttp
  • setRequestProperty (URLConnection)
  • findViewById (Activity)
  • onCreateOptionsMenu (Activity)
  • ObjectMapper (com.fasterxml.jackson.databind)
    ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Pl
  • RandomAccessFile (java.io)
    Allows reading from and writing to a file in a random-access manner. This is different from the uni-
  • MessageFormat (java.text)
    Produces concatenated messages in language-neutral way. New code should probably use java.util.Forma
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • DataSource (javax.sql)
    An interface for the creation of Connection objects which represent a connection to a database. This
  • BoxLayout (javax.swing)
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now