Skip to content

Commit

Permalink
Added a forms() convenience method to Elements
Browse files Browse the repository at this point in the history
This allows one to get at FormElements without casting.
  • Loading branch information
jhy committed Feb 16, 2013
1 parent 7690381 commit 834d314
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/jsoup/select/Elements.java
Expand Up @@ -2,6 +2,7 @@


import org.jsoup.helper.Validate; import org.jsoup.helper.Validate;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.nodes.FormElement;
import org.jsoup.nodes.Node; import org.jsoup.nodes.Node;


import java.util.*; import java.util.*;
Expand Down Expand Up @@ -487,6 +488,19 @@ public Elements traverse(NodeVisitor nodeVisitor) {
return this; return this;
} }


/**
* Get the {@link FormElement} forms from the selected elements, if any.
* @return a list of FormElements pulled from the matched elements. The list will be empty if the elements contain
* no forms.
*/
public List<FormElement> forms() {
ArrayList<FormElement> forms = new ArrayList<FormElement>();
for (Element el: contents)
if (el instanceof FormElement)
forms.add((FormElement) el);
return forms;
}

// implements List<Element> delegates: // implements List<Element> delegates:
public int size() {return contents.size();} public int size() {return contents.size();}


Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/jsoup/select/ElementsTest.java
Expand Up @@ -3,8 +3,12 @@
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.TextUtil; import org.jsoup.TextUtil;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.FormElement;
import org.jsoup.nodes.Node; import org.jsoup.nodes.Node;
import org.junit.Test; import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*; import static org.junit.Assert.*;


/** /**
Expand Down Expand Up @@ -252,4 +256,17 @@ public void tail(Node node, int depth) {
}); });
assertEquals("<div><p><#text></#text></p></div><div><#text></#text></div>", accum.toString()); assertEquals("<div><p><#text></#text></p></div><div><#text></#text></div>", accum.toString());
} }

@Test public void forms() {
Document doc = Jsoup.parse("<form id=1><input name=q></form><div /><form id=2><input name=f></form>");
Elements els = doc.select("*");
assertEquals(9, els.size());

List<FormElement> forms = els.forms();
assertEquals(2, forms.size());
assertTrue(forms.get(0) != null);
assertTrue(forms.get(1) != null);
assertEquals("1", forms.get(0).id());
assertEquals("2", forms.get(1).id());
}
} }

2 comments on commit 834d314

@dma
Copy link

@dma dma commented on 834d314 Feb 23, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi jhy, does this fix issue #249?

I just sent over a pull request. Thanks for looking at this.

@jhy
Copy link
Owner Author

@jhy jhy commented on 834d314 Jul 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dma, yes. (Sorry for late reply!)

Please sign in to comment.