/** * Removes the set of matched elements from the DOM. */ public Jerry remove() { if (nodes.length == 0) { return this; } for (Node node : nodes) { node.detachFromParent(); } return this; }
/** * Removes the set of matched elements from the DOM. * Identical to {@link #remove()}. */ public Jerry detach() { if (nodes.length == 0) { return this; } for (Node node : nodes) { node.detachFromParent(); } return this; }
/** * Removes child node at given index. * Returns removed node or <code>null</code> if index is invalid. */ public Node removeChild(final int index) { if (childNodes == null) { return null; } Node node; try { node = childNodes.get(index); } catch (IndexOutOfBoundsException ignore) { return null; } node.detachFromParent(); return node; }
/** * Removes all child nodes. Each child node will be detached from this parent. */ public void removeAllChilds() { List<Node> removedNodes = childNodes; childNodes = null; childElementNodes = null; childElementNodesCount = 0; if (removedNodes != null) { for (int i = 0, removedNodesSize = removedNodes.size(); i < removedNodesSize; i++) { Node removedNode = removedNodes.get(i); removedNode.detachFromParent(); } } }
/** * 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(); }
/** * Appends child node. Don't use this node in the loop, * since it might be slow due to {@link #reindexChildren()}. */ public void addChild(final Node node) { node.detachFromParent(); node.parentNode = this; initChildNodes(node); childNodes.add(node); reindexChildrenOnAdd(1); }
/** * Appends several child nodes at once. * Reindex is done only once, after all children are added. */ public void addChild(final Node... nodes) { if (nodes.length == 0) { return; // nothing to add } for (Node node : nodes) { node.detachFromParent(); node.parentNode = this; initChildNodes(node); childNodes.add(node); } reindexChildrenOnAdd(nodes.length); }
/** * Inserts node at given index. */ public void insertChild(final Node node, final int index) { node.detachFromParent(); node.parentNode = this; try { initChildNodes(node); childNodes.add(index, node); } catch (IndexOutOfBoundsException ignore) { throw new LagartoDOMException("Invalid node index: " + index); } reindexChildren(); }
/** * Inserts several nodes at ones. Reindex is done onl once, * after all children are added. */ public void insertChild(final Node[] nodes, int index) { for (Node node : nodes) { node.detachFromParent(); node.parentNode = this; try { initChildNodes(node); childNodes.add(index, node); index++; } catch (IndexOutOfBoundsException ignore) { throw new LagartoDOMException("Invalid node index: " + index); } } reindexChildren(); }
/** * Removes last child node if contains just empty text. */ protected void removeLastChildNodeIfEmptyText(final Node parentNode, final boolean closedTag) { if (parentNode == null) { return; } Node lastChild = parentNode.getLastChild(); if (lastChild == null) { return; } if (lastChild.getNodeType() != Node.NodeType.TEXT) { return; } if (closedTag) { if (parentNode.getChildNodesCount() == 1) { return; } } Text text = (Text) lastChild; if (text.isBlank()) { lastChild.detachFromParent(); } }
/** * 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; }
/** * 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; }
parentNode.detachFromParent();
/** * Performs the fix for elements. */ protected void fixElements() { for (Element fosterElement : fosterElements) { // find parent table Element lastTable = findLastTable(fosterElement); Node fosterElementParent = fosterElement.getParentNode(); // filter our foster element Node[] fosterChilds = fosterElement.getChildNodes(); for (Node fosterChild : fosterChilds) { if (fosterChild.getNodeType() == Node.NodeType.ELEMENT) { if (isOneOfTableElements((Element) fosterChild)) { // move all child table elements outside // the foster element fosterChild.detachFromParent(); fosterElementParent.insertBefore(fosterChild, fosterElement); } } } // finally, move foster element above the table fosterElement.detachFromParent(); lastTable.getParentNode().insertBefore(fosterElement, lastTable); } }
/** * Removes the set of matched elements from the DOM. */ public Jerry remove() { if (nodes.length == 0) { return this; } for (Node node : nodes) { node.detachFromParent(); } return this; }
/** * 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(); }
/** * Removes child node. It works only with direct children, i.e. * if provided child node is not a child nothing happens. */ public void removeChild(Node childNode) { if (childNode.getParentNode() != this) { return; } childNode.detachFromParent(); }
/** * Appends child node. Don't use this node in the loop, * since it might be slow due to {@link #reindexChildren()}. */ public void addChild(final Node node) { node.detachFromParent(); node.parentNode = this; initChildNodes(node); childNodes.add(node); reindexChildrenOnAdd(1); }
/** * Appends child node. Don't use this node in the loop, * since it might be slow due to {@link #reindexChildren()}. */ public void addChild(Node node) { node.detachFromParent(); node.parentNode = this; node.deepLevel = deepLevel + 1; initChildNodes(); childNodes.add(node); reindexChildrenOnAdd(1); }
/** * Appends child node. Don't use this node in the loop, * since it might be slow due to {@link #reindexChildren()}. */ public void addChild(Node node) { node.detachFromParent(); node.parentNode = this; initChildNodes(node); childNodes.add(node); reindexChildrenOnAdd(1); }