tor-browser

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

InsertTextTransaction.h (4193B)


      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 InsertTextTransaction_h
      7 #define InsertTextTransaction_h
      8 
      9 #include "EditTransactionBase.h"  // base class
     10 
     11 #include "EditorDOMPoint.h"
     12 #include "EditorForwards.h"
     13 
     14 #include "mozilla/dom/Text.h"
     15 #include "nsCycleCollectionParticipant.h"  // various macros
     16 #include "nsID.h"                          // NS_INLINE_DECL_STATIC_IID
     17 #include "nsISupportsImpl.h"               // NS_DECL_ISUPPORTS_INHERITED
     18 #include "nsString.h"                      // nsString members
     19 #include "nscore.h"                        // NS_IMETHOD, nsAString
     20 
     21 namespace mozilla {
     22 
     23 /**
     24 * A transaction that inserts text into a content node.
     25 */
     26 class InsertTextTransaction : public EditTransactionBase {
     27 protected:
     28  InsertTextTransaction(EditorBase& aEditorBase,
     29                        const nsAString& aStringToInsert,
     30                        const EditorDOMPointInText& aPointToInsert);
     31 
     32 public:
     33  /**
     34   * Creates new InsertTextTransaction instance.  This never returns nullptr.
     35   *
     36   * @param aEditorBase     The editor which manages the transaction.
     37   * @param aPointToInsert  The insertion point.
     38   * @param aStringToInsert The new string to insert.
     39   */
     40  static already_AddRefed<InsertTextTransaction> Create(
     41      EditorBase& aEditorBase, const nsAString& aStringToInsert,
     42      const EditorDOMPointInText& aPointToInsert);
     43 
     44  NS_DECL_ISUPPORTS_INHERITED
     45  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertTextTransaction,
     46                                           EditTransactionBase)
     47 
     48  NS_DECL_EDITTRANSACTIONBASE
     49  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(InsertTextTransaction)
     50 
     51  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
     52  NS_IMETHOD Merge(nsITransaction* aOtherTransaction, bool* aDidMerge) override;
     53 
     54  /**
     55   * Return the string data associated with this transaction.
     56   */
     57  const nsString& GetData() const { return mStringToInsert; }
     58 
     59  /**
     60   * Return the `Text` node to insert text (or delete text from).
     61   */
     62  dom::Text* GetTextNode() const;
     63 
     64  template <typename EditorDOMPointType>
     65  EditorDOMPointType SuggestPointToPutCaret() const {
     66    dom::Text* const textNode = GetTextNode();
     67    if (NS_WARN_IF(!textNode)) {
     68      return EditorDOMPointType();
     69    }
     70    return EditorDOMPointType(textNode, mOffset + mStringToInsert.Length());
     71  }
     72 
     73  friend std::ostream& operator<<(std::ostream& aStream,
     74                                  const InsertTextTransaction& aTransaction);
     75 
     76 protected:
     77  virtual ~InsertTextTransaction() = default;
     78 
     79  // Return true if aOtherTransaction immediately follows this transaction.
     80  bool IsSequentialInsert(InsertTextTransaction& aOtherTransaction) const;
     81 
     82  // The editor, which we'll need to get the selection.
     83  RefPtr<EditorBase> mEditorBase;
     84  // The text to insert into mTextNode at mOffset.
     85  nsString mStringToInsert;
     86  // The offset into mTextNode where the insertion is to take place.
     87  uint32_t mOffset;
     88 };
     89 
     90 /**
     91 * Private class for InsertTextTransaction when it needs to handle a transaction
     92 * of `HTMLEditor`.
     93 */
     94 class InsertTextIntoTextNodeTransaction final : public InsertTextTransaction {
     95 public:
     96  NS_DECL_ISUPPORTS_INHERITED
     97  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(InsertTextIntoTextNodeTransaction,
     98                                           InsertTextTransaction)
     99 
    100  friend std::ostream& operator<<(
    101      std::ostream& aStream,
    102      const InsertTextIntoTextNodeTransaction& aTransaction);
    103 
    104 private:
    105  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(
    106      InsertTextIntoTextNodeTransaction)
    107 
    108  InsertTextIntoTextNodeTransaction(EditorBase& aEditorBase,
    109                                    const nsAString& aStringToInsert,
    110                                    const EditorDOMPointInText& aPointToInsert);
    111  virtual ~InsertTextIntoTextNodeTransaction() = default;
    112 
    113  RefPtr<dom::Text> mTextNode;
    114 
    115  friend class InsertTextTransaction;
    116 };
    117 
    118 }  // namespace mozilla
    119 
    120 #endif  // #ifndef InsertTextTransaction_h