File: MoveNodeTransaction.h

package info (click to toggle)
firefox 146.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,653,260 kB
  • sloc: cpp: 7,587,892; javascript: 6,509,455; ansic: 3,755,295; python: 1,410,813; xml: 629,201; asm: 438,677; java: 186,096; sh: 62,697; makefile: 18,086; objc: 13,087; perl: 12,811; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (258 lines) | stat: -rw-r--r-- 9,286 bytes parent folder | download | duplicates (4)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* -*- 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 MoveNodeTransaction_h
#define MoveNodeTransaction_h

#include "EditTransactionBase.h"  // for EditTransactionBase, etc.

#include "EditorDOMPoint.h"
#include "EditorForwards.h"
#include "SelectionState.h"

#include "nsCOMPtr.h"  // for nsCOMPtr
#include "nsCycleCollectionParticipant.h"
#include "nsIContent.h"       // for nsIContent
#include "nsISupportsImpl.h"  // for NS_DECL_ISUPPORTS_INHERITED

namespace mozilla {

class MoveNodeTransactionBase : public EditTransactionBase {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MoveNodeTransactionBase,
                                           EditTransactionBase)
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(MoveNodeTransactionBase)

  virtual EditorRawDOMPoint SuggestPointToPutCaret() const = 0;
  virtual EditorRawDOMPoint SuggestNextInsertionPoint() const = 0;

 protected:
  MoveNodeTransactionBase(HTMLEditor& aHTMLEditor,
                          nsIContent& aLastContentToMove,
                          const EditorRawDOMPoint& aPointToInsert);

  virtual ~MoveNodeTransactionBase() = default;

  [[nodiscard]] EditorRawDOMPoint SuggestPointToPutCaret(
      nsIContent* aLastMoveContent) const {
    if (MOZ_UNLIKELY(!mContainer || !aLastMoveContent)) {
      return EditorRawDOMPoint();
    }
    return EditorRawDOMPoint::After(*aLastMoveContent);
  }

  [[nodiscard]] EditorRawDOMPoint SuggestNextInsertionPoint(
      nsIContent* aLastMoveContent) const {
    if (MOZ_UNLIKELY(!mContainer)) {
      return EditorRawDOMPoint();
    }
    if (!mReference) {
      return EditorRawDOMPoint::AtEndOf(*aLastMoveContent);
    }
    if (MOZ_UNLIKELY(mReference->GetParentNode() != mContainer)) {
      if (MOZ_LIKELY(aLastMoveContent->GetParentNode() == mContainer)) {
        return EditorRawDOMPoint(aLastMoveContent).NextPoint();
      }
      return EditorRawDOMPoint::AtEndOf(mContainer);
    }
    return EditorRawDOMPoint(mReference);
  }

  // mContainer is new container of mContentToInsert after (re-)doing the
  // transaction.
  nsCOMPtr<nsINode> mContainer;

  // mReference is the child content where mContentToMove should be or was
  // inserted into the mContainer.  This is typically the next sibling of
  // mContentToMove after moving.
  nsCOMPtr<nsIContent> mReference;

  // mOldContainer is the original container of mContentToMove before moving.
  nsCOMPtr<nsINode> mOldContainer;

  // mOldNextSibling is the next sibling of mCOntentToMove before moving.
  nsCOMPtr<nsIContent> mOldNextSibling;

  // The editor for this transaction.
  RefPtr<HTMLEditor> mHTMLEditor;
};

/**
 * A transaction that moves a content node to a specified point.
 */
class MoveNodeTransaction final : public MoveNodeTransactionBase {
 protected:
  template <typename PT, typename CT>
  MoveNodeTransaction(HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
                      const EditorDOMPointBase<PT, CT>& aPointToInsert);

 public:
  /**
   * Create a transaction for moving aContentToMove before the child at
   * aPointToInsert.
   *
   * @param aHTMLEditor         The editor which manages the transaction.
   * @param aContentToMove      The node to be moved.
   * @param aPointToInsert      The insertion point of aContentToMove.
   * @return                    A MoveNodeTransaction which was initialized
   *                            with the arguments.
   */
  template <typename PT, typename CT>
  static already_AddRefed<MoveNodeTransaction> MaybeCreate(
      HTMLEditor& aHTMLEditor, nsIContent& aContentToMove,
      const EditorDOMPointBase<PT, CT>& aPointToInsert);

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MoveNodeTransaction,
                                           MoveNodeTransactionBase)

  NS_DECL_EDITTRANSACTIONBASE
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(MoveNodeTransaction)

  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() final;

  /**
   * SuggestPointToPutCaret() suggests a point after doing or redoing the
   * transaction.
   */
  EditorRawDOMPoint SuggestPointToPutCaret() const final {
    return MoveNodeTransactionBase::SuggestPointToPutCaret(mContentToMove);
  }

  /**
   * Suggest next insertion point if the caller wants to move another content
   * node around the insertion point.
   */
  EditorRawDOMPoint SuggestNextInsertionPoint() const final {
    return MoveNodeTransactionBase::SuggestNextInsertionPoint(mContentToMove);
  }

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const MoveNodeTransaction& aTransaction);

 protected:
  virtual ~MoveNodeTransaction() = default;

  MOZ_CAN_RUN_SCRIPT nsresult DoTransactionInternal();

  // The content which will be or was moved from mOldContainer to mContainer.
  nsCOMPtr<nsIContent> mContentToMove;
};

