Search in sources :

Example 1 with Element

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

the class ParsingProblemsTest method testKelkoo.

@Test
public void testKelkoo() throws Exception {
    File file = new File(testDataRoot, "kelkoo.html");
    Jerry jerry;
    try {
        jerry = Jerry.jerry().parse(FileUtil.readString(file));
    } catch (Exception ex) {
        fail(ex.toString());
        throw ex;
    }
    Element script = (Element) jerry.$("script").get(0);
    assertEquals("script", script.getNodeName());
    assertEquals(6, script.getAttributesCount());
    assertEquals("src", script.getAttribute(0).getName());
    assertEquals("data-config", script.getAttribute(1).getName());
    assertEquals("ext\\u00e9rieur|barbecue,", script.getAttribute(2).getName());
    assertEquals("planchaaccessoires\":\"http:\\", script.getAttribute(3).getName());
    assertEquals("www.kelkoo.fr\"}'", script.getAttribute(4).getName());
    assertEquals("data-adsense-append", script.getAttribute(5).getName());
}
Also used : Element(jodd.lagarto.dom.Element) Jerry(jodd.jerry.Jerry) File(java.io.File) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Element

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

the class ParsingProblemsTest method testNamespaces.

@Test
public void testNamespaces() throws IOException {
    File file = new File(testDataRoot, "namespace.xml");
    LagartoDOMBuilder lagartoDOMBuilder = new LagartoDOMBuilder();
    lagartoDOMBuilder.enableXmlMode();
    lagartoDOMBuilder.getConfig().setCalculatePosition(true);
    Document doc = lagartoDOMBuilder.parse(FileUtil.readString(file));
    assertTrue(doc.check());
    Element cfgTestElement = (Element) doc.getChild(1);
    assertEquals("cfg:test", cfgTestElement.getNodeName());
    Element cfgNode = (Element) cfgTestElement.getChild(0);
    assertEquals("cfg:node", cfgNode.getNodeName());
    Jerry.JerryParser jerryParser = new Jerry.JerryParser();
    ((LagartoDOMBuilder) jerryParser.getDOMBuilder()).enableXmlMode();
    Jerry jerry = jerryParser.parse(FileUtil.readString(file));
    final StringBuilder result = new StringBuilder();
    jerry.$("cfg\\:test").each(($this, index) -> {
        result.append($this.$("cfg\\:node").text());
        return true;
    });
    assertEquals("This is a text", result.toString());
}
Also used : Element(jodd.lagarto.dom.Element) LagartoDOMBuilder(jodd.lagarto.dom.LagartoDOMBuilder) Document(jodd.lagarto.dom.Document) Jerry(jodd.jerry.Jerry) File(java.io.File) Test(org.junit.Test)

Example 3 with Element

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

the class JerryMiscTest method testHtmlNodesOwner.

@Test
public void testHtmlNodesOwner() {
    Jerry doc = Jerry.jerry().parse("<div>1<div id='x'>2</div>3</div>");
    doc.$("#x").html("<span>wow</span>");
    assertEquals("<div>1<div id=\"x\"><span>wow</span></div>3</div>", doc.html());
    Element divx = doc.get(0).getChildElement(0).getChildElement(0);
    assertSame(doc.get(0), divx.getOwnerDocument());
    assertEquals("span", divx.getChildElement(0).getNodeName());
    assertSame(doc.get(0), divx.getChildElement(0).getOwnerDocument());
    assertSame(doc.get(0), divx.getChildElement(0).getChild(0).getOwnerDocument());
}
Also used : Element(jodd.lagarto.dom.Element) Test(org.junit.Test)

Example 4 with Element

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

Aggregations

Element (jodd.lagarto.dom.Element)4 Test (org.junit.Test)4 File (java.io.File)2 Jerry (jodd.jerry.Jerry)2 Document (jodd.lagarto.dom.Document)2 LagartoDOMBuilder (jodd.lagarto.dom.LagartoDOMBuilder)2 IOException (java.io.IOException)1