/** * Inserts several child nodes before provided node. */ public void insertBefore(final Node[] newChilds, final Node refChild) { if (newChilds.length == 0) { return; } int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChilds, siblingIndex); }
/** * Inserts node before provided node. */ public void insertBefore(final Node newChild, final Node refChild) { int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChild, siblingIndex); }
/** * 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); } }
/** * Inserts several child nodes after referent node. */ public void insertAfter(final Node[] newChilds, final Node refChild) { if (newChilds.length == 0) { return; } int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChilds); } else { refChild.parentNode.insertChild(newChilds, siblingIndex); } }
/** * Insert content, specified by the parameter, to the beginning of each * element in the set of matched elements. */ public Jerry prepend(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.insertChild(workingDoc.getChildNodes(), 0); } return this; }
/** * 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; }
/** * 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; }
/** * Inserts several child nodes before provided node. */ public void insertBefore(final Node[] newChilds, final Node refChild) { if (newChilds.length == 0) { return; } int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChilds, siblingIndex); }
/** * Inserts node before provided node. */ public void insertBefore(final Node newChild, final Node refChild) { int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChild, siblingIndex); }
/** * Inserts node before provided node. */ public void insertBefore(Node newChild, Node refChild) { int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChild, siblingIndex); }
/** * Inserts node before provided node. */ public void insertBefore(Node newChild, Node refChild) { int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChild, siblingIndex); }
/** * Inserts several child nodes before provided node. */ public void insertBefore(Node[] newChilds, Node refChild) { int siblingIndex = refChild.getSiblingIndex(); refChild.parentNode.insertChild(newChilds, siblingIndex); }
/** * 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); } }
/** * Inserts node after provided node. */ public void insertAfter(Node newChild, Node refChild) { int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChild); } else { refChild.parentNode.insertChild(newChild, siblingIndex); } }
/** * Inserts several child nodes after referent node. */ public void insertAfter(Node[] newChilds, Node refChild) { int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChilds); } else { refChild.parentNode.insertChild(newChilds, siblingIndex); } }
/** * Inserts node after provided node. */ public void insertAfter(Node newChild, Node refChild) { int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChild); } else { refChild.parentNode.insertChild(newChild, siblingIndex); } }
/** * Inserts several child nodes after referent node. */ public void insertAfter(final Node[] newChilds, final Node refChild) { if (newChilds.length == 0) { return; } int siblingIndex = refChild.getSiblingIndex() + 1; if (siblingIndex == refChild.parentNode.getChildNodesCount()) { refChild.parentNode.addChild(newChilds); } else { refChild.parentNode.insertChild(newChilds, siblingIndex); } }
/** * Insert content, specified by the parameter, to the beginning of each * element in the set of matched elements. */ public Jerry prepend(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.insertChild(workingDoc.getChildNodes(), 0); } return this; }
/** * 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) { final Document doc = builder.parse(html); 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; }