diff --git a/dom/events/EventDispatcher.cpp b/dom/events/EventDispatcher.cpp index 96f065ee84..25644cd1f3 100644 --- a/dom/events/EventDispatcher.cpp +++ b/dom/events/EventDispatcher.cpp @@ -40,6 +40,7 @@ #include "mozilla/dom/ScrollAreaEvent.h" #include "mozilla/dom/SimpleGestureEvent.h" #include "mozilla/dom/StorageEvent.h" +#include "mozilla/dom/SubmitEvent.h" #include "mozilla/dom/TimeEvent.h" #include "mozilla/dom/TouchEvent.h" #include "mozilla/dom/TransitionEvent.h" @@ -1066,6 +1067,12 @@ EventDispatcher::CreateEvent(EventTarget* aOwner, case eAnimationEventClass: return NS_NewDOMAnimationEvent(aOwner, aPresContext, aEvent->AsAnimationEvent()); + case eFormEventClass: + if (aEvent->mMessage == eFormSubmit) { + return NS_NewDOMSubmitEvent(aOwner, aPresContext, + aEvent->AsFormEvent()); + } + return NS_NewDOMEvent(aOwner, aPresContext, aEvent); default: // For all other types of events, create a vanilla event object. return NS_NewDOMEvent(aOwner, aPresContext, aEvent); diff --git a/dom/events/SubmitEvent.cpp b/dom/events/SubmitEvent.cpp new file mode 100644 index 0000000000..71c6777e8f --- /dev/null +++ b/dom/events/SubmitEvent.cpp @@ -0,0 +1,64 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/SubmitEvent.h" + +#include "mozilla/ContentEvents.h" +#include "nsIContent.h" + +namespace mozilla { +namespace dom { + +NS_IMPL_CYCLE_COLLECTION_INHERITED(SubmitEvent, Event, mSubmitter) + +NS_IMPL_ADDREF_INHERITED(SubmitEvent, Event) +NS_IMPL_RELEASE_INHERITED(SubmitEvent, Event) + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SubmitEvent) +NS_INTERFACE_MAP_END_INHERITING(Event) + +SubmitEvent::SubmitEvent(EventTarget* aOwner, + nsPresContext* aPresContext, + InternalFormEvent* aEvent) + : Event(aOwner, aPresContext, aEvent) +{ + if (aEvent) { + nsIContent* originator = aEvent->mOriginator; + if (originator && originator->IsHTMLElement()) { + mSubmitter = static_cast<::nsGenericHTMLElement*>(originator); + } + } +} + +already_AddRefed +SubmitEvent::Constructor(const GlobalObject& aGlobal, + const nsAString& aType, + const SubmitEventInit& aParam, + ErrorResult& aRv) +{ + nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); + RefPtr e = new SubmitEvent(t, nullptr, nullptr); + bool trusted = e->Init(t); + e->InitEvent(aType, aParam.mBubbles, aParam.mCancelable); + e->mSubmitter = aParam.mSubmitter; + e->SetTrusted(trusted); + e->SetComposed(aParam.mComposed); + return e.forget(); +} + +} // namespace dom +} // namespace mozilla + +using namespace mozilla; +using namespace mozilla::dom; + +already_AddRefed +NS_NewDOMSubmitEvent(EventTarget* aOwner, + nsPresContext* aPresContext, + InternalFormEvent* aEvent) +{ + RefPtr it = new SubmitEvent(aOwner, aPresContext, aEvent); + return it.forget(); +} diff --git a/dom/events/SubmitEvent.h b/dom/events/SubmitEvent.h new file mode 100644 index 0000000000..2259a5faa7 --- /dev/null +++ b/dom/events/SubmitEvent.h @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_SubmitEvent_h_ +#define mozilla_dom_SubmitEvent_h_ + +#include "mozilla/dom/Event.h" +#include "mozilla/dom/SubmitEventBinding.h" +#include "mozilla/EventForwards.h" + +class nsGenericHTMLElement; + +namespace mozilla { +namespace dom { + +class SubmitEvent final : public Event +{ +public: + SubmitEvent(EventTarget* aOwner, + nsPresContext* aPresContext, + InternalFormEvent* aEvent); + + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SubmitEvent, Event) + + // Forward to base class + NS_FORWARD_TO_EVENT + + static already_AddRefed Constructor(const GlobalObject& aGlobal, + const nsAString& aType, + const SubmitEventInit& aParam, + ErrorResult& aRv); + + ::nsGenericHTMLElement* GetSubmitter() const + { + return mSubmitter; + } + + virtual JSObject* WrapObjectInternal(JSContext* aCx, JS::Handle aGivenProto) override + { + return SubmitEventBinding::Wrap(aCx, this, aGivenProto); + } + +protected: + ~SubmitEvent() {} + +private: + RefPtr<::nsGenericHTMLElement> mSubmitter; +}; + +} // namespace dom +} // namespace mozilla + +already_AddRefed +NS_NewDOMSubmitEvent(mozilla::dom::EventTarget* aOwner, + nsPresContext* aPresContext, + mozilla::InternalFormEvent* aEvent); + +#endif // mozilla_dom_SubmitEvent_h_ diff --git a/dom/events/moz.build b/dom/events/moz.build index ae57e91fe3..859ef39c45 100644 --- a/dom/events/moz.build +++ b/dom/events/moz.build @@ -65,6 +65,7 @@ EXPORTS.mozilla.dom += [ 'ScrollAreaEvent.h', 'SimpleGestureEvent.h', 'StorageEvent.h', + 'SubmitEvent.h', 'TextClause.h', 'Touch.h', 'TouchEvent.h', @@ -115,6 +116,7 @@ UNIFIED_SOURCES += [ 'ScrollAreaEvent.cpp', 'SimpleGestureEvent.cpp', 'StorageEvent.cpp', + 'SubmitEvent.cpp', 'TextClause.cpp', 'TextComposition.cpp', 'Touch.cpp', diff --git a/dom/events/test/mochitest.ini b/dom/events/test/mochitest.ini index 27e8e7150e..7508d3c006 100644 --- a/dom/events/test/mochitest.ini +++ b/dom/events/test/mochitest.ini @@ -170,6 +170,7 @@ skip-if = toolkit == 'android' #CRASH_DUMP, RANDOM skip-if = toolkit == 'android' #TIMED_OUT [test_eventctors.html] skip-if = toolkit == 'android' #CRASH_DUMP, RANDOM +[test_submit_event.html] [test_eventhandler_scoping.html] [test_eventTimeStamp.html] [test_focus_disabled.html] diff --git a/dom/events/test/test_submit_event.html b/dom/events/test/test_submit_event.html new file mode 100644 index 0000000000..e528efbdc0 --- /dev/null +++ b/dom/events/test/test_submit_event.html @@ -0,0 +1,80 @@ + + + + Test for SubmitEvent and submitter + + + + +
+
+
+ + diff --git a/dom/webidl/SubmitEvent.webidl b/dom/webidl/SubmitEvent.webidl new file mode 100644 index 0000000000..7227b7dc61 --- /dev/null +++ b/dom/webidl/SubmitEvent.webidl @@ -0,0 +1,16 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +[Constructor(DOMString type, optional SubmitEventInit eventInitDict)] +interface SubmitEvent : Event +{ + readonly attribute HTMLElement? submitter; +}; + +dictionary SubmitEventInit : EventInit +{ + HTMLElement? submitter = null; +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index ee8b779ae8..609b00dbfc 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -412,6 +412,7 @@ WEBIDL_FILES = [ 'StorageType.webidl', 'StyleSheet.webidl', 'StyleSheetList.webidl', + 'SubmitEvent.webidl', 'SubtleCrypto.webidl', 'SVGAElement.webidl', 'SVGAngle.webidl',