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;
29 void InputEvent::GetData(nsAString
& aData
, CallerType aCallerType
) {
30 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
31 MOZ_ASSERT(editorInputEvent
);
32 // If clipboard event is disabled, user may not want to leak clipboard
33 // information via DOM events. If so, we should return empty string instead.
34 if (mEvent
->IsTrusted() && aCallerType
!= CallerType::System
&&
35 !StaticPrefs::dom_event_clipboardevents_enabled() &&
36 ExposesClipboardDataOrDataTransfer(editorInputEvent
->mInputType
)) {
37 aData
= editorInputEvent
->mData
.IsVoid() ? VoidString() : u
""_ns
;
40 aData
= editorInputEvent
->mData
;
43 already_AddRefed
<DataTransfer
> InputEvent::GetDataTransfer(
44 CallerType aCallerType
) {
45 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
46 MOZ_ASSERT(editorInputEvent
);
47 // If clipboard event is disabled, user may not want to leak clipboard
48 // information via DOM events. If so, we should return DataTransfer which
49 // has empty string instead. The reason why we make it have empty string is,
50 // web apps may not expect that InputEvent.dataTransfer returns empty and
51 // non-null DataTransfer instance.
52 if (mEvent
->IsTrusted() && aCallerType
!= CallerType::System
&&
53 !StaticPrefs::dom_event_clipboardevents_enabled() &&
54 ExposesClipboardDataOrDataTransfer(editorInputEvent
->mInputType
)) {
55 if (!editorInputEvent
->mDataTransfer
) {
59 new DataTransfer(editorInputEvent
->mDataTransfer
->GetParentObject(),
60 editorInputEvent
->mMessage
, u
""_ns
));
62 return do_AddRef(editorInputEvent
->mDataTransfer
);
65 void InputEvent::GetInputType(nsAString
& aInputType
) {
66 InternalEditorInputEvent
* editorInputEvent
= mEvent
->AsEditorInputEvent();
67 MOZ_ASSERT(editorInputEvent
);
68 if (editorInputEvent
->mInputType
== EditorInputType::eUnknown
) {
69 aInputType
= mInputTypeValue
;
71 editorInputEvent
->GetDOMInputTypeName(aInputType
);
75 void InputEvent::GetTargetRanges(nsTArray
<RefPtr
<StaticRange
>>& aTargetRanges
) {
76 MOZ_ASSERT(aTargetRanges
.IsEmpty());
77 MOZ_ASSERT(mEvent
->AsEditorInputEvent());
78 aTargetRanges
.AppendElements(mEvent
->AsEditorInputEvent()->mTargetRanges
);
81 bool InputEvent::IsComposing() {
82 return mEvent
->AsEditorInputEvent()->mIsComposing
;
85 already_AddRefed
<InputEvent
> InputEvent::Constructor(
86 const GlobalObject
& aGlobal
, const nsAString
& aType
,
87 const InputEventInit
& aParam
) {
88 nsCOMPtr
<EventTarget
> t
= do_QueryInterface(aGlobal
.GetAsSupports());
89 RefPtr
<InputEvent
> e
= new InputEvent(t
, nullptr, nullptr);
90 bool trusted
= e
->Init(t
);
91 e
->InitUIEvent(aType
, aParam
.mBubbles
, aParam
.mCancelable
, aParam
.mView
,
93 InternalEditorInputEvent
* internalEvent
= e
->mEvent
->AsEditorInputEvent();
94 internalEvent
->mInputType
=
95 InternalEditorInputEvent::GetEditorInputType(aParam
.mInputType
);
96 if (internalEvent
->mInputType
== EditorInputType::eUnknown
) {
97 e
->mInputTypeValue
= aParam
.mInputType
;
99 internalEvent
->mData
= aParam
.mData
;
100 internalEvent
->mDataTransfer
= aParam
.mDataTransfer
;
101 internalEvent
->mTargetRanges
= aParam
.mTargetRanges
;
102 internalEvent
->mIsComposing
= aParam
.mIsComposing
;
103 e
->SetTrusted(trusted
);
104 e
->SetComposed(aParam
.mComposed
);
108 } // namespace mozilla::dom
110 using namespace mozilla
;
111 using namespace mozilla::dom
;
113 already_AddRefed
<InputEvent
> NS_NewDOMInputEvent(
114 EventTarget
* aOwner
, nsPresContext
* aPresContext
,
115 InternalEditorInputEvent
* aEvent
) {
116 RefPtr
<InputEvent
> it
= new InputEvent(aOwner
, aPresContext
, aEvent
);