1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef ReplaceTextTransaction_h
7 #define ReplaceTextTransaction_h
9 #include "EditorBase.h"
10 #include "EditorDOMPoint.h"
11 #include "EditorForwards.h"
12 #include "EditTransactionBase.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/RefPtr.h"
17 #include "mozilla/dom/Text.h"
23 class ReplaceTextTransaction final
: public EditTransactionBase
{
25 ReplaceTextTransaction(EditorBase
& aEditorBase
,
26 const nsAString
& aStringToInsert
, dom::Text
& aTextNode
,
27 uint32_t aStartOffset
, uint32_t aLength
)
28 : mEditorBase(&aEditorBase
),
29 mTextNode(&aTextNode
),
30 mStringToInsert(aStringToInsert
),
31 mOffset(aStartOffset
) {
33 IgnoredErrorResult ignoredError
;
34 mTextNode
->SubstringData(mOffset
, aLength
, mStringToBeReplaced
,
37 !ignoredError
.Failed(),
38 "Failed to initialize ReplaceTextTransaction::mStringToBeReplaced, "
43 virtual ~ReplaceTextTransaction() = default;
46 static already_AddRefed
<ReplaceTextTransaction
> Create(
47 EditorBase
& aEditorBase
, const nsAString
& aStringToInsert
,
48 dom::Text
& aTextNode
, uint32_t aStartOffset
, uint32_t aLength
) {
49 MOZ_ASSERT(aLength
> 0, "Use InsertTextTransaction instead");
50 MOZ_ASSERT(!aStringToInsert
.IsEmpty(), "Use DeleteTextTransaction instead");
51 MOZ_ASSERT(aTextNode
.Length() >= aStartOffset
);
52 MOZ_ASSERT(aTextNode
.Length() >= aStartOffset
+ aLength
);
54 RefPtr
<ReplaceTextTransaction
> transaction
= new ReplaceTextTransaction(
55 aEditorBase
, aStringToInsert
, aTextNode
, aStartOffset
, aLength
);
56 return transaction
.forget();
59 ReplaceTextTransaction() = delete;
60 ReplaceTextTransaction(const ReplaceTextTransaction
&) = delete;
61 ReplaceTextTransaction
& operator=(const ReplaceTextTransaction
&) = delete;
63 NS_DECL_ISUPPORTS_INHERITED
64 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ReplaceTextTransaction
,
67 NS_DECL_EDITTRANSACTIONBASE
68 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(ReplaceTextTransaction
)
70 MOZ_CAN_RUN_SCRIPT NS_IMETHOD
RedoTransaction() final
;
72 template <typename EditorDOMPointType
>
73 EditorDOMPointType
SuggestPointToPutCaret() const {
74 if (NS_WARN_IF(!mTextNode
)) {
75 return EditorDOMPointType();
77 return EditorDOMPointType(mTextNode
, mOffset
+ mStringToInsert
.Length());
80 friend std::ostream
& operator<<(std::ostream
& aStream
,
81 const ReplaceTextTransaction
& aTransaction
);
84 RefPtr
<EditorBase
> mEditorBase
;
85 RefPtr
<dom::Text
> mTextNode
;
87 nsString mStringToInsert
;
88 nsString mStringToBeReplaced
;
93 } // namespace mozilla
95 #endif // #ifndef ReplaceTextTransaction_