use of jodd.lagarto.dom.Node in project jodd by oblac.
the class JerryMiscTest method test250.
@Test
public 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"));
}
use of jodd.lagarto.dom.Node in project jodd by oblac.
the class JerryMiscTest method test233.
@Test
public 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());
}
use of jodd.lagarto.dom.Node in project jodd by oblac.
the class Jerry method append.
// ---------------------------------------------------------------- DOM
/**
* Inserts content, specified by the parameter, to the end of each
* element in the set of matched elements.
*/
public Jerry append(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.addChild(workingDoc.getChildNodes());
}
return this;
}
use of jodd.lagarto.dom.Node in project jodd by oblac.
the class Jerry method html.
/**
* Sets the HTML contents of each element in the set of matched elements.
*/
public Jerry html(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.removeAllChilds();
// clone to preserve for next iteration
// as nodes will be detached from parent
Document workingDoc = doc.clone();
node.addChild(workingDoc.getChildNodes());
}
return this;
}
use of jodd.lagarto.dom.Node in project jodd by oblac.
the class Jerry method replaceWith.
/**
* 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;
}
Aggregations