@Test public void adoptedFormsRetainInputs() { String html = "<html>\n" + "<body> \n" + " <table>\n" + " <form action=\"/hello.php\" method=\"post\">\n" + " <tr><td>User:</td><td> <input type=\"text\" name=\"user\" /></td></tr>\n" + " <tr><td>Password:</td><td> <input type=\"password\" name=\"pass\" /></td></tr>\n" + " <tr><td><input type=\"submit\" name=\"login\" value=\"login\" /></td></tr>\n" + " </form>\n" + " </table>\n" + "</body>\n" + "</html>"; Document doc = Jsoup.parse(html); FormElement form = (FormElement) doc.select("form").first(); List<Connection.KeyVal> data = form.formData(); assertEquals(3, data.size()); assertEquals("user", data.get(0).key()); assertEquals("pass", data.get(1).key()); assertEquals("login", data.get(2).key()); }
public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(HttpConnection.KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(HttpConnection.KeyVal.create(name, val)); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; }
FormElement extends Element { public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(HttpConnection.KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(HttpConnection.KeyVal.create(name, val)); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; } }
FormElement extends Element { public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(HttpConnection.KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(HttpConnection.KeyVal.create(name, val)); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); }
FormElement extends Element { public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(HttpConnection.KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(HttpConnection.KeyVal.create(name, val)); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); Elements elements(); FormElement addElement(Element element); Connection submit(); List<Connection.KeyVal> formData(); }
FormElement extends Element { public List<Connection.KeyVal> formData() { ArrayList<Connection.KeyVal> data = new ArrayList<>(); for (Element el: elements) { if (!el.tag().isFormSubmittable()) continue; if (el.hasAttr("disabled")) continue; String name = el.attr("name"); if (name.length() == 0) continue; String type = el.attr("type"); if ("select".equals(el.tagName())) { Elements options = el.select("option[selected]"); boolean set = false; for (Element option: options) { data.add(HttpConnection.KeyVal.create(name, option.val())); set = true; } if (!set) { Element option = el.select("option").first(); if (option != null) data.add(HttpConnection.KeyVal.create(name, option.val())); } } else if ("checkbox".equalsIgnoreCase(type) || "radio".equalsIgnoreCase(type)) { if (el.hasAttr("checked")) { final String val = el.val().length() > 0 ? el.val() : "on"; data.add(HttpConnection.KeyVal.create(name, val)); } } else { data.add(HttpConnection.KeyVal.create(name, el.val())); } } return data; } FormElement(Tag tag, String baseUri, Attributes attributes); Elements elements(); FormElement addElement(Element element); Connection submit(); List<Connection.KeyVal> formData();  }