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());
}
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);
}
}
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()));
}
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());
}
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"));
}
Aggregations