tor-browser

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

DeleteTextTransaction.h (4202B)


      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 DeleteTextTransaction_h
      7 #define DeleteTextTransaction_h
      8 
      9 #include "DeleteContentTransactionBase.h"
     10 
     11 #include "EditorForwards.h"
     12 
     13 #include "mozilla/dom/Text.h"
     14 
     15 #include "nsCOMPtr.h"
     16 #include "nsCycleCollectionParticipant.h"
     17 #include "nsID.h"
     18 #include "nsString.h"
     19 #include "nscore.h"
     20 
     21 namespace mozilla {
     22 
     23 /**
     24 * A transaction that removes text from a content node.
     25 */
     26 class DeleteTextTransaction : public DeleteContentTransactionBase {
     27 protected:
     28  DeleteTextTransaction(EditorBase& aEditorBase, dom::Text& aTextNode,
     29                        uint32_t aOffset, uint32_t aLengthToDelete);
     30 
     31 public:
     32  /**
     33   * Creates a delete text transaction to remove given range.  This returns
     34   * nullptr if it cannot modify the text node.
     35   *
     36   * @param aEditorBase         The provider of basic editing operations.
     37   * @param aTextNode           The content node to remove text from.
     38   * @param aOffset             The location in aElement to begin the deletion.
     39   * @param aLengthToDelete     The length to delete.
     40   */
     41  static already_AddRefed<DeleteTextTransaction> MaybeCreate(
     42      EditorBase& aEditorBase, dom::Text& aTextNode, uint32_t aOffset,
     43      uint32_t aLengthToDelete);
     44 
     45  /**
     46   * Creates a delete text transaction to remove a previous or next character.
     47   * Those methods MAY return nullptr.
     48   *
     49   * @param aEditorBase         The provider of basic editing operations.
     50   * @param aTextNode           The content node to remove text from.
     51   * @param aOffset             The location in aElement to begin the deletion.
     52   */
     53  static already_AddRefed<DeleteTextTransaction>
     54  MaybeCreateForPreviousCharacter(EditorBase& aEditorBase, dom::Text& aTextNode,
     55                                  uint32_t aOffset);
     56  static already_AddRefed<DeleteTextTransaction> MaybeCreateForNextCharacter(
     57      EditorBase& aEditorBase, dom::Text& aTextNode, uint32_t aOffset);
     58 
     59  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteTextTransaction,
     60                                           DeleteContentTransactionBase)
     61  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
     62 
     63  NS_DECL_EDITTRANSACTIONBASE
     64  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(DeleteTextTransaction)
     65 
     66  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() final;
     67 
     68  EditorDOMPoint SuggestPointToPutCaret() const final;
     69 
     70  dom::Text* GetTextNode() const;
     71  uint32_t Offset() const { return mOffset; }
     72  uint32_t LengthToDelete() const { return mLengthToDelete; }
     73 
     74  friend std::ostream& operator<<(std::ostream& aStream,
     75                                  const DeleteTextTransaction& aTransaction);
     76 
     77 protected:
     78  // The offset into the text node where the deletion is to take place.
     79  uint32_t mOffset;
     80 
     81  // The length to delete.
     82  uint32_t mLengthToDelete;
     83 
     84  // The text that was deleted.
     85  nsString mDeletedText;
     86 };
     87 
     88 /**
     89 * Private class for DeleteTextTransaction when it needs to handle a transaction
     90 * of `HTMLEditor`.
     91 */
     92 class DeleteTextFromTextNodeTransaction final : public DeleteTextTransaction {
     93 public:
     94  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(DeleteTextFromTextNodeTransaction,
     95                                           DeleteTextTransaction)
     96  NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
     97 
     98  friend std::ostream& operator<<(
     99      std::ostream& aStream,
    100      const DeleteTextFromTextNodeTransaction& aTransaction);
    101 
    102 private:
    103  DeleteTextFromTextNodeTransaction(EditorBase& aEditorBase,
    104                                    dom::Text& aTextNode, uint32_t aOffset,
    105                                    uint32_t aLengthToDelete);
    106  virtual ~DeleteTextFromTextNodeTransaction() = default;
    107 
    108  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(
    109      DeleteTextFromTextNodeTransaction)
    110 
    111  // The CharacterData node to operate upon.
    112  RefPtr<dom::Text> mTextNode;
    113 
    114  friend class DeleteTextTransaction;
    115 };
    116 
    117 }  // namespace mozilla
    118 
    119 #endif  // #ifndef DeleteTextTransaction_h