-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The FormElement extends Element to provide ready access to a form's controls, and to allow the form to be submitted. It also connects forms to their controls in situations when the DOM tree created does not have the form element be a parent of the control, like when the form tag is in a TR but the control in a TD. In that case the form tag gets reparented.
- Loading branch information
Showing
8 changed files
with
329 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.jsoup.nodes; | ||
|
||
import org.jsoup.Connection; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.helper.HttpConnection; | ||
import org.jsoup.helper.Validate; | ||
import org.jsoup.parser.Tag; | ||
import org.jsoup.select.Elements; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A HTML Form Element provides ready access to the form fields/controls that are associated with it. It also allows a | ||
* form to easily be submitted. | ||
*/ | ||
public class FormElement extends Element { | ||
private final Elements elements = new Elements(); | ||
|
||
/** | ||
* Create a new, standalone form element. | ||
* | ||
* @param tag tag of this element | ||
* @param baseUri the base URI | ||
* @param attributes initial attributes | ||
*/ | ||
public FormElement(Tag tag, String baseUri, Attributes attributes) { | ||
super(tag, baseUri, attributes); | ||
} | ||
|
||
/** | ||
* Get the list of form control elements associated with this form. | ||
* @return form controls associated with this element. | ||
*/ | ||
public Elements elements() { | ||
return elements; | ||
} | ||
|
||
/** | ||
* Add a form control element to this form. | ||
* @param element form control to add | ||
* @return this form element, for chaining | ||
*/ | ||
public FormElement addElement(Element element) { | ||
elements.add(element); | ||
return this; | ||
} | ||
|
||
/** | ||
* Prepare to submit this form. A Connection object is created with the request set up from the form values. You | ||
* can then set up other options (like user-agent, timeout, cookies), then execute it. | ||
* @return a connection prepared from the values of this form. | ||
* @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the | ||
* document's base URI when parsing. | ||
*/ | ||
public Connection submit() { | ||
String action = hasAttr("action") ? absUrl("action") : baseUri(); | ||
Validate.notEmpty(action, "Could not determine a form action URL for submit. Ensure you set a base URI when parsing."); | ||
Connection.Method method = attr("method").toUpperCase().equals("POST") ? | ||
Connection.Method.POST : Connection.Method.GET; | ||
|
||
Connection con = Jsoup.connect(action) | ||
.data(formData()) | ||
.method(method); | ||
|
||
return con; | ||
} | ||
|
||
/** | ||
* Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the | ||
* list will not be reflected in the DOM. | ||
* @return a list of key vals | ||
*/ | ||
public List<Connection.KeyVal> formData() { | ||
ArrayList<Connection.KeyVal> data = new ArrayList<Connection.KeyVal>(); | ||
|
||
// iterate the form control elements and accumulate their values | ||
for (Element el: elements) { | ||
if (!el.tag().isFormSubmittable()) continue; // contents are form listable, superset of submitable | ||
String name = el.attr("name"); | ||
if (name.length() == 0) continue; | ||
|
||
if ("select".equals(el.tagName())) { | ||
Elements options = el.select("option[selected]"); | ||
for (Element option: options) { | ||
data.add(HttpConnection.KeyVal.create(name, option.val())); | ||
} | ||
} else { | ||
data.add(HttpConnection.KeyVal.create(name, el.val())); | ||
} | ||
} | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.