/** * Removes child node. It works only with direct children, i.e. * if provided child node is not a child nothing happens. */ public void removeChild(final Node childNode) { if (childNode.getParentNode() != this) { return; } childNode.detachFromParent(); }
@Override public boolean match(final Node node) { return node.getParentNode().getNodeType() == Node.NodeType.DOCUMENT; } }
/** * Gets the parent of each element in the current set of matched elements. */ public Jerry parent() { List<Node> result = new NodeList(nodes.length); if (nodes.length > 0) { for (Node node : nodes) { result.add(node.getParentNode()); } } 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 CSS path to this node from document root. */ public String getCssPath() { StringBuilder path = new StringBuilder(); Node node = this; while (node != null) { String nodeName = node.getNodeName(); if (nodeName != null) { StringBuilder sb = new StringBuilder(); sb.append(' ').append(nodeName); String id = node.getAttribute("id"); if (id != null) { sb.append('#').append(id); } path.insert(0, sb); } node = node.getParentNode(); } if (path.charAt(0) == ' ') { return path.substring(1); } return path.toString(); }
@Override public boolean match(final Node node) { return node.getSiblingElementIndex() == node.getParentNode().getChildElementsCount() - 1; } }
@Override public boolean match(final Node node) { return (node.getSiblingElementIndex() == 0) && (node.getParentNode().getChildElementsCount() == 1); } }
/** * Finds matching parent open tag or <code>null</code> if not found. */ protected Node findMatchingParentOpenTag(String tagName) { Node parent = parentNode; if (!rootNode.config.isCaseSensitive()) { tagName = tagName.toLowerCase(); } while (parent != null) { String parentNodeName = parent.getNodeName(); if (parentNodeName != null) { if (!rootNode.config.isCaseSensitive()) { parentNodeName = parentNodeName.toLowerCase(); } } if (tagName.equals(parentNodeName)) { return parent; } parent = parent.getParentNode(); } return null; }
/** * 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); }
/** * Checks the current matched set of elements against a selector and * return <code>true</code> if at least one of these elements matches * the given arguments. */ public boolean is(final String cssSelectors) { if (nodes.length == 0) { return false; } 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) { return true; } } } return false; }
@Override public boolean match(final Node node, final PseudoFunctionExpression expression) { int value = node.getParentNode().getChildElementsCount() - node.getSiblingElementIndex(); return expression.match(value); }
/** * Remove the parents of the set of matched elements from the DOM, leaving * the matched elements (and siblings, if any) in their place. */ public Jerry unwrap() { if (nodes.length == 0) { return this; } for (Node node : nodes) { Node parent = node.getParentNode(); // if a node already is the root element, don't unwrap if (parent == null) { continue; } // replace, if possible Node grandparent = parent.getParentNode(); if (grandparent == null) { continue; } Node[] siblings = parent.getChildNodes(); int index = parent.getSiblingIndex(); grandparent.insertChild(siblings, index); parent.detachFromParent(); } return this; }
/** * Reduces the set of matched elements to those that pass the * {@link JerryFunction function's} test. */ public Jerry filter(final JerryFunction jerryFunction) { List<Node> result = new NodeList(nodes.length); for (int i = 0; i < nodes.length; i++) { Node node = nodes[i]; Node parentNode = node.getParentNode(); if (parentNode == null) { continue; } Jerry $this = new Jerry(this, node); boolean accept = jerryFunction.onNode($this, i); if (accept) { result.add(node); } } return new Jerry(this, result); }
/** * 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); }
@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); } }
/** * Replace each element in the set of matched elements with the provided * new content and return the set of elements that was removed. */ public Jerry replaceWith(String html) { if (html == null) { html = StringPool.EMPTY; } final Document doc = builder.parse(html); if (nodes.length == 0) { return this; } for (Node node : nodes) { Node parent = node.getParentNode(); // if a node already is the root element, don't unwrap if (parent == null) { continue; } // replace, if possible Document workingDoc = doc.clone(); int index = node.getSiblingIndex(); parent.insertChild(workingDoc.getFirstChild(), index); node.detachFromParent(); } return this; }
if (domBuilder.config.isImpliedEndTags()) { if (implRules.implicitlyCloseTagOnEOF(thisNode.getNodeName())) { thisNode = thisNode.getParentNode(); continue; thisNode = thisNode.getParentNode();
@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()); }
/** * 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; }
newPerson.getChild(0).addChild(new Text(xml, "Just Maria")); man.getParentNode().addChild(newPerson);