Search in sources :

Example 1 with Node

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"));
}
Also used : Node(jodd.lagarto.dom.Node) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) NodeSelector(jodd.lagarto.dom.NodeSelector) Test(org.junit.Test)

Example 2 with Node

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());
}
Also used : Node(jodd.lagarto.dom.Node) Test(org.junit.Test)

Example 3 with Node

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;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 4 with Node

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;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 5 with Node

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;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Aggregations

Node (jodd.lagarto.dom.Node)30 Document (jodd.lagarto.dom.Document)7 NodeSelector (jodd.lagarto.dom.NodeSelector)6 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Text (jodd.lagarto.dom.Text)1