Skip to content

Commit 11f7c1b

Browse files
committed
Search for element siblings by identity not content
Fixes jhy#554, jhy#561
1 parent 373ea35 commit 11f7c1b

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGES

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
jsoup changelog
22

3+
*** Release 1.8.3 [PENDING]
4+
* Fix an issue in Element.getElementSiblingIndex (and related methods) where sibling elements with the same content
5+
would incorrectly have the same sibling index.
6+
<https://github.com/jhy/jsoup/issues/554>
7+
38
*** Release 1.8.2 [2015-Apr-13]
49
* Performance improvements for parsing HTML on Android, of 1.5x to 1.9x, with larger parses getting a bigger
510
speed increase. For non-Android JREs, around 1.1x to 1.2x.

src/main/java/org/jsoup/nodes/Element.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ private static <E extends Element> Integer indexInList(Element search, List<E> e
571571

572572
for (int i = 0; i < elements.size(); i++) {
573573
E element = elements.get(i);
574-
if (element.equals(search))
574+
if (element == search)
575575
return i;
576576
}
577577
return null;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public class ElementTest {
117117
assertEquals("element", p.lastElementSibling().text());
118118
}
119119

120+
@Test public void testGetSiblingsWithDuplicateContent() {
121+
Document doc = Jsoup.parse("<div><p>Hello<p id=1>there<p>this<p>this<p>is<p>an<p id=last>element</div>");
122+
Element p = doc.getElementById("1");
123+
assertEquals("there", p.text());
124+
assertEquals("Hello", p.previousElementSibling().text());
125+
assertEquals("this", p.nextElementSibling().text());
126+
assertEquals("this", p.nextElementSibling().nextElementSibling().text());
127+
assertEquals("is", p.nextElementSibling().nextElementSibling().nextElementSibling().text());
128+
assertEquals("Hello", p.firstElementSibling().text());
129+
assertEquals("element", p.lastElementSibling().text());
130+
}
131+
120132
@Test public void testGetParents() {
121133
Document doc = Jsoup.parse("<div><p>Hello <span>there</span></div>");
122134
Element span = doc.select("span").first();
@@ -137,6 +149,14 @@ public class ElementTest {
137149
assertTrue(2 == ps.get(2).elementSiblingIndex());
138150
}
139151

152+
@Test public void testElementSiblingIndexSameContent() {
153+
Document doc = Jsoup.parse("<div><p>One</p>...<p>One</p>...<p>One</p>");
154+
Elements ps = doc.select("p");
155+
assertTrue(0 == ps.get(0).elementSiblingIndex());
156+
assertTrue(1 == ps.get(1).elementSiblingIndex());
157+
assertTrue(2 == ps.get(2).elementSiblingIndex());
158+
}
159+
140160
@Test public void testGetElementsWithClass() {
141161
Document doc = Jsoup.parse("<div class='mellow yellow'><span class=mellow>Hello <b class='yellow'>Yellow!</b></span><p>Empty</p></div>");
142162

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