tor-browser

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

ReplaceTextTransaction.h (3984B)


      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 ReplaceTextTransaction_h
      7 #define ReplaceTextTransaction_h
      8 
      9 #include "EditorBase.h"
     10 #include "EditorDOMPoint.h"
     11 #include "EditorForwards.h"
     12 #include "EditTransactionBase.h"
     13 
     14 #include "mozilla/Attributes.h"
     15 #include "mozilla/RefPtr.h"
     16 #include "mozilla/dom/Text.h"
     17 #include "nsDebug.h"
     18 #include "nsString.h"
     19 
     20 namespace mozilla {
     21 
     22 class ReplaceTextTransaction : public EditTransactionBase {
     23 protected:
     24  ReplaceTextTransaction(EditorBase& aEditorBase,
     25                         const nsAString& aStringToInsert, dom::Text& aTextNode,
     26                         uint32_t aStartOffset, uint32_t aLength)
     27      : mEditorBase(&aEditorBase),
     28        mStringToInsert(aStringToInsert),
     29        mOffset(aStartOffset) {
     30    if (aLength) {
     31      IgnoredErrorResult ignoredError;
     32      aTextNode.SubstringData(mOffset, aLength, mStringToBeReplaced,
     33                              ignoredError);
     34      NS_WARNING_ASSERTION(
     35          !ignoredError.Failed(),
     36          "Failed to initialize ReplaceTextTransaction::mStringToBeReplaced, "
     37          "but ignored");
     38    }
     39  }
     40  virtual ~ReplaceTextTransaction() = default;
     41 
     42 public:
     43  static already_AddRefed<ReplaceTextTransaction> Create(
     44      EditorBase& aEditorBase, const nsAString& aStringToInsert,
     45      dom::Text& aTextNode, uint32_t aStartOffset, uint32_t aLength);
     46 
     47  ReplaceTextTransaction() = delete;
     48  ReplaceTextTransaction(const ReplaceTextTransaction&) = delete;
     49  ReplaceTextTransaction& operator=(const ReplaceTextTransaction&) = delete;
     50 
     51  NS_DECL_ISUPPORTS_INHERITED
     52  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ReplaceTextTransaction,
     53                                           EditTransactionBase)
     54 
     55  NS_DECL_EDITTRANSACTIONBASE
     56  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(ReplaceTextTransaction)
     57 
     58  MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() final;
     59 
     60  dom::Text* GetTextNode() const;
     61 
     62  template <typename EditorDOMPointType>
     63  EditorDOMPointType SuggestPointToPutCaret() const {
     64    dom::Text* const textNode = GetTextNode();
     65    if (NS_WARN_IF(!textNode)) {
     66      return EditorDOMPointType();
     67    }
     68    return EditorDOMPointType(textNode, mOffset + mStringToInsert.Length());
     69  }
     70 
     71  friend std::ostream& operator<<(std::ostream& aStream,
     72                                  const ReplaceTextTransaction& aTransaction);
     73 
     74 protected:
     75  RefPtr<EditorBase> mEditorBase;
     76 
     77  nsString mStringToInsert;
     78  nsString mStringToBeReplaced;
     79 
     80  uint32_t mOffset;
     81 };
     82 
     83 /**
     84 * Private class for ReplaceTextTransaction when it needs to handle a
     85 * transaction of `HTMLEditor`.
     86 */
     87 class ReplaceTextInTextNodeTransaction final : public ReplaceTextTransaction {
     88 public:
     89  NS_DECL_ISUPPORTS_INHERITED
     90  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ReplaceTextInTextNodeTransaction,
     91                                           ReplaceTextTransaction)
     92 
     93  friend std::ostream& operator<<(
     94      std::ostream& aStream,
     95      const ReplaceTextInTextNodeTransaction& aTransaction);
     96 
     97 private:
     98  ReplaceTextInTextNodeTransaction(EditorBase& aEditorBase,
     99                                   const nsAString& aStringToInsert,
    100                                   dom::Text& aTextNode, uint32_t aStartOffset,
    101                                   uint32_t aLength)
    102      : ReplaceTextTransaction(aEditorBase, aStringToInsert, aTextNode,
    103                               aStartOffset, aLength),
    104        mTextNode(&aTextNode) {
    105    MOZ_ASSERT(aEditorBase.IsHTMLEditor());
    106  }
    107 
    108  virtual ~ReplaceTextInTextNodeTransaction() = default;
    109 
    110  NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(
    111      ReplaceTextInTextNodeTransaction)
    112 
    113  RefPtr<dom::Text> mTextNode;
    114 
    115  friend class ReplaceTextTransaction;
    116 };
    117 
    118 }  // namespace mozilla
    119 
    120 #endif  // #ifndef ReplaceTextTransaction_