/** * Test fetching a form, and submitting it with a file attached. */ @Test public void postHtmlFile() throws IOException { Document index = Jsoup.connect("http://direct.infohound.net/tidy/").get(); FormElement form = index.select("[name=tidy]").forms().get(0); Connection post = form.submit(); File uploadFile = ParseTest.getFile("/htmltests/google-ipod.html"); FileInputStream stream = new FileInputStream(uploadFile); Connection.KeyVal fileData = post.data("_file"); fileData.value("check.html"); fileData.inputStream(stream); Connection.Response res; try { res = post.execute(); } finally { stream.close(); } Document out = res.parse(); assertTrue(out.text().contains("HTML Tidy Complete")); }
/** * Give a Document, parse it and return all form data. * * @param doc * @return null if this Document has more than one form or does not have form */ private static Map<String, String> getFormData(Document doc){ Elements formElements = doc.getElementsByTag("form"); if(formElements.size() != 1) return null; List<FormElement> forms = formElements.forms(); List<KeyVal> data = forms.get(0).formData(); HashMap<String, String> map = new HashMap<String, String>(); for(KeyVal kv: data){ map.put(kv.key(), kv.value()); } return map; }
FormElement insertForm(Token.StartTag startTag, boolean onStack) { Tag tag = Tag.valueOf(startTag.name(), settings); FormElement el = new FormElement(tag, baseUri, startTag.attributes); setFormElement(el); insertNode(el); if (onStack) stack.add(el); return el; }
@Test public void createsFormElements() { String html = "<body><form><input id=1><input id=2></form></body>"; Document doc = Jsoup.parse(html); Element el = doc.select("form").first(); assertTrue("Is form element", el instanceof FormElement); FormElement form = (FormElement) el; Elements controls = form.elements(); assertEquals(2, controls.size()); assertEquals("1", controls.get(0).id()); assertEquals("2", controls.get(1).id()); }
@Test public void associatedFormControlsWithDisjointForms() { // form gets closed, isn't parent of controls String html = "<table><tr><form><input type=hidden id=1><td><input type=text id=2></td><tr></table>"; Document doc = Jsoup.parse(html); Element el = doc.select("form").first(); assertTrue("Is form element", el instanceof FormElement); FormElement form = (FormElement) el; Elements controls = form.elements(); assertEquals(2, controls.size()); assertEquals("1", controls.get(0).id()); assertEquals("2", controls.get(1).id()); assertEquals("<table><tbody><tr><form></form><input type=\"hidden\" id=\"1\"><td><input type=\"text\" id=\"2\"></td></tr><tr></tr></tbody></table>", TextUtil.stripNewlines(doc.body().html())); }
List<Node> parseFragment(String inputFragment, Element context, String baseUri, ParseErrorList errors, ParseSettings settings) { // context may be null state = HtmlTreeBuilderState.Initial; initialiseParse(new StringReader(inputFragment), baseUri, errors, settings); contextElement = context; fragmentParsing = true; Element root = null; if (context != null) { if (context.ownerDocument() != null) // quirks setup: doc.quirksMode(context.ownerDocument().quirksMode()); // initialise the tokeniser state: String contextTag = context.tagName(); if (StringUtil.in(contextTag, "title", "textarea")) tokeniser.transition(TokeniserState.Rcdata); else if (StringUtil.in(contextTag, "iframe", "noembed", "noframes", "style", "xmp")) tokeniser.transition(TokeniserState.Rawtext); else if (contextTag.equals("script")) tokeniser.transition(TokeniserState.ScriptData); else if (contextTag.equals(("noscript"))) tokeniser.transition(TokeniserState.Data); // if scripting enabled, rawtext else if (contextTag.equals("plaintext")) tokeniser.transition(TokeniserState.Data); else tokeniser.transition(TokeniserState.Data); // default root = new Element(Tag.valueOf("html", settings), baseUri); doc.appendChild(root); stack.add(root); resetInsertionMode(); // setup form element to nearest form on context (up ancestor chain). ensures form controls are associated // with form correctly Elements contextChain = context.parents(); contextChain.add(0, context); for (Element parent: contextChain) { if (parent instanceof FormElement) { formElement = (FormElement) parent; break; } } } runParser(); if (context != null) return root.childNodes(); else return doc.childNodes(); }
FormElement getFormElement() { return formElement; }
void setFormElement(FormElement formElement) { this.formElement = formElement; }
/** * This function logs in the student/ parent with their current credentials to Gradebook Pinnacle. * It then records session IDs and expiration time via cookies. * Precondition: username and password are both defined. * * @return a status code (1 for no error, -1 for bad password, and -2 for server error.) * @throws IOException if an I/O error occurs while connecting to server. */ public int login() throws IOException { final URL LOAD_URL = new URL(LOGON); final String USERNAME_FIELD = "ctl00$ContentPlaceHolder$Username"; final String PASSWORD_FIELD = "ctl00$ContentPlaceHolder$Password"; try { // Submitting form data. Document html = Jsoup.parse(LOAD_URL, 60000); FormElement form = (FormElement) html.getElementsByTag("form").get(0); Connection conn = form.submit(); // Find username and password field. Connection.KeyVal usernameField = null; Connection.KeyVal passwordField = null; for (Connection.KeyVal field : conn.request().data()) if (field.key().equals(USERNAME_FIELD)) usernameField = field; else if (field.key().equals(PASSWORD_FIELD)) passwordField = field; if (usernameField == null) { System.err.println("EXPECTED: Form field for username."); return -2; } if (passwordField == null) { System.err.println("EXPECTED: Form field for password."); return -2; } // Submit username and password usernameField.value(username); passwordField.value(password); Connection.Response resp = conn.timeout(60000).execute(); if (resp.url().equals(LOAD_URL)) return -1; else { cookies.putAll(resp.cookies()); updateExpirationDate(); students.addAll(Parser.parseStudents(this, resp.body())); MULTIPLE_STUDENTS = students.size() > 1; loggedIn = true; return 1; } } catch (SocketTimeoutException e) { e.printStackTrace(); return -2; } }