/** * 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, final String expression) { String text = node.getTextContent(); return text.contains(expression); } }
/** * Wraps an HTML structure around each element in the set of matched elements. * Returns the original set of elements for chaining purposes. */ public Jerry wrap(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Document workingDoc = doc.clone(); Node inmostNode = workingDoc; while (inmostNode.hasChildNodes()) { inmostNode = inmostNode.getFirstChild(); } // replace Node parent = node.getParentNode(); int index = node.getSiblingIndex(); inmostNode.addChild(node); parent.insertChild(workingDoc.getFirstChild(), index); } return this; }
/** * Changes owner document for given node and all its children. */ protected void changeOwnerDocument(final Node node, final Document ownerDocument) { node.ownerDocument = ownerDocument; int childCount = node.getChildNodesCount(); for (int i = 0; i < childCount; i++) { Node child = node.getChild(i); changeOwnerDocument(child, ownerDocument); } }
/** * Inserts node after provided node. */ public void insertAfter(final Node newChild, final Node refChild) { int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChild); } else { refChild.parentNode.insertChild(newChild, siblingIndex); } }
@Override public boolean match(final Node node) { return (node.getSiblingNameIndex() == 0) && (node.getNextSiblingName() == null); } }
if (node.getNodeType() == Node.NodeType.TEXT) { String value = node.getNodeValue(); if (!StringUtil.isBlank(value)) { if (isParentNodeOneOfFosterTableElements(node.getParentNode())) { fosterTexts.add((Text) node); if (node.getNodeType() == Node.NodeType.ELEMENT) { Element element = (Element) node; Node parentNode = node.getParentNode(); if ( isParentNodeOneOfFosterTableElements(parentNode) && parentNode.insertAfter(formChildNodes, element); return false; } else { int childs = node.getChildNodesCount(); for (int i = 0; i < childs; i++) { Node childNode = node.getChild(i);
if (childNode.getNodeType() == NodeType.ELEMENT) { if (childNode.siblingElementIndex != siblingElementIndex) { return false; int childCount = getChildNodesCount(); for (int i = 0; i < childCount; i++) { Node child = getChild(i); if (child.siblingElementIndex >= 0) { if (childElementNodes[child.siblingElementIndex] != child) { if (sibling.siblingNameIndex == -1 && nodeType == NodeType.ELEMENT && nodeName.equals(sibling.getNodeName())) { if (sibling.siblingNameIndex != index++) { return false; if (!childNode.check()) { return false;
assertEquals("Fred Bloggs", man.getChild(0).getTextContent()); man.getChild(0).getChild(0).setNodeValue("Just Joe"); newPerson.getChild(0).addChild(new Text(xml, "Just Maria")); man.getParentNode().addChild(newPerson);
case DESCENDANT: LinkedList<Node> nodes = new LinkedList<>(); int childCount = rootNode.getChildNodesCount(); for (int i = 0; i < childCount; i++) { nodes.add(rootNode.getChild(i)); break; case CHILD: childCount = rootNode.getChildNodesCount(); for (int i = 0; i < childCount; i++) { Node node = rootNode.getChild(i); selectAndAdd(node, cssSelector, result); Node node = rootNode.getNextSiblingElement(); if (node != null) { selectAndAdd(node, cssSelector, result); node = rootNode; while (true) { node = node.getNextSiblingElement(); if (node == null) { break;
@Override public boolean match(final Node node, final PseudoFunctionExpression expression) { Node child = node.getParentNode().getLastChildElement(node.getNodeName()); int value = child.getSiblingNameIndex() + 1 - node.getSiblingNameIndex(); return expression.match(value); } }
@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()); }
@Test void test233() { String html = "<div><span>name</span>value</div>"; Jerry $ = Jerry.jerry(html); assertEquals("namevalue", $.text()); assertEquals(1, $.children().size()); Node div = $.children().get(0); assertEquals("div", div.getNodeName()); assertEquals(2, div.getChildNodesCount()); assertEquals("value", div.getChild(1).getNodeValue()); }
/** * Initializes list of child elements. */ protected void initChildElementNodes() { if (childElementNodes == null) { childElementNodes = new Element[childElementNodesCount]; int childCount = getChildNodesCount(); for (int i = 0; i < childCount; i++) { Node child = getChild(i); if (child.siblingElementIndex >= 0) { childElementNodes[child.siblingElementIndex] = (Element) child; } } } }
/** * Gets the siblings of each element in the set of matched elements. */ public Jerry siblings() { List<Node> result = new NodeList(nodes.length); if (nodes.length > 0) { for (Node node : nodes) { Node[] allElements = node.getParentNode().getChildElements(); for (Node sibling : allElements) { if (sibling != node) { result.add(sibling); } } } } return new Jerry(this, result); }
/** * Finds the last table in stack of open elements. */ protected Element findLastTable(final Node node) { Node tableNode = node; while (tableNode != null) { if (tableNode.getNodeType() == Node.NodeType.ELEMENT) { String tableNodeName = tableNode.getNodeName().toLowerCase(); if (tableNodeName.equals("table")) { break; } } tableNode = tableNode.getParentNode(); } return (Element) tableNode; }
/** * Returns this node's next <b>element</b>. */ public Node getNextSiblingElement() { parentNode.initChildElementNodes(); if (siblingElementIndex == -1) { int max = parentNode.getChildNodesCount(); for (int i = siblingIndex; i < max; i++) { Node sibling = parentNode.childNodes.get(i); if (sibling.getNodeType() == NodeType.ELEMENT) { return sibling; } } return null; } int index = siblingElementIndex + 1; if (index >= parentNode.childElementNodesCount) { return null; } return parentNode.childElementNodes[index]; }
/** * Returns this node's next <b>element</b> with the same name. */ public Node getNextSiblingName() { if (nodeName == null) { return null; } initSiblingNames(); int index = siblingNameIndex + 1; int max = parentNode.getChildNodesCount(); for (int i = siblingIndex + 1; i < max; i++) { Node sibling = parentNode.childNodes.get(i); if ((index == sibling.siblingNameIndex) && nodeName.equals(sibling.getNodeName())) { return sibling; } } return null; }
/** * Sets the content of each element in the set of matched elements to the specified text. */ public Jerry text(String text) { if (nodes.length == 0) { return this; } if (text == null) { text = StringPool.EMPTY; } for (Node node : nodes) { node.removeAllChilds(); Text textNode = new Text(node.getOwnerDocument(), text); node.addChild(textNode); } return this; }
@Override public List<String> extractList(String data) { List<String> strings = new LinkedList<>(); Jerry doc = jerry(data); Node[] nodes = doc.$(query).get(); for (Node node : nodes) { switch (outType) { case TYPE_TEXT: strings.add(parse(node.getTextContent())); break; case TYPE_HTML: strings.add(parse(node.getHtml())); break; default: strings.add(parse(node.getAttribute(outType))); break; } } return strings; }