Skip to content

Commit

Permalink
Added new method childrenCount complementary to child(int)
Browse files Browse the repository at this point in the history
  • Loading branch information
literakl committed Jan 1, 2020
1 parent 8d1d503 commit 468c536
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/jsoup/nodes/Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,18 @@ public Element child(int index) {
return childElementsList().get(index);
}

/**
* Get the number of child nodes that are elements.
* <p>
* This method works on the same filtered list like {@link #child(int)}.
* </p>
* @return the number of child nodes that are elements
* @see #child(int)
*/
public int childrenCount() {
return childElementsList().size();
}

/**
* Get this element's child elements.
* <p>
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1482,4 +1482,12 @@ public FilterResult tail(Node node, int depth) {

assertSame(div, div2);
}

@Test
public void testChildMixedContent() {
Document doc = Jsoup.parse("<table><tbody>\n<tr>\n<td>15:00</td>\n<td>sport</td>\n</tr>\n</tbody></table>");
Element row = doc.selectFirst("table tbody tr");
assertSame(2, row.childrenCount());
assertFalse(row.childrenCount() == row.childNodeSize());
}
}

0 comments on commit 468c536

Please sign in to comment.