/**
 * A transaction that moves multiple siblings once.
 */
class MoveSiblingsTransaction final : public MoveNodeTransactionBase {
 protected:
  template <typename PT, typename CT>
  MoveSiblingsTransaction(HTMLEditor& aHTMLEditor,
                          nsIContent& aFirstContentToMove,
                          nsIContent& aLastContentToMove,
                          uint32_t aNumberOfSiblings,
                          const EditorDOMPointBase<PT, CT>& aPointToInsert);

 public:
  /**
   * Create a transaction for moving aContentToMove before the child at
   * aPointToInsert.
   *
   * @param aHTMLEditor         The editor which manages the transaction.
   * @param aFirstContentToMove The first node to be moved.
   * @param aLastContentToMove  The last node to be moved.  Its parent node
   *                            should be the parent of aFirstContentToMove and
   *                            a following sibling of aFirstContentToMove.
   * @param aNumberOfSiblings   The number of siblings to move.
   * @param aPointToInsert      The insertion point of aContentToMove.
   * @return                    A MoveSiblingsTransaction which was initialized
   *                            with the arguments.
   */
  template <typename PT, typename CT>
  static already_AddRefed<MoveSiblingsTransaction> MaybeCreate(
      HTMLEditor& aHTMLEditor, nsIContent& aFirstContentToMove,
      nsIContent& aLastContentToMove,
      const EditorDOMPointBase<PT, CT>& aPointToInsert);

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MoveSiblingsTransaction,
                                           MoveNodeTransactionBase)

  NS_DECL_EDITTRANSACTIONBASE
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(MoveSiblingsTransaction)

  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;

  /**
   * SuggestPointToPutCaret() suggests a point after doing or redoing the
   * transaction.
   */
  EditorRawDOMPoint SuggestPointToPutCaret() const final {
    return MoveNodeTransactionBase::SuggestPointToPutCaret(
        GetLastMovedContent());
  }

  /**
   * Suggest next insertion point if the caller wants to move another content
   * node around the insertion point.
   */
  EditorRawDOMPoint SuggestNextInsertionPoint() const final {
    return MoveNodeTransactionBase::SuggestNextInsertionPoint(
        GetLastMovedContent());
  }

  const nsTArray<OwningNonNull<nsIContent>>& TargetSiblings() const {
    return mSiblingsToMove;
  }

  [[nodiscard]] nsIContent* GetFirstMovedContent() const;
  [[nodiscard]] nsIContent* GetLastMovedContent() const;

  friend std::ostream& operator<<(std::ostream& aStream,
                                  const MoveSiblingsTransaction& aTransaction);

 protected:
  virtual ~MoveSiblingsTransaction() = default;

  MOZ_CAN_RUN_SCRIPT nsresult DoTransactionInternal();

  [[nodiscard]] bool IsSiblingsToMoveValid() const {
    for (const auto& content : mSiblingsToMove) {
      if (MOZ_UNLIKELY(!content.isInitialized())) {
        return false;
      }
    }
    return true;
  }

  /**
   * Remove all aClonedSiblingsToMove from the DOM.  aClonedSiblingsToMove must
   * be a clone of mSiblingsToMove in the stack.
   */
  MOZ_CAN_RUN_SCRIPT void RemoveAllSiblingsToMove(
      HTMLEditor& aHTMLEditor,
      const nsTArray<OwningNonNull<nsIContent>>& aClonedSiblingsToMove,
      AutoMoveNodeSelNotify& aNotifier) const;

  /**
   * Insert all disconnected aClonedSiblingsToMove to before aReferenceNode or
   * end of aParentNode.  Before calling this, RemoveAllSiblingsToMove()
   * should've already been called.
   */
  MOZ_CAN_RUN_SCRIPT nsresult InsertAllSiblingsToMove(
      HTMLEditor& aHTMLEditor,
      const nsTArray<OwningNonNull<nsIContent>>& aClonedSiblingsToMove,
      nsINode& aParentNode, nsIContent* aReferenceNode,
      AutoMoveNodeSelNotify& aNotifier) const;

  // The content which will be or was moved from mOldContainer to mContainer.
  AutoTArray<OwningNonNull<nsIContent>, 2> mSiblingsToMove;

  // At undoing, this is set to true and at redoing, this is set to false.
  bool mDone = false;
};

}  // namespace mozilla

#endif  // #ifndef MoveNodeTransaction_h