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/. */
5 
6 #ifndef DeleteRangeTransaction_h
7 #define DeleteRangeTransaction_h
8 
9 #include "EditAggregateTransaction.h"
10 #include "mozilla/RangeBoundary.h"
11 #include "nsCycleCollectionParticipant.h"
12 #include "nsID.h"
13 #include "nsIEditor.h"
14 #include "nsISupportsImpl.h"
15 #include "nsRange.h"
16 #include "nscore.h"
17 
18 class nsINode;
19 
20 namespace mozilla {
21 
22 class EditorBase;
23 class RangeUpdater;
24 
25 /**
26  * A transaction that deletes an entire range in the content tree
27  */
28 class DeleteRangeTransaction final : public EditAggregateTransaction {
29  protected:
30   DeleteRangeTransaction(EditorBase& aEditorBase,
31                          const nsRange& aRangeToDelete);
32 
33  public:
34   /**
35    * Creates a delete range transaction.  This never returns nullptr.
36    *
37    * @param aEditorBase         The object providing basic editing operations.
38    * @param aRangeToDelete      The range to delete.
39    */
Create(EditorBase & aEditorBase,const nsRange & aRangeToDelete)40   static already_AddRefed<DeleteRangeTransaction> Create(
41       EditorBase& aEditorBase, const nsRange& aRangeToDelete) {
42     RefPtr<DeleteRangeTransaction> transaction =
43         new DeleteRangeTransaction(aEditorBase, aRangeToDelete);
44     return transaction.forget();
45   }
46 
47   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteRangeTransaction,
48                                            EditAggregateTransaction)
49   NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
50 
51   NS_DECL_EDITTRANSACTIONBASE
52   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(DeleteRangeTransaction)
53 
54   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
55 
56  protected:
57   /**
58    * CreateTxnsToDeleteBetween() creates a DeleteTextTransaction or some
59    * DeleteNodeTransactions to remove text or nodes between aStart and aEnd
60    * and appends the created transactions to the array.
61    *
62    * @param aStart      Must be set and valid point.
63    * @param aEnd        Must be set and valid point.  Additionally, the
64    *                    container must be same as aStart's container.
65    *                    And of course, this must not be before aStart in
66    *                    the DOM tree order.
67    * @return            Returns NS_OK in most cases.
68    *                    When the arguments are invalid, returns
69    *                    NS_ERROR_INVALID_ARG.
70    *                    When mEditorBase isn't available, returns
71    *                    NS_ERROR_NOT_AVAIALBLE.
72    *                    When created DeleteTextTransaction cannot do its
73    *                    transaction, returns NS_ERROR_FAILURE.
74    *                    Note that even if one of created DeleteNodeTransaction
75    *                    cannot do its transaction, this returns NS_OK.
76    */
77   nsresult CreateTxnsToDeleteBetween(const RawRangeBoundary& aStart,
78                                      const RawRangeBoundary& aEnd);
79 
80   nsresult CreateTxnsToDeleteNodesBetween(nsRange* aRangeToDelete);
81 
82   /**
83    * CreateTxnsToDeleteContent() creates a DeleteTextTransaction to delete
84    * text between start of aPoint.GetContainer() and aPoint or aPoint and end of
85    * aPoint.GetContainer() and appends the created transaction to the array.
86    *
87    * @param aPoint      Must be set and valid point.  If the container is not
88    *                    a data node, this method does nothing.
89    * @param aAction     If nsIEditor::eNext, this method creates a transaction
90    *                    to delete text from aPoint to the end of the data node.
91    *                    Otherwise, this method creates a transaction to delete
92    *                    text from start of the data node to aPoint.
93    * @return            Returns NS_OK in most cases.
94    *                    When the arguments are invalid, returns
95    *                    NS_ERROR_INVALID_ARG.
96    *                    When mEditorBase isn't available, returns
97    *                    NS_ERROR_NOT_AVAIALBLE.
98    *                    When created DeleteTextTransaction cannot do its
99    *                    transaction, returns NS_ERROR_FAILURE.
100    *                    Note that even if no character will be deleted,
101    *                    this returns NS_OK.
102    */
103   nsresult CreateTxnsToDeleteContent(const RawRangeBoundary& aPoint,
104                                      nsIEditor::EDirection aAction);
105 
106   // The editor for this transaction.
107   RefPtr<EditorBase> mEditorBase;
108 
109   // P1 in the range.  This is only non-null until DoTransaction is called and
110   // we convert it into child transactions.
111   RefPtr<nsRange> mRangeToDelete;
112 };
113 
114 }  // namespace mozilla
115 
116 #endif  // #ifndef DeleteRangeTransaction_h
117