summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/JoinNodesTransaction.h
blob: a355838b036190b2e0a095791b3da36a17ccbf2f (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
/* -*- 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 JoinNodesTransaction_h
#define JoinNodesTransaction_h

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

#include "EditorDOMPoint.h"  // for EditorDOMPoint, etc.
#include "EditorForwards.h"

#include "nsCOMPtr.h"  // for nsCOMPtr
#include "nsCycleCollectionParticipant.h"
#include "nsID.h"    // for REFNSIID
#include "nscore.h"  // for NS_IMETHOD

class nsIContent;
class nsINode;

namespace mozilla {

/**
 * A transaction that joins two nodes E1 (left node) and E2 (right node) into a
 * single node E.  The children of E are the children of E1 followed by the
 * children of E2.  After DoTransaction() and RedoTransaction(), E1 is removed
 * from the content tree and E2 remains.
 */
class JoinNodesTransaction final : public EditTransactionBase {
 protected:
  JoinNodesTransaction(HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
                       nsIContent& aRightContent);

 public:
  /**
   * Creates a join node transaction.  This returns nullptr if cannot join the
   * nodes.
   *
   * @param aHTMLEditor     The provider of core editing operations.
   * @param aLeftContent    The first of two nodes to join.
   * @param aRightContent   The second of two nodes to join.
   */
  static already_AddRefed<JoinNodesTransaction> MaybeCreate(
      HTMLEditor& aHTMLEditor, nsIContent& aLeftContent,
      nsIContent& aRightContent);

  /**
   * CanDoIt() returns true if there are enough members and can join or
   * restore the nodes.  Otherwise, false.
   */
  bool CanDoIt() const;

  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(JoinNodesTransaction,
                                           EditTransactionBase)
  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;

  NS_DECL_EDITTRANSACTIONBASE
  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(JoinNodesTransaction)

  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;

  // Note that we don't support join/split node direction switching per
  // transaction.
  [[nodiscard]] SplitNodeDirection GetSplitNodeDirection() const;
  [[nodiscard]] JoinNodesDirection GetJoinNodesDirection() const;

  /**
   * GetExistingContent() and GetRemovedContent() never returns nullptr
   * unless the cycle collector clears them out.
   */
  nsIContent* GetExistingContent() const { return mKeepingContent; }
  nsIContent* GetRemovedContent() const { return mRemovedContent; }
  nsINode* GetParentNode() const { return mParentNode; }

  template <typename EditorDOMPointType>
  EditorDOMPointType CreateJoinedPoint() const {
    if (MOZ_UNLIKELY(!mKeepingContent)) {
      return EditorDOMPointType();
    }
    return EditorDOMPointType(
        mKeepingContent, std::min(mJoinedOffset, mKeepingContent->Length()));
  }

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

 protected:
  virtual ~JoinNodesTransaction() = default;

  enum class RedoingTransaction { No, Yes };
  MOZ_CAN_RUN_SCRIPT nsresult DoTransactionInternal(RedoingTransaction);

  RefPtr<HTMLEditor> mHTMLEditor;

  // The original parent of the left/right nodes.
  nsCOMPtr<nsINode> mParentNode;

  // Removed content after joined.
  nsCOMPtr<nsIContent> mRemovedContent;

  // The keeping content node which contains ex-children of mRemovedContent.
  nsCOMPtr<nsIContent> mKeepingContent;

  // Offset where the original first content is in mKeepingContent after
  // doing or redoing.
  uint32_t mJoinedOffset = 0u;
};

}  // namespace mozilla

#endif  // #ifndef JoinNodesTransaction_h