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/BasePrincipal.h" // for nsIPrincipal::IsSystemPrincipal()
8 #include "mozilla/EventForwards.h"
9 #include "mozilla/TextEvents.h"
10 #include "mozilla/dom/DataTransfer.h"
11 #include "mozilla/dom/TextEvent.h"
12 #include "nsGlobalWindowInner.h"
13 #include "nsIPrincipal.h"
14 #include "nsPresContext.h"
16 namespace mozilla::dom
{
18 TextEvent::TextEvent(EventTarget
* aOwner
, nsPresContext
* aPresContext
,
19 InternalLegacyTextEvent
* aEvent
)
20 : UIEvent(aOwner
, aPresContext
,
23 : new InternalLegacyTextEvent(false, eVoidEvent
, nullptr)) {
24 NS_ASSERTION(mEvent
->mClass
== eLegacyTextEventClass
, "event type mismatch");
25 mEventIsInternal
= !aEvent
;
28 void TextEvent::InitTextEvent(const nsAString
& typeArg
, bool canBubbleArg
,
29 bool cancelableArg
, nsGlobalWindowInner
* viewArg
,
30 const nsAString
& dataArg
) {
31 if (NS_WARN_IF(mEvent
->mFlags
.mIsBeingDispatched
)) {
35 UIEvent::InitUIEvent(typeArg
, canBubbleArg
, cancelableArg
, viewArg
, 0);
37 static_cast<InternalLegacyTextEvent
*>(mEvent
)->mData
= dataArg
;
40 void TextEvent::GetData(nsAString
& aData
,
41 nsIPrincipal
& aSubjectPrincipal
) const {
42 InternalLegacyTextEvent
* textEvent
= mEvent
->AsLegacyTextEvent();
43 MOZ_ASSERT(textEvent
);
44 if (mEvent
->IsTrusted() && !aSubjectPrincipal
.IsSystemPrincipal() &&
45 !StaticPrefs::dom_event_clipboardevents_enabled() &&
46 ExposesClipboardDataOrDataTransfer(textEvent
->mInputType
)) {
50 if (!textEvent
->mDataTransfer
) {
51 aData
= textEvent
->mData
;
54 textEvent
->mDataTransfer
->GetData(u
"text/plain"_ns
, aData
, aSubjectPrincipal
,
58 } // namespace mozilla::dom
60 using namespace mozilla
;
61 using namespace mozilla::dom
;
63 already_AddRefed
<TextEvent
> NS_NewDOMTextEvent(
64 EventTarget
* aOwner
, nsPresContext
* aPresContext
,
65 InternalLegacyTextEvent
* aEvent
) {
66 RefPtr
<TextEvent
> it
= new TextEvent(aOwner
, aPresContext
, aEvent
);