/** * Gets the value of an attribute for the first element in the set of matched elements. * Returns <code>null</code> if set is empty. */ public String attr(final String name) { if (nodes.length == 0) { return null; } if (name == null) { return null; } return nodes[0].getAttribute(name); }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("checkbox"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("image"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("file"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("password"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("radio"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("reset"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("submit"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("button"); } }
@Override public boolean match(final Node node) { String type = node.getAttribute("type"); if (type == null) { return false; } return type.equals("text"); } }
/** * Determines whether any of the matched elements are assigned the given class. */ public boolean hasClass(final String... classNames) { if (nodes.length == 0) { return false; } for (Node node : nodes) { String attrClass = node.getAttribute("class"); Set<String> classes = createPropertiesSet(attrClass, ' '); for (String className : classNames) { if (classes.contains(className)) { return true; } } } return false; }
/** * Gets the value of a style property for the first element * in the set of matched elements. Returns <code>null</code> * if set is empty. */ public String css(String propertyName) { if (nodes.length == 0) { return null; } propertyName = Format.fromCamelCase(propertyName, '-'); String styleAttrValue = nodes[0].getAttribute("style"); if (styleAttrValue == null) { return null; } Map<String, String> styles = createPropertiesMap(styleAttrValue, ';', ':'); return styles.get(propertyName); }
public boolean accept(final Node node) { if (!node.hasAttribute(name)) { return false; } if (value == null) { // just detect if attribute exist return true; } String nodeValue = node.getAttribute(name); if (nodeValue == null) { return false; } return match.compare(nodeValue, value); } }
@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; } });
/** * Adds the specified class(es) to each of the set of matched elements. */ public Jerry addClass(final String... classNames) { if (nodes.length == 0) { return this; } for (Node node : nodes) { String attrClass = node.getAttribute("class"); Set<String> classes = createPropertiesSet(attrClass, ' '); boolean wasChange = false; for (String className : classNames) { if (classes.add(className)) { wasChange = true; } } if (wasChange) { String attrValue = generateAttributeValue(classes, ' '); node.setAttribute("class", attrValue); } } return this; }
@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")); }
@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")); }
@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()); }
@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()); }
@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()); }