Search in sources :

Example 6 with Document

use of jodd.lagarto.dom.Document 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 7 with Document

use of jodd.lagarto.dom.Document 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)

Example 8 with Document

use of jodd.lagarto.dom.Document in project jodd by oblac.

the class Jerry method before.

/**
	 * Inserts content, specified by the parameter, before each
	 * element in the set of matched elements.
	 */
public Jerry before(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.insertBefore(workingDoc.getChildNodes(), node);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Example 9 with Document

use of jodd.lagarto.dom.Document in project jodd by oblac.

the class JerryParserTest method testAttributeCaseSensitive.

@Test
public void testAttributeCaseSensitive() {
    String str = "<dIV id='one' myAttr='aaa'>xxx</dIV>";
    Jerry.JerryParser jerryParser = Jerry.jerry();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableHtmlMode();
    // default, case not sensitive
    Jerry doc = jerryParser.parse(str);
    Document document = (Document) doc.get(0);
    Element divNode = (Element) document.getChild(0);
    assertEquals("div", divNode.getNodeName());
    assertNotNull(divNode.getAttribute("myattr"));
    assertNotNull(divNode.getAttribute("myAttr"));
    Element divNode2 = (Element) doc.$("div[myattr=aaa]").nodes[0];
    assertSame(divNode, divNode2);
    assertEquals("<div id=\"one\" myattr=\"aaa\">xxx</div>", doc.html());
    // case sensitive
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).getConfig().setCaseSensitive(true);
    doc = jerryParser.parse(str);
    document = (Document) doc.get(0);
    divNode = (Element) document.getChild(0);
    assertEquals("dIV", divNode.getNodeName());
    assertNull(divNode.getAttribute("myattr"));
    assertEquals("<dIV id=\"one\" myAttr=\"aaa\">xxx</dIV>", doc.html());
    assertEquals(0, doc.$("div[myattr=aaa]").nodes.length);
    divNode2 = (Element) doc.$("dIV[myAttr=aaa]").nodes[0];
    assertSame(divNode, divNode2);
}
Also used : Element(jodd.lagarto.dom.Element) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Test(org.junit.Test)

Example 10 with Document

use of jodd.lagarto.dom.Document in project jodd by oblac.

the class Jerry method after.

/**
	 * Inserts content, specified by the parameter, after each
	 * element in the set of matched elements.
	 */
public Jerry after(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.insertAfter(workingDoc.getChildNodes(), node);
    }
    return this;
}
Also used : Node(jodd.lagarto.dom.Node) Document(jodd.lagarto.dom.Document)

Aggregations

Document (jodd.lagarto.dom.Document)13 Node (jodd.lagarto.dom.Node)7 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)6 Test (org.junit.Test)6 File (java.io.File)4 Jerry (jodd.jerry.Jerry)2 Element (jodd.lagarto.dom.Element)2