1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/InputEvent.h"
8 #include "mozilla/TextEvents.h"
9 #include "mozilla/StaticPrefs_dom.h"
12 namespace mozilla::dom
{
14 InputEvent::InputEvent(EventTarget
* aOwner
, nsPresContext
* aPresContext
,
15 InternalEditorInputEvent
* aEvent
)
16 : UIEvent(aOwner
, aPresContext
,
19 : new InternalEditorInputEvent(false, eVoidEvent
, nullptr)) {
20 NS_ASSERTION(mEvent
->mClass
== eEditorInputEventClass
, "event type mismatch");
23 mEventIsInternal
= false;
25 mEventIsInternal
= true;
26 mEvent
->mTime
= PR_Now();
30 void InputEvent::GetData(nsAString
& aData
, CallerType aCallerType
) {
31 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
32 MOZ_ASSERT(editorInputEvent
);
33 // If clipboard event is disabled, user may not want to leak clipboard
34 // information via DOM events. If so, we should return empty string instead.
35 if (mEvent
->IsTrusted() && aCallerType
!= CallerType::System
&&
36 !StaticPrefs::dom_event_clipboardevents_enabled() &&
37 ExposesClipboardDataOrDataTransfer(editorInputEvent
->mInputType
)) {
38 aData
= editorInputEvent
->mData
.IsVoid() ? VoidString() : u
""_ns
;
41 aData
= editorInputEvent
->mData
;
44 already_AddRefed
<DataTransfer
> InputEvent::GetDataTransfer(
45 CallerType aCallerType
) {
46 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
47 MOZ_ASSERT(editorInputEvent
);
48 // If clipboard event is disabled, user may not want to leak clipboard
49 // information via DOM events. If so, we should return DataTransfer which
50 // has empty string instead. The reason why we make it have empty string is,
51 // web apps may not expect that InputEvent.dataTransfer returns empty and
52 // non-null DataTransfer instance.
53 if (mEvent
->IsTrusted() && aCallerType
!= CallerType::System
&&
54 !StaticPrefs::dom_event_clipboardevents_enabled() &&
55 ExposesClipboardDataOrDataTransfer(editorInputEvent
->mInputType
)) {
56 if (!editorInputEvent
->mDataTransfer
) {
60 new DataTransfer(editorInputEvent
->mDataTransfer
->GetParentObject(),
61 editorInputEvent
->mMessage
, u
""_ns
));
63 return do_AddRef(editorInputEvent
->mDataTransfer
);
66 void InputEvent::GetInputType(nsAString
& aInputType
) {
67 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
68 MOZ_ASSERT(editorInputEvent
);
69 if (editorInputEvent
->mInputType
== EditorInputType::eUnknown
) {
70 aInputType
= mInputTypeValue
;
72 editorInputEvent
->GetDOMInputTypeName(aInputType
);
76 void InputEvent::GetTargetRanges(nsTArray
<RefPtr
<StaticRange
>>& aTargetRanges
) {
77 MOZ_ASSERT(aTargetRanges
.IsEmpty());
78 MOZ_ASSERT(mEvent
->AsEditorInputEvent());
79 aTargetRanges
.AppendElements(mEvent
->AsEditorInputEvent()->mTargetRanges
);
82 bool InputEvent::IsComposing() {
83 return mEvent
->AsEditorInputEvent()->mIsComposing
;
86 already_AddRefed
<InputEvent
> InputEvent::Constructor(
87 const GlobalObject
& aGlobal
, const nsAString
& aType
,
88 const InputEventInit
& aParam
) {
89 nsCOMPtr
<EventTarget
> t
= do_QueryInterface(aGlobal
.GetAsSupports());
90 RefPtr
<InputEvent
> e
= new InputEvent(t
, nullptr, nullptr);
91 bool trusted
= e
->Init(t
);
92 e
->InitUIEvent(aType
, aParam
.mBubbles
, aParam
.mCancelable
, aParam
.mView
,
94 InternalEditorInputEvent
* internalEvent
= e
->mEvent
->AsEditorInputEvent();
95 internalEvent
->mInputType
=
96 InternalEditorInputEvent::GetEditorInputType(aParam
.mInputType
);
97 if (internalEvent
->mInputType
== EditorInputType::eUnknown
) {
98 e
->mInputTypeValue
= aParam
.mInputType
;
100 internalEvent
->mData
= aParam
.mData
;
101 internalEvent
->mDataTransfer
= aParam
.mDataTransfer
;
102 internalEvent
->mTargetRanges
= aParam
.mTargetRanges
;
103 internalEvent
->mIsComposing
= aParam
.mIsComposing
;
104 e
->SetTrusted(trusted
);
105 e
->SetComposed(aParam
.mComposed
);
109 } // namespace mozilla::dom
111 using namespace mozilla
;
112 using namespace mozilla::dom
;
114 already_AddRefed
<InputEvent
> NS_NewDOMInputEvent(
115 EventTarget
* aOwner
, nsPresContext
* aPresContext
,
116 InternalEditorInputEvent
* aEvent
) {
117 RefPtr
<InputEvent
> it
= new InputEvent(aOwner
, aPresContext
, aEvent
);