Search in sources :

Example 11 with Elements

use of org.jsoup.select.Elements in project jsoup by jhy.

the class ElementTest method testHashAndEqualsAndValue.

@Test
public void testHashAndEqualsAndValue() {
    // .equals and hashcode are identity. value is content.
    String doc1 = "<div id=1><p class=one>One</p><p class=one>One</p><p class=one>Two</p><p class=two>One</p></div>" + "<div id=2><p class=one>One</p><p class=one>One</p><p class=one>Two</p><p class=two>One</p></div>";
    Document doc = Jsoup.parse(doc1);
    Elements els = doc.select("p");
    /*
        for (Element el : els) {
            System.out.println(el.hashCode() + " - " + el.outerHtml());
        }

        0 1534787905 - <p class="one">One</p>
        1 1534787905 - <p class="one">One</p>
        2 1539683239 - <p class="one">Two</p>
        3 1535455211 - <p class="two">One</p>
        4 1534787905 - <p class="one">One</p>
        5 1534787905 - <p class="one">One</p>
        6 1539683239 - <p class="one">Two</p>
        7 1535455211 - <p class="two">One</p>
        */
    assertEquals(8, els.size());
    Element e0 = els.get(0);
    Element e1 = els.get(1);
    Element e2 = els.get(2);
    Element e3 = els.get(3);
    Element e4 = els.get(4);
    Element e5 = els.get(5);
    Element e6 = els.get(6);
    Element e7 = els.get(7);
    assertEquals(e0, e0);
    assertTrue(e0.hasSameValue(e1));
    assertTrue(e0.hasSameValue(e4));
    assertTrue(e0.hasSameValue(e5));
    assertFalse(e0.equals(e2));
    assertFalse(e0.hasSameValue(e2));
    assertFalse(e0.hasSameValue(e3));
    assertFalse(e0.hasSameValue(e6));
    assertFalse(e0.hasSameValue(e7));
    assertEquals(e0.hashCode(), e0.hashCode());
    assertFalse(e0.hashCode() == (e2.hashCode()));
    assertFalse(e0.hashCode() == (e3).hashCode());
    assertFalse(e0.hashCode() == (e6).hashCode());
    assertFalse(e0.hashCode() == (e7).hashCode());
}
Also used : Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 12 with Elements

use of org.jsoup.select.Elements in project jsoup by jhy.

the class ElementTest method testPrependRowToTable.

@Test
public void testPrependRowToTable() {
    Document doc = Jsoup.parse("<table><tr><td>1</td></tr></table>");
    Element table = doc.select("tbody").first();
    table.prepend("<tr><td>2</td></tr>");
    assertEquals("<table><tbody><tr><td>2</td></tr><tr><td>1</td></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html()));
    // check sibling index (reindexChildren):
    Elements ps = doc.select("tr");
    for (int i = 0; i < ps.size(); i++) {
        assertEquals(i, ps.get(i).siblingIndex);
    }
}
Also used : Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 13 with Elements

use of org.jsoup.select.Elements in project jsoup by jhy.

the class ElementTest method insertChildrenAsCopy.

@Test
public void insertChildrenAsCopy() {
    Document doc = Jsoup.parse("<div id=1>Text <p>One</p> Text <p>Two</p></div><div id=2></div>");
    Element div1 = doc.select("div").get(0);
    Element div2 = doc.select("div").get(1);
    Elements ps = doc.select("p").clone();
    ps.first().text("One cloned");
    div2.insertChildren(-1, ps);
    // not moved -- cloned
    assertEquals(4, div1.childNodeSize());
    assertEquals(2, div2.childNodeSize());
    assertEquals("<div id=\"1\">Text <p>One</p> Text <p>Two</p></div><div id=\"2\"><p>One cloned</p><p>Two</p></div>", TextUtil.stripNewlines(doc.body().html()));
}
Also used : Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 14 with Elements

use of org.jsoup.select.Elements in project jsoup by jhy.

the class HtmlParserTest method testSupportsPartiallyNonAsciiTags.

@Test
public void testSupportsPartiallyNonAsciiTags() {
    String body = "<div>Check</divá>";
    Document doc = Jsoup.parse(body);
    Elements els = doc.select("div");
    assertEquals("Check", els.text());
}
Also used : Elements(org.jsoup.select.Elements) Test(org.junit.Test) ParseTest(org.jsoup.integration.ParseTest)

Example 15 with Elements

use of org.jsoup.select.Elements in project jsoup by jhy.

the class HtmlParserTest method handlesBaseTags.

@Test
public void handlesBaseTags() {
    // only listen to the first base href
    String h = "<a href=1>#</a><base href='/2/'><a href='3'>#</a><base href='http://bar'><a href=/4>#</a>";
    Document doc = Jsoup.parse(h, "http://foo/");
    // gets set once, so doc and descendants have first only
    assertEquals("http://foo/2/", doc.baseUri());
    Elements anchors = doc.getElementsByTag("a");
    assertEquals(3, anchors.size());
    assertEquals("http://foo/2/", anchors.get(0).baseUri());
    assertEquals("http://foo/2/", anchors.get(1).baseUri());
    assertEquals("http://foo/2/", anchors.get(2).baseUri());
    assertEquals("http://foo/2/1", anchors.get(0).absUrl("href"));
    assertEquals("http://foo/2/3", anchors.get(1).absUrl("href"));
    assertEquals("http://foo/4", anchors.get(2).absUrl("href"));
}
Also used : Elements(org.jsoup.select.Elements) Test(org.junit.Test) ParseTest(org.jsoup.integration.ParseTest)

Aggregations

Elements (org.jsoup.select.Elements)168 Element (org.jsoup.nodes.Element)96 Document (org.jsoup.nodes.Document)70 ArrayList (java.util.ArrayList)38 Test (org.junit.Test)33 IOException (java.io.IOException)24 URL (java.net.URL)13 ParseTest (org.jsoup.integration.ParseTest)11 File (java.io.File)9 List (java.util.List)8 HashMap (java.util.HashMap)7 SSPHandler (org.asqatasun.processor.SSPHandler)7 TestSolutionHandler (org.asqatasun.ruleimplementation.TestSolutionHandler)7 ProcessRemarkService (org.asqatasun.service.ProcessRemarkService)7 MalformedURLException (java.net.MalformedURLException)6 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 TextView (android.widget.TextView)4 RequestUtil (com.kyj.fx.voeditor.visual.util.RequestUtil)4 ResponseHandler (com.kyj.fx.voeditor.visual.util.ResponseHandler)4