summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/MoveNodeTransaction.h
blob: 8ab1cd5b8b230aa4b8531741dd912f25e8eae0d9 (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
/* -*- 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 "EditorForwards.h"

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

namespace mozilla {

/**
 * A transaction that moves a content node to a specified point.
 */
class MoveNodeTransaction final : public EditTransactionBase {
 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,
                                           EditTransactionBase)

  NS_DECL_EDITTRANSACTIONBASE
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(MoveNodeTransaction)

  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;

  /**
   * SuggestPointToPutCaret() suggests a point after doing or redoing the
   * transaction.
   */
  template <typename EditorDOMPointType>
  EditorDOMPointType SuggestPointToPutCaret() const {
    if (MOZ_UNLIKELY(!mContainer || !mContentToMove)) {
      return EditorDOMPointType();
    }
    return EditorDOMPointType::After(mContentToMove);
  }

  /**
   * Suggest next insertion point if the caller wants to move another content
   * node around the insertion point.
   */
  template <typename EditorDOMPointType>
  EditorDOMPointType SuggestNextInsertionPoint() const {
    if (MOZ_UNLIKELY(!mContainer)) {
      return EditorDOMPointType();
    }
    if (!mReference) {
      return EditorDOMPointType::AtEndOf(mContainer);
    }
    if (MOZ_UNLIKELY(mReference->GetParentNode() != mContainer)) {
      if (MOZ_LIKELY(mContentToMove->GetParentNode() == mContainer)) {
        return EditorDOMPointType(mContentToMove).NextPoint();
      }
      return EditorDOMPointType::AtEndOf(mContainer);
    }
    return EditorDOMPointType(mReference);
  }

  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;

  // 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;
};

}  // namespace mozilla

#endif  // #ifndef MoveNodeTransaction_h