From 6482e313d646e25368548eb734a8ed49a4dfcee7 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 24 Nov 2014 21:49:55 +0530 Subject: [PATCH] Implement implicit form submission --- components/script/dom/activation.rs | 3 +++ components/script/dom/htmlinputelement.rs | 17 ++++++++++++++++- components/script/script_task.rs | 7 +++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/components/script/dom/activation.rs b/components/script/dom/activation.rs index 267ca5d58a1..e4a54112e58 100644 --- a/components/script/dom/activation.rs +++ b/components/script/dom/activation.rs @@ -25,6 +25,9 @@ pub trait Activatable : Copy { // https://html.spec.whatwg.org/multipage/interaction.html#run-post-click-activation-steps 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 fn synthetic_click_activation(&self, ctrlKey: bool, shiftKey: bool, altKey: bool, metaKey: bool) { let element = self.as_element().root(); diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index a0b02f6a3d2..c5e103eaa48 100644 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -20,7 +20,7 @@ use dom::bindings::js::{JS, JSRef, Root, Temporary, OptionalRootable, ResultRoot use dom::bindings::utils::{Reflectable, Reflector}; use dom::document::{Document, DocumentHelpers}; use dom::element::{AttributeHandlers, Element, HTMLInputElementTypeId}; -use dom::element::RawLayoutElementHelpers; +use dom::element::{RawLayoutElementHelpers, ActivationElementHelpers}; use dom::event::{Event, Bubbles, NotCancelable, EventHelpers}; use dom::eventtarget::{EventTarget, NodeTargetTypeId}; 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)); + } } diff --git a/components/script/script_task.rs b/components/script/script_task.rs index 9ccf7653af5..f0352052df9 100644 --- a/components/script/script_task.rs +++ b/components/script/script_task.rs @@ -915,11 +915,14 @@ impl ScriptTask { // 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 match key { - Key::KeySpace | Key::KeyEnter if !prevented && state == Released => { - // TODO handle space and enter slightly differently + Key::KeySpace if !prevented && state == Released => { let maybe_elem: Option> = ElementCast::to_ref(target); 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> = ElementCast::to_ref(target); + maybe_elem.map(|el| el.as_maybe_activatable().map(|a| a.implicit_submission(ctrl, alt, shift, meta))); + } _ => () } window.flush_layout();