tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

InsertNodeTransaction.h (3213B)


      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 InsertNodeTransaction_h
      7 #define InsertNodeTransaction_h
      8 
      9 #include "EditTransactionBase.h"  // for EditTransactionBase, etc.
     10 
     11 #include "EditorDOMPoint.h"  // for EditorDOMPoint
     12 #include "EditorForwards.h"
     13 
     14 #include "nsCOMPtr.h"  // for nsCOMPtr
     15 #include "nsCycleCollectionParticipant.h"
     16 #include "nsIContent.h"       // for nsIContent
     17 #include "nsISupportsImpl.h"  // for NS_DECL_ISUPPORTS_INHERITED
     18 
     19 namespace mozilla {
     20 
     21 class EditorBase;
     22 
     23 /**
     24 * A transaction that inserts a single element
     25 */
     26 class InsertNodeTransaction final : public EditTransactionBase {
     27 protected:
     28  template <typename PT, typename CT>
     29  InsertNodeTransaction(EditorBase& aEditorBase, nsIContent& aContentToInsert,
     30                        const EditorDOMPointBase<PT, CT>& aPointToInsert);
     31 
     32 public:
     33  /**
     34   * Create a transaction for inserting aContentToInsert before the child
     35   * at aPointToInsert.
     36   *
     37   * @param aEditorBase         The editor which manages the transaction.
     38   * @param aContentToInsert    The node to be inserted.
     39   * @param aPointToInsert      The insertion point of aContentToInsert.
     40   *                            If this refers end of the container, the
     41   *                            transaction will append the node to the
     42   *                            container.  Otherwise, will insert the node
     43   *                            before child node referred by this.
     44   * @return                    A InsertNodeTranaction which was initialized
     45   *                            with the arguments.
     46   */
     47  template <typename PT, typename CT>
     48  static already_AddRefed<InsertNodeTransaction> Create(
     49      EditorBase& aEditorBase, nsIContent& aContentToInsert,
     50      const EditorDOMPointBase<PT, CT>& aPointToInsert);
     51 
     52  NS_DECL_ISUPPORTS_INHERITED
     53  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertNodeTransaction,
     54                                           EditTransactionBase)
     55 
     56  NS_DECL_EDITTRANSACTIONBASE
     57  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(InsertNodeTransaction)
     58 
     59  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
     60 
     61  /**
     62   * SuggestPointToPutCaret() suggests a point after doing or redoing the
     63   * transaction.
     64   */
     65  template <typename EditorDOMPointType>
     66  EditorDOMPointType SuggestPointToPutCaret() const {
     67    if (MOZ_UNLIKELY(!mPointToInsert.IsSet() || !mContentToInsert)) {
     68      return EditorDOMPointType();
     69    }
     70    return EditorDOMPointType::After(mContentToInsert);
     71  }
     72 
     73  friend std::ostream& operator<<(std::ostream& aStream,
     74                                  const InsertNodeTransaction& aTransaction);
     75 
     76 protected:
     77  virtual ~InsertNodeTransaction() = default;
     78 
     79  // The element to insert.
     80  nsCOMPtr<nsIContent> mContentToInsert;
     81 
     82  // The DOM point we will insert mContentToInsert.
     83  EditorDOMPoint mPointToInsert;
     84 
     85  // The editor for this transaction.
     86  RefPtr<EditorBase> mEditorBase;
     87 };
     88 
     89 }  // namespace mozilla
     90 
     91 #endif  // #ifndef InsertNodeTransaction_h