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 CompositionTransaction_h
7 #define CompositionTransaction_h
8 
9 #include "mozilla/EditTransactionBase.h"  // base class
10 
11 #include "mozilla/EditorDOMPoint.h"  // EditorDOMPointInText
12 #include "mozilla/WeakPtr.h"
13 #include "nsCycleCollectionParticipant.h"  // various macros
14 #include "nsString.h"                      // mStringToInsert
15 
16 namespace mozilla {
17 
18 class EditorBase;
19 class TextComposition;
20 class TextRangeArray;
21 
22 namespace dom {
23 class Text;
24 }  // namespace dom
25 
26 /**
27  * CompositionTransaction stores all edit for a composition, i.e.,
28  * from compositionstart event to compositionend event.  E.g., inserting a
29  * composition string, modifying the composition string or its IME selection
30  * ranges and commit or cancel the composition.
31  */
32 class CompositionTransaction final : public EditTransactionBase,
33                                      public SupportsWeakPtr {
34  protected:
35   CompositionTransaction(EditorBase& aEditorBase,
36                          const nsAString& aStringToInsert,
37                          const EditorDOMPointInText& aPointToInsert);
38 
39  public:
40   /**
41    * Creates a composition transaction.  aEditorBase must not return from
42    * GetComposition() while calling this method.  Note that this method will
43    * update text node information of aEditorBase.mComposition.
44    *
45    * @param aEditorBase         The editor which has composition.
46    * @param aStringToInsert     The new composition string to insert.  This may
47    *                            be different from actual composition string.
48    *                            E.g., password editor can hide the character
49    *                            with a different character.
50    * @param aPointToInsert      The insertion point.
51    */
52   static already_AddRefed<CompositionTransaction> Create(
53       EditorBase& aEditorBase, const nsAString& aStringToInsert,
54       const EditorDOMPointInText& aPointToInsert);
55 
56   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CompositionTransaction,
57                                            EditTransactionBase)
58 
59   NS_DECL_ISUPPORTS_INHERITED
60 
61   NS_DECL_EDITTRANSACTIONBASE
62   NS_DECL_EDITTRANSACTIONBASE_GETASMETHODS_OVERRIDE(CompositionTransaction)
63 
64   MOZ_CAN_RUN_SCRIPT NS_IMETHOD RedoTransaction() override;
65   NS_IMETHOD Merge(nsITransaction* aOtherTransaction, bool* aDidMerge) override;
66 
67   void MarkFixed();
68 
69   MOZ_CAN_RUN_SCRIPT static nsresult SetIMESelection(
70       EditorBase& aEditorBase, dom::Text* aTextNode, uint32_t aOffsetInNode,
71       uint32_t aLengthOfCompositionString, const TextRangeArray* aRanges);
72 
73   friend std::ostream& operator<<(std::ostream& aStream,
74                                   const CompositionTransaction& aTransaction);
75 
76  private:
77   virtual ~CompositionTransaction() = default;
78 
79   MOZ_CAN_RUN_SCRIPT nsresult SetSelectionForRanges();
80 
81   // The text element to operate upon.
82   RefPtr<dom::Text> mTextNode;
83 
84   // The offsets into mTextNode where the insertion should be placed.
85   uint32_t mOffset;
86 
87   uint32_t mReplaceLength;
88 
89   // The range list.
90   RefPtr<TextRangeArray> mRanges;
91 
92   // The text to insert into mTextNode at mOffset.
93   nsString mStringToInsert;
94 
95   // The editor, which is used to get the selection controller.
96   RefPtr<EditorBase> mEditorBase;
97 
98   bool mFixed;
99 };
100 
101 }  // namespace mozilla
102 
103 #endif  // #ifndef CompositionTransaction_h
104