DeleteRangeTransaction.h (5950B)
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 "DeleteContentTransactionBase.h" 10 #include "EditAggregateTransaction.h" 11 12 #include "EditorBase.h" 13 #include "EditorDOMPoint.h" 14 #include "EditorForwards.h" 15 16 #include "mozilla/RefPtr.h" 17 18 #include "nsCycleCollectionParticipant.h" 19 #include "nsID.h" 20 #include "nsIEditor.h" 21 #include "nsISupportsImpl.h" 22 #include "nsRange.h" 23 #include "nscore.h" 24 25 class nsINode; 26 27 namespace mozilla { 28 29 /** 30 * A transaction that deletes an entire range in the content tree 31 */ 32 class DeleteRangeTransaction final : public EditAggregateTransaction { 33 protected: 34 DeleteRangeTransaction(EditorBase& aEditorBase, 35 const nsRange& aRangeToDelete); 36 37 public: 38 /** 39 * Creates a delete range transaction. This never returns nullptr. 40 * 41 * @param aEditorBase The object providing basic editing operations. 42 * @param aRangeToDelete The range to delete. 43 */ 44 static already_AddRefed<DeleteRangeTransaction> Create( 45 EditorBase& aEditorBase, const nsRange& aRangeToDelete) { 46 RefPtr<DeleteRangeTransaction> transaction = 47 new DeleteRangeTransaction(aEditorBase, aRangeToDelete); 48 return transaction.forget(); 49 } 50 51 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteRangeTransaction, 52 EditAggregateTransaction) 53 NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override; 54 55 NS_DECL_EDITTRANSACTIONBASE 56 NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(DeleteRangeTransaction) 57 58 void AppendChild(DeleteContentTransactionBase& aTransaction); 59 60 MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override; 61 62 /** 63 * Return a good point to put caret after calling DoTransaction(). 64 */ 65 EditorDOMPoint SuggestPointToPutCaret() const; 66 67 protected: 68 /** 69 * Extend the range by adding a surrounding whitespace character to the range 70 * that is about to be deleted. This method depends on the pref 71 * `layout.word_select.delete_space_after_doubleclick_selection`. 72 * 73 * Considered cases: 74 * "one [two] three" -> "one [two ]three" -> "one three" 75 * "[one] two" -> "[one ]two" -> "two" 76 * "one [two]" -> "one[ two]" -> "one" 77 * "one [two], three" -> "one[ two], three" -> "one, three" 78 * "one [two]" -> "one [ two]" -> "one " 79 * 80 * @param aRange [inout] The range that is about to be deleted. 81 * @return NS_OK, unless nsRange::SetStart / ::SetEnd fails. 82 */ 83 nsresult MaybeExtendDeletingRangeWithSurroundingWhitespace( 84 nsRange& aRange) const; 85 86 /** 87 * AppendTransactionsToDeleteIn() creates a DeleteTextTransaction or some 88 * DeleteNodeTransactions to remove text or nodes between aStart and aEnd 89 * and appends the created transactions to the array. 90 * 91 * @param aRangeToDelete Must be positioned, valid and in same container. 92 * @return Returns NS_OK in most cases. 93 * When the arguments are invalid, returns 94 * NS_ERROR_INVALID_ARG. 95 * When mEditorBase isn't available, returns 96 * NS_ERROR_NOT_AVAILABLE. 97 * When created DeleteTextTransaction cannot do its 98 * transaction, returns NS_ERROR_FAILURE. 99 * Note that even if one of created DeleteNodeTransaction 100 * cannot do its transaction, this returns NS_OK. 101 */ 102 nsresult AppendTransactionsToDeleteIn( 103 const EditorRawDOMRange& aRangeToDelete); 104 105 /** 106 * AppendTransactionsToDeleteNodesWhoseEndBoundaryIn() creates 107 * DeleteNodeTransaction instances to remove nodes whose end is in the range 108 * (in other words, its end tag is in the range if it's an element) and append 109 * them to the array. 110 * 111 * @param aRangeToDelete Must be positioned and valid. 112 */ 113 nsresult AppendTransactionsToDeleteNodesWhoseEndBoundaryIn( 114 const EditorRawDOMRange& aRangeToDelete); 115 116 /** 117 * AppendTransactionToDeleteText() creates a DeleteTextTransaction to delete 118 * text between start of aPoint.GetContainer() and aPoint or aPoint and end of 119 * aPoint.GetContainer() and appends the created transaction to the array. 120 * 121 * @param aMaybePointInText Must be set and valid. If the point is not 122 * in a text node, this method does nothing. 123 * @param aAction If nsIEditor::eNext, this method creates a transaction 124 * to delete text from aPoint to the end of the data node. 125 * Otherwise, this method creates a transaction to delete 126 * text from start of the data node to aPoint. 127 * @return Returns NS_OK in most cases. 128 * When the arguments are invalid, returns 129 * NS_ERROR_INVALID_ARG. 130 * When mEditorBase isn't available, returns 131 * NS_ERROR_NOT_AVAILABLE. 132 * When created DeleteTextTransaction cannot do its 133 * transaction, returns NS_ERROR_FAILURE. 134 * Note that even if no character will be deleted, 135 * this returns NS_OK. 136 */ 137 nsresult AppendTransactionToDeleteText( 138 const EditorRawDOMPoint& aMaybePointInText, 139 nsIEditor::EDirection aAction); 140 141 // The editor for this transaction. 142 RefPtr<EditorBase> mEditorBase; 143 144 // P1 in the range. This is only non-null until DoTransaction is called and 145 // we convert it into child transactions. 146 RefPtr<nsRange> mRangeToDelete; 147 148 EditorDOMPoint mPointToPutCaret; 149 }; 150 151 } // namespace mozilla 152 153 #endif // #ifndef DeleteRangeTransaction_h