Add form submission via input element

This commit is contained in:
Rohan Prinja 2014-10-11 17:29:15 +05:30 committed by Manish Goregaokar
parent c92d58980c
commit 05134e6d1f
3 changed files with 103 additions and 22 deletions

View file

@ -367,46 +367,77 @@ pub enum FormMethod {
} }
pub enum FormSubmitter<'a> { pub enum FormSubmitter<'a> {
FormElement(JSRef<'a, HTMLFormElement>) FormElement(JSRef<'a, HTMLFormElement>),
InputElement(JSRef<'a, HTMLInputElement>)
// TODO: Submit buttons, image submit, etc etc // TODO: Submit buttons, image submit, etc etc
} }
impl<'a> FormSubmitter<'a> { impl<'a> FormSubmitter<'a> {
fn action(&self) -> DOMString { fn action(&self) -> DOMString {
match *self { match *self {
FormElement(form) => form.Action() FormElement(form) => form.Action(),
InputElement(input_element) => {
let element: JSRef<Element> = ElementCast::from_ref(input_element);
if element.has_attribute("formaction") {
input_element.FormAction()
} else {
input_element.form_owner().map_or("".to_string(), |f_o| f_o.Action())
}
}
} }
} }
fn enctype(&self) -> FormEncType { fn enctype(&self) -> FormEncType {
match *self { let attr = match *self {
FormElement(form) => { FormElement(form) => form.Enctype(),
match form.Enctype().as_slice() { InputElement(input_element) => {
"multipart/form-data" => FormDataEncoded, let element: JSRef<Element> = ElementCast::from_ref(input_element);
"text/plain" => TextPlainEncoded, if element.has_attribute("formenctype") {
// https://html.spec.whatwg.org/multipage/forms.html#attr-fs-enctype input_element.FormEnctype()
// urlencoded is the default } else {
_ => UrlEncoded input_element.form_owner().map_or("".to_string(), |f_o| f_o.Enctype())
} }
} }
};
match attr.as_slice() {
"multipart/form-data" => FormDataEncoded,
"text/plain" => TextPlainEncoded,
// https://html.spec.whatwg.org/multipage/forms.html#attr-fs-enctype
// urlencoded is the default
_ => UrlEncoded
} }
} }
fn method(&self) -> FormMethod { fn method(&self) -> FormMethod {
match *self { let attr = match *self {
FormElement(form) => { FormElement(form) => form.Method(),
match form.Method().as_slice() { InputElement(input_element) => {
"dialog" => FormDialog, let element: JSRef<Element> = ElementCast::from_ref(input_element);
"post" => FormPost, if element.has_attribute("formmethod") {
_ => FormGet input_element.FormMethod()
} else {
input_element.form_owner().map_or("".to_string(), |f_o| f_o.Method())
} }
} }
};
match attr.as_slice() {
"dialog" => FormDialog,
"post" => FormPost,
_ => FormGet
} }
} }
fn target(&self) -> DOMString { fn target(&self) -> DOMString {
match *self { match *self {
FormElement(form) => form.Target() FormElement(form) => form.Target(),
InputElement(input_element) => {
let element: JSRef<Element> = ElementCast::from_ref(input_element);
if element.has_attribute("formtarget") {
input_element.FormTarget()
} else {
input_element.form_owner().map_or("".to_string(), |f_o| f_o.Target())
}
}
} }
} }
} }

View file

@ -19,7 +19,7 @@ use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
use dom::htmlformelement::{FormOwner, HTMLFormElement}; use dom::htmlformelement::{FormOwner, HTMLFormElement};
use dom::node::{DisabledStateHelpers, Node, NodeHelpers, ElementNodeTypeId, document_from_node}; use dom::node::{DisabledStateHelpers, Node, NodeHelpers, ElementNodeTypeId, document_from_node, window_from_node};
use dom::virtualmethods::VirtualMethods; use dom::virtualmethods::VirtualMethods;
use servo_util::str::{DOMString, parse_unsigned_integer}; use servo_util::str::{DOMString, parse_unsigned_integer};
@ -169,6 +169,56 @@ impl<'a> HTMLInputElementMethods for JSRef<'a, HTMLInputElement> {
// https://html.spec.whatwg.org/multipage/forms.html#attr-fe-name // https://html.spec.whatwg.org/multipage/forms.html#attr-fe-name
make_setter!(SetName, "name") make_setter!(SetName, "name")
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formaction
fn FormAction(self) -> DOMString {
let element: JSRef<Element> = ElementCast::from_ref(self);
let url = element.get_url_attribute("formaction");
match url.as_slice() {
"" => {
let window = window_from_node(self).root();
window.get_url().serialize()
},
_ => url
}
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formaction
make_setter!(SetFormAction, "formaction")
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formenctype
fn FormEnctype(self) -> DOMString {
let elem: JSRef<Element> = ElementCast::from_ref(self);
let enctype = elem.get_string_attribute("formenctype").into_ascii_lower();
// https://html.spec.whatwg.org/multipage/forms.html#attr-fs-enctype
match enctype.as_slice() {
"text/plain" | "multipart/form-data" => enctype,
_ => "application/x-www-form-urlencoded".to_string()
}
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formenctype
make_setter!(SetFormEnctype, "formenctype")
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formmethod
fn FormMethod(self) -> DOMString {
let elem: JSRef<Element> = ElementCast::from_ref(self);
let method = elem.get_string_attribute("formmethod").into_ascii_lower();
// https://html.spec.whatwg.org/multipage/forms.html#attr-fs-method
match method.as_slice() {
"post" | "dialog" => method,
_ => "get".to_string()
}
}
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formmethod
make_setter!(SetFormMethod, "formmethod")
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formtarget
make_getter!(FormTarget)
// https://html.spec.whatwg.org/multipage/forms.html#dom-input-formtarget
make_setter!(SetFormTarget, "formtarget")
} }
trait HTMLInputElementHelpers { trait HTMLInputElementHelpers {

View file

@ -15,11 +15,11 @@ interface HTMLInputElement : HTMLElement {
attribute boolean disabled; attribute boolean disabled;
//readonly attribute HTMLFormElement? form; //readonly attribute HTMLFormElement? form;
//readonly attribute FileList? files; //readonly attribute FileList? files;
// attribute DOMString formAction; attribute DOMString formAction;
// attribute DOMString formEnctype; attribute DOMString formEnctype;
// attribute DOMString formMethod; attribute DOMString formMethod;
// attribute boolean formNoValidate; // attribute boolean formNoValidate;
// attribute DOMString formTarget; attribute DOMString formTarget;
// attribute unsigned long height; // attribute unsigned long height;
// attribute boolean indeterminate; // attribute boolean indeterminate;
// attribute DOMString inputMode; // attribute DOMString inputMode;