Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / editor / libeditor / ReplaceTextTransaction.h
blobf7bfba5dbea4b56c4ed13303ace815b0051ca731
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"
18 #include "nsDebug.h"
19 #include "nsString.h"
21 namespace mozilla {
23 class ReplaceTextTransaction final : public EditTransactionBase {
24 private:
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) {
32 if (aLength) {
33 IgnoredErrorResult ignoredError;
34 mTextNode->SubstringData(mOffset, aLength, mStringToBeReplaced,
35 ignoredError);
36 NS_WARNING_ASSERTION(
37 !ignoredError.Failed(),
38 "Failed to initialize ReplaceTextTransaction::mStringToBeReplaced, "
39 "but ignored");
43 virtual ~ReplaceTextTransaction() = default;
45 public:
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,
65 EditTransactionBase)
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);
83 private:
84 RefPtr<EditorBase> mEditorBase;
85 RefPtr<dom::Text> mTextNode;
87 nsString mStringToInsert;
88 nsString mStringToBeReplaced;
90 uint32_t mOffset;
93 } // namespace mozilla
95 #endif // #ifndef ReplaceTextTransaction_