summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/DeleteRangeTransaction.h
blob: 623b5139445acd15db990a429ff9e985d90b2942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef DeleteRangeTransaction_h
#define DeleteRangeTransaction_h

#include "DeleteContentTransactionBase.h"
#include "EditAggregateTransaction.h"

#include "EditorBase.h"
#include "EditorDOMPoint.h"
#include "EditorForwards.h"

#include "mozilla/RefPtr.h"

#include "nsCycleCollectionParticipant.h"
#include "nsID.h"
#include "nsIEditor.h"
#include "nsISupportsImpl.h"
#include "nsRange.h"
#include "nscore.h"

class nsINode;

namespace mozilla {

/**
 * A transaction that deletes an entire range in the content tree
 */
class DeleteRangeTransaction final : public EditAggregateTransaction {
 protected:
  DeleteRangeTransaction(EditorBase& aEditorBase,
                         const nsRange& aRangeToDelete);

 public:
  /**
   * Creates a delete range transaction.  This never returns nullptr.
   *
   * @param aEditorBase         The object providing basic editing operations.
   * @param aRangeToDelete      The range to delete.
   */
  static already_AddRefed<DeleteRangeTransaction> Create(
      EditorBase& aEditorBase, const nsRange& aRangeToDelete) {
    RefPtr<DeleteRangeTransaction> transaction =
        new DeleteRangeTransaction(aEditorBase, aRangeToDelete);
    return transaction.forget();
  }

  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteRangeTransaction,
                                           EditAggregateTransaction)
  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;

  NS_DECL_EDITTRANSACTIONBASE
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(DeleteRangeTransaction)

  void AppendChild(DeleteContentTransactionBase& aTransaction);

  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;

  /**
   * Return a good point to put caret after calling DoTransaction().
   */
  EditorDOMPoint SuggestPointToPutCaret() const {
    return mPointToPutCaret.IsSetAndValid() ? mPointToPutCaret
                                            : EditorDOMPoint();
  }

 protected:
  /**
   * Extend the range by adding a surrounding whitespace character to the range
   * that is about to be deleted. This method depends on the pref
   * `layout.word_select.delete_space_after_doubleclick_selection`.
   *
   * Considered cases:
   *   "one [two] three" -> "one [two ]three" -> "one three"
   *   "[one] two" -> "[one ]two" -> "two"
   *   "one [two]" -> "one[ two]" -> "one"
   *   "one [two], three" -> "one[ two], three" -> "one, three"
   *   "one  [two]" -> "one [ two]" -> "one "
   *
   * @param aRange  [inout] The range that is about to be deleted.
   * @return                NS_OK, unless nsRange::SetStart / ::SetEnd fails.
   */
  nsresult MaybeExtendDeletingRangeWithSurroundingWhitespace(
      nsRange& aRange) const;

  /**
   * AppendTransactionsToDeleteIn() creates a DeleteTextTransaction or some
   * DeleteNodeTransactions to remove text or nodes between aStart and aEnd
   * and appends the created transactions to the array.
   *
   * @param aRangeToDelete      Must be positioned, valid and in same container.
   * @return            Returns NS_OK in most cases.
   *                    When the arguments are invalid, returns
   *                    NS_ERROR_INVALID_ARG.
   *                    When mEditorBase isn't available, returns
   *                    NS_ERROR_NOT_AVAILABLE.
   *                    When created DeleteTextTransaction cannot do its
   *                    transaction, returns NS_ERROR_FAILURE.
   *                    Note that even if one of created DeleteNodeTransaction
   *                    cannot do its transaction, this returns NS_OK.
   */
  nsresult AppendTransactionsToDeleteIn(
      const EditorRawDOMRange& aRangeToDelete);

  /**
   * AppendTransactionsToDeleteNodesWhoseEndBoundaryIn() creates
   * DeleteNodeTransaction instances to remove nodes whose end is in the range
   * (in other words, its end tag is in the range if it's an element) and append
   * them to the array.
   *
   * @param aRangeToDelete      Must be positioned and valid.
   */
  nsresult AppendTransactionsToDeleteNodesWhoseEndBoundaryIn(
      const EditorRawDOMRange& aRangeToDelete);

  /**
   * AppendTransactionToDeleteText() creates a DeleteTextTransaction to delete
   * text between start of aPoint.GetContainer() and aPoint or aPoint and end of
   * aPoint.GetContainer() and appends the created transaction to the array.
   *
   * @param aMaybePointInText   Must be set and valid.  If the point is not
   *                            in a text node, this method does nothing.
   * @param aAction     If nsIEditor::eNext, this method creates a transaction
   *                    to delete text from aPoint to the end of the data node.
   *                    Otherwise, this method creates a transaction to delete
   *                    text from start of the data node to aPoint.
   * @return            Returns NS_OK in most cases.
   *                    When the arguments are invalid, returns
   *                    NS_ERROR_INVALID_ARG.
   *                    When mEditorBase isn't available, returns
   *                    NS_ERROR_NOT_AVAILABLE.
   *                    When created DeleteTextTransaction cannot do its
   *                    transaction, returns NS_ERROR_FAILURE.
   *                    Note that even if no character will be deleted,
   *                    this returns NS_OK.
   */
  nsresult AppendTransactionToDeleteText(
      const EditorRawDOMPoint& aMaybePointInText,
      nsIEditor::EDirection aAction);

  // The editor for this transaction.
  RefPtr<EditorBase> mEditorBase;

  // P1 in the range.  This is only non-null until DoTransaction is called and
  // we convert it into child transactions.
  RefPtr<nsRange> mRangeToDelete;

  EditorDOMPoint mPointToPutCaret;
};

}  // namespace mozilla

#endif  // #ifndef DeleteRangeTransaction_h