Skip to content

Commit 1ad72d3

Browse files
committed
* tests for dom navigation methods (up/down/previous/next)
1 parent 40e1316 commit 1ad72d3

File tree

1 file changed

+78
-18
lines changed

1 file changed

+78
-18
lines changed

src/test/java/org/jsoup/nodes/ElementTest.java

Lines changed: 78 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,67 @@ public class ElementTest {
4242
List<Element> empty = doc.getElementsByTag("wtf");
4343
assertEquals(0, empty.size());
4444
}
45-
45+
46+
@Test public void up() {
47+
Document doc = Jsoup.parse(reference);
48+
Element div2 = doc.getElementById("div2");
49+
assertNotNull(div2);
50+
51+
Element div1 = div2.up("div#div1");
52+
assertNotNull(div1);
53+
assertEquals("div1", div1.id());
54+
55+
Element invalid = div2.up("img.invalid");
56+
assertNull(invalid);
57+
}
58+
59+
@Test public void down() {
60+
Document doc = Jsoup.parse(reference);
61+
Element div1 = doc.getElementById("div1");
62+
assertNotNull(div1);
63+
64+
Element el = div1.down("div#div2 > img[src^=foo]");
65+
assertNotNull(el);
66+
assertEquals("img", el.tagName().toLowerCase());
67+
assertEquals("foo.png", el.attr("src"));
68+
}
69+
70+
@Test public void next() {
71+
Document doc = Jsoup.parse(reference);
72+
Elements ps = doc.getElementsByTag("p");
73+
assertEquals(2, ps.size());
74+
75+
Element el = ps.first().next("div#div2");
76+
assertNotNull(el);
77+
assertEquals("div2", el.id());
78+
}
79+
80+
@Test public void previous() {
81+
Document doc = Jsoup.parse(reference);
82+
Element div2 = doc.getElementById("div2");
83+
assertNotNull(div2);
84+
85+
Element el = div2.previous("p + p");
86+
assertNotNull(el);
87+
assertEquals("Another element", el.text());
88+
}
89+
90+
@Test public void chained() {
91+
Document doc = Jsoup.parse(reference);
92+
Element div1 = doc.getElementById("div1");
93+
assertNotNull(div1);
94+
95+
Element el = div1
96+
.down("img")
97+
.up("div")
98+
.previous("p")
99+
.previous("p")
100+
.next("p")
101+
.down("b");
102+
assertNotNull(el);
103+
assertEquals("element", el.text());
104+
}
105+
46106
@Test public void getNamespacedElementsByTag() {
47107
Document doc = Jsoup.parse("<div><abc:def id=1>Hello</abc:def></div>");
48108
Elements els = doc.getElementsByTag("abc:def");
@@ -63,7 +123,7 @@ public class ElementTest {
63123
Element span = div2.child(0).getElementById("2"); // called from <p> context should be span
64124
assertEquals("span", span.tagName());
65125
}
66-
126+
67127
@Test public void testGetText() {
68128
Document doc = Jsoup.parse(reference);
69129
assertEquals("Hello Another element", doc.text());
@@ -127,7 +187,7 @@ public class ElementTest {
127187
assertEquals("body", parents.get(2).tagName());
128188
assertEquals("html", parents.get(3).tagName());
129189
}
130-
190+
131191
@Test public void testElementSiblingIndex() {
132192
Document doc = Jsoup.parse("<div><p>One</p>...<p>Two</p>...<p>Three</p>");
133193
Elements ps = doc.select("p");
@@ -180,7 +240,7 @@ public class ElementTest {
180240
List<Element> none = doc.getElementsByAttributeValue("style", "none");
181241
assertEquals(0, none.size());
182242
}
183-
243+
184244
@Test public void testClassDomMethods() {
185245
Document doc = Jsoup.parse("<div><span class='mellow yellow'>Hello <b>Yellow</b></span></div>");
186246
List<Element> els = doc.getElementsByAttribute("class");
@@ -225,7 +285,7 @@ public class ElementTest {
225285
Document doc = Jsoup.parse("<title>Format test</title><div><p>Hello <span>jsoup <span>users</span></span></p><p>Good.</p></div>");
226286
assertEquals("<html>\n <head>\n <title>Format test</title>\n </head>\n <body>\n <div>\n <p>Hello <span>jsoup <span>users</span></span></p>\n <p>Good.</p>\n </div>\n </body>\n</html>", doc.html());
227287
}
228-
288+
229289
@Test public void testFormatOutline() {
230290
Document doc = Jsoup.parse("<title>Format test</title><div><p>Hello <span>jsoup <span>users</span></span></p><p>Good.</p></div>");
231291
doc.outputSettings().outline(true);
@@ -246,7 +306,7 @@ public class ElementTest {
246306
Element div = doc.select("div").first();
247307
assertEquals(" \n<p>Hello\n there\n</p>", div.html());
248308
}
249-
309+
250310
@Test public void testEmptyElementFormatHtml() {
251311
// don't put newlines into empty blocks
252312
Document doc = Jsoup.parse("<section><div></div></section>");
@@ -276,7 +336,7 @@ public class ElementTest {
276336
assertEquals("Gone", div.text());
277337
assertEquals(0, doc.select("p").size());
278338
}
279-
339+
280340
@Test public void testAddNewElement() {
281341
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
282342
Element div = doc.getElementById("1");
@@ -313,30 +373,30 @@ public class ElementTest {
313373
assertEquals(i, ps.get(i).siblingIndex);
314374
}
315375
}
316-
376+
317377
@Test public void testPrependElement() {
318378
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
319379
Element div = doc.getElementById("1");
320380
div.prependElement("p").text("Before");
321381
assertEquals("Before", div.child(0).text());
322382
assertEquals("Hello", div.child(1).text());
323383
}
324-
384+
325385
@Test public void testAddNewText() {
326386
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
327387
Element div = doc.getElementById("1");
328388
div.appendText(" there & now >");
329389
assertEquals("<p>Hello</p> there &amp; now &gt;", TextUtil.stripNewlines(div.html()));
330390
}
331-
391+
332392
@Test public void testPrependText() {
333393
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
334394
Element div = doc.getElementById("1");
335395
div.prependText("there & now > ");
336396
assertEquals("there & now > Hello", div.text());
337397
assertEquals("there &amp; now &gt; <p>Hello</p>", TextUtil.stripNewlines(div.html()));
338398
}
339-
399+
340400
@Test public void testAddNewHtml() {
341401
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
342402
Element div = doc.getElementById("1");
@@ -349,7 +409,7 @@ public class ElementTest {
349409
assertEquals(i, ps.get(i).siblingIndex);
350410
}
351411
}
352-
412+
353413
@Test public void testPrependNewHtml() {
354414
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
355415
Element div = doc.getElementById("1");
@@ -362,7 +422,7 @@ public class ElementTest {
362422
assertEquals(i, ps.get(i).siblingIndex);
363423
}
364424
}
365-
425+
366426
@Test public void testSetHtml() {
367427
Document doc = Jsoup.parse("<div id=1><p>Hello</p></div>");
368428
Element div = doc.getElementById("1");
@@ -377,28 +437,28 @@ public class ElementTest {
377437
assertEquals("<div><div class=\"head\"><p>Hello</p></div><p>There</p></div>", TextUtil.stripNewlines(doc.body().html()));
378438

379439
Element ret = p.wrap("<div><div class=foo></div><p>What?</p></div>");
380-
assertEquals("<div><div class=\"head\"><div><div class=\"foo\"><p>Hello</p></div><p>What?</p></div></div><p>There</p></div>",
440+
assertEquals("<div><div class=\"head\"><div><div class=\"foo\"><p>Hello</p></div><p>What?</p></div></div><p>There</p></div>",
381441
TextUtil.stripNewlines(doc.body().html()));
382442

383443
assertEquals(ret, p);
384444
}
385-
445+
386446
@Test public void before() {
387447
Document doc = Jsoup.parse("<div><p>Hello</p><p>There</p></div>");
388448
Element p1 = doc.select("p").first();
389449
p1.before("<div>one</div><div>two</div>");
390450
assertEquals("<div><div>one</div><div>two</div><p>Hello</p><p>There</p></div>", TextUtil.stripNewlines(doc.body().html()));
391-
451+
392452
doc.select("p").last().before("<p>Three</p><!-- four -->");
393453
assertEquals("<div><div>one</div><div>two</div><p>Hello</p><p>Three</p><!-- four --><p>There</p></div>", TextUtil.stripNewlines(doc.body().html()));
394454
}
395-
455+
396456
@Test public void after() {
397457
Document doc = Jsoup.parse("<div><p>Hello</p><p>There</p></div>");
398458
Element p1 = doc.select("p").first();
399459
p1.after("<div>one</div><div>two</div>");
400460
assertEquals("<div><p>Hello</p><div>one</div><div>two</div><p>There</p></div>", TextUtil.stripNewlines(doc.body().html()));
401-
461+
402462
doc.select("p").last().after("<p>Three</p><!-- four -->");
403463
assertEquals("<div><p>Hello</p><div>one</div><div>two</div><p>There</p><p>Three</p><!-- four --></div>", TextUtil.stripNewlines(doc.body().html()));
404464
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy