Search in sources :

Example 11 with Document

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

the class Jerry method wrap.

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

Example 12 with Document

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

the class ParsingProblemsTest method testIssue23_1.

@Test
public void testIssue23_1() throws IOException {
    File file = new File(testDataRoot, "index-4-v1.html");
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    lagartoDOMBuilder.getConfig().setCollectErrors(true);
    Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file));
    assertTrue(doc.check());
    assertEquals(1, doc.getErrors().size());
}
Also used : LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) File(java.io.File) Test(org.junit.Test)

Example 13 with Document

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

the class Jerry method prepend.

/**
	 * 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;
}
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