Implement implicit form submission

This commit is contained in:
Manish Goregaokar 2014-11-24 21:49:55 +05:30
parent c89ec3910f
commit 6482e313d6
3 changed files with 24 additions and 3 deletions

View file

@ -25,6 +25,9 @@ pub trait Activatable : Copy {
// https://html.spec.whatwg.org/multipage/interaction.html#run-post-click-activation-steps // https://html.spec.whatwg.org/multipage/interaction.html#run-post-click-activation-steps
fn activation_behavior(&self); fn activation_behavior(&self);
// https://html.spec.whatwg.org/multipage/forms.html#implicit-submission
fn implicit_submission(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool);
// https://html.spec.whatwg.org/multipage/interaction.html#run-synthetic-click-activation-steps // https://html.spec.whatwg.org/multipage/interaction.html#run-synthetic-click-activation-steps
fn synthetic_click_activation(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool) { fn synthetic_click_activation(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool) {
let element = self.as_element().root(); let element = self.as_element().root();

View file

@ -20,7 +20,7 @@ use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable, ResultRoot
use dom::bindings::utils::{Reflectable, Reflector}; use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::{Document, DocumentHelpers}; use dom::document::{Document, DocumentHelpers};
use dom::element::{AttributeHandlers, Element, HTMLInputElementTypeId}; use dom::element::{AttributeHandlers, Element, HTMLInputElementTypeId};
use dom::element::RawLayoutElementHelpers; use dom::element::{RawLayoutElementHelpers, ActivationElementHelpers};
use dom::event::{Event, Bubbles, NotCancelable, EventHelpers}; use dom::event::{Event, Bubbles, NotCancelable, EventHelpers};
use dom::eventtarget::{EventTarget, NodeTargetTypeId}; use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement; use dom::htmlelement::HTMLElement;
@ -695,4 +695,19 @@ impl<'a> Activatable for JSRef<'a, HTMLInputElement> {
_ => () _ => ()
} }
} }
// https://html.spec.whatwg.org/multipage/forms.html#implicit-submission
fn implicit_submission(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool) {
let doc = document_from_node(*self).root();
// FIXME (#4082) use a custom iterator
let submits = doc.QuerySelectorAll("input[type=submit]".to_string()).unwrap().root();
// XXXManishearth there may be a more efficient way of doing this (#3553)
let owner = self.form_owner();
if owner == None || ElementCast::from_ref(*self).click_in_progress() {
return;
}
submits.into_vec().iter()
.filter_map(|t| HTMLInputElementCast::to_ref(*t.root()))
.find(|r| r.form_owner() == owner).map(|s| s.synthetic_click_activation(ctrlKey, shiftKey, altKey, metaKey));
}
} }

View file

@ -915,11 +915,14 @@ impl ScriptTask {
// I'm dispatching it after the key event so the script has a chance to cancel it // I'm dispatching it after the key event so the script has a chance to cancel it
// https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=27337
match key { match key {
Key::KeySpace | Key::KeyEnter if !prevented && state == Released => { Key::KeySpace if !prevented && state == Released => {
// TODO handle space and enter slightly differently
let maybe_elem: Option<JSRef<Element>> = ElementCast::to_ref(target); let maybe_elem: Option<JSRef<Element>> = ElementCast::to_ref(target);
maybe_elem.map(|el| el.as_maybe_activatable().map(|a| a.synthetic_click_activation(ctrl, alt, shift, meta))); maybe_elem.map(|el| el.as_maybe_activatable().map(|a| a.synthetic_click_activation(ctrl, alt, shift, meta)));
} }
Key::KeyEnter if !prevented && state == Released => {
let maybe_elem: Option<JSRef<Element>> = ElementCast::to_ref(target);
maybe_elem.map(|el| el.as_maybe_activatable().map(|a| a.implicit_submission(ctrl, alt, shift, meta)));
}
_ => () _ => ()
} }
window.flush_layout(); window.flush_layout();