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 #include "PlaceholderTransaction.h"
7 
8 #include "CompositionTransaction.h"
9 #include "mozilla/EditorBase.h"
10 #include "mozilla/dom/Selection.h"
11 #include "nsGkAtoms.h"
12 #include "nsQueryObject.h"
13 
14 namespace mozilla {
15 
16 using namespace dom;
17 
PlaceholderTransaction()18 PlaceholderTransaction::PlaceholderTransaction()
19   : mAbsorb(true)
20   , mForwarding(nullptr)
21   , mCompositionTransaction(nullptr)
22   , mCommitted(false)
23   , mEditorBase(nullptr)
24 {
25 }
26 
~PlaceholderTransaction()27 PlaceholderTransaction::~PlaceholderTransaction()
28 {
29 }
30 
31 NS_IMPL_CYCLE_COLLECTION_CLASS(PlaceholderTransaction)
32 
33 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PlaceholderTransaction,
34                                                 EditAggregateTransaction)
35   if (tmp->mStartSel) {
36     ImplCycleCollectionUnlink(*tmp->mStartSel);
37   }
38   NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndSel);
39 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
40 
41 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PlaceholderTransaction,
42                                                   EditAggregateTransaction)
43   if (tmp->mStartSel) {
44     ImplCycleCollectionTraverse(cb, *tmp->mStartSel, "mStartSel", 0);
45   }
46   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndSel);
47 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
48 
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PlaceholderTransaction)49 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PlaceholderTransaction)
50   NS_INTERFACE_MAP_ENTRY(nsIAbsorbingTransaction)
51   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
52 NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
53 
54 NS_IMPL_ADDREF_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
55 NS_IMPL_RELEASE_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
56 
57 NS_IMETHODIMP
58 PlaceholderTransaction::Init(nsIAtom* aName,
59                              SelectionState* aSelState,
60                              EditorBase* aEditorBase)
61 {
62   NS_ENSURE_TRUE(aEditorBase && aSelState, NS_ERROR_NULL_POINTER);
63 
64   mName = aName;
65   mStartSel = aSelState;
66   mEditorBase = aEditorBase;
67   return NS_OK;
68 }
69 
70 NS_IMETHODIMP
DoTransaction()71 PlaceholderTransaction::DoTransaction()
72 {
73   return NS_OK;
74 }
75 
76 NS_IMETHODIMP
UndoTransaction()77 PlaceholderTransaction::UndoTransaction()
78 {
79   // Undo transactions.
80   nsresult rv = EditAggregateTransaction::UndoTransaction();
81   NS_ENSURE_SUCCESS(rv, rv);
82 
83   NS_ENSURE_TRUE(mStartSel, NS_ERROR_NULL_POINTER);
84 
85   // now restore selection
86   RefPtr<Selection> selection = mEditorBase->GetSelection();
87   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
88   return mStartSel->RestoreSelection(selection);
89 }
90 
91 NS_IMETHODIMP
RedoTransaction()92 PlaceholderTransaction::RedoTransaction()
93 {
94   // Redo transactions.
95   nsresult rv = EditAggregateTransaction::RedoTransaction();
96   NS_ENSURE_SUCCESS(rv, rv);
97 
98   // now restore selection
99   RefPtr<Selection> selection = mEditorBase->GetSelection();
100   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
101   return mEndSel.RestoreSelection(selection);
102 }
103 
104 
105 NS_IMETHODIMP
Merge(nsITransaction * aTransaction,bool * aDidMerge)106 PlaceholderTransaction::Merge(nsITransaction* aTransaction,
107                               bool* aDidMerge)
108 {
109   NS_ENSURE_TRUE(aDidMerge && aTransaction, NS_ERROR_NULL_POINTER);
110 
111   // set out param default value
112   *aDidMerge=false;
113 
114   if (mForwarding) {
115     NS_NOTREACHED("tried to merge into a placeholder that was in forwarding mode!");
116     return NS_ERROR_FAILURE;
117   }
118 
119   // check to see if aTransaction is one of the editor's
120   // private transactions. If not, we want to avoid merging
121   // the foreign transaction into our placeholder since we
122   // don't know what it does.
123 
124   nsCOMPtr<nsPIEditorTransaction> pTxn = do_QueryInterface(aTransaction);
125   NS_ENSURE_TRUE(pTxn, NS_OK); // it's foreign so just bail!
126 
127   // XXX: hack, not safe!  need nsIEditTransaction!
128   EditTransactionBase* editTransactionBase = (EditTransactionBase*)aTransaction;
129   // determine if this incoming txn is a placeholder txn
130   nsCOMPtr<nsIAbsorbingTransaction> absorbingTransaction =
131     do_QueryObject(editTransactionBase);
132 
133   // We are absorbing all transactions if mAbsorb is lit.
134   if (mAbsorb) {
135     RefPtr<CompositionTransaction> otherTransaction =
136       do_QueryObject(aTransaction);
137     if (otherTransaction) {
138       // special handling for CompositionTransaction's: they need to merge with
139       // any previous CompositionTransaction in this placeholder, if possible.
140       if (!mCompositionTransaction) {
141         // this is the first IME txn in the placeholder
142         mCompositionTransaction = otherTransaction;
143         AppendChild(editTransactionBase);
144       } else {
145         bool didMerge;
146         mCompositionTransaction->Merge(otherTransaction, &didMerge);
147         if (!didMerge) {
148           // it wouldn't merge.  Earlier IME txn is already committed and will
149           // not absorb further IME txns.  So just stack this one after it
150           // and remember it as a candidate for further merges.
151           mCompositionTransaction = otherTransaction;
152           AppendChild(editTransactionBase);
153         }
154       }
155     } else if (!absorbingTransaction) {
156       // See bug 171243: just drop incoming placeholders on the floor.
157       // Their children will be swallowed by this preexisting one.
158       AppendChild(editTransactionBase);
159     }
160     *aDidMerge = true;
161 //  RememberEndingSelection();
162 //  efficiency hack: no need to remember selection here, as we haven't yet
163 //  finished the initial batch and we know we will be told when the batch ends.
164 //  we can remeber the selection then.
165   } else {
166     // merge typing or IME or deletion transactions if the selection matches
167     if ((mName.get() == nsGkAtoms::TypingTxnName ||
168          mName.get() == nsGkAtoms::IMETxnName    ||
169          mName.get() == nsGkAtoms::DeleteTxnName) && !mCommitted) {
170       if (absorbingTransaction) {
171         nsCOMPtr<nsIAtom> atom;
172         absorbingTransaction->GetTxnName(getter_AddRefs(atom));
173         if (atom && atom == mName) {
174           // check if start selection of next placeholder matches
175           // end selection of this placeholder
176           bool isSame;
177           absorbingTransaction->StartSelectionEquals(&mEndSel, &isSame);
178           if (isSame) {
179             mAbsorb = true;  // we need to start absorbing again
180             absorbingTransaction->ForwardEndBatchTo(this);
181             // AppendChild(editTransactionBase);
182             // see bug 171243: we don't need to merge placeholders
183             // into placeholders.  We just reactivate merging in the pre-existing
184             // placeholder and drop the new one on the floor.  The EndPlaceHolderBatch()
185             // call on the new placeholder will be forwarded to this older one.
186             RememberEndingSelection();
187             *aDidMerge = true;
188           }
189         }
190       }
191     }
192   }
193   return NS_OK;
194 }
195 
196 NS_IMETHODIMP
GetTxnDescription(nsAString & aString)197 PlaceholderTransaction::GetTxnDescription(nsAString& aString)
198 {
199   aString.AssignLiteral("PlaceholderTransaction: ");
200 
201   if (mName) {
202     nsAutoString name;
203     mName->ToString(name);
204     aString += name;
205   }
206 
207   return NS_OK;
208 }
209 
210 NS_IMETHODIMP
GetTxnName(nsIAtom ** aName)211 PlaceholderTransaction::GetTxnName(nsIAtom** aName)
212 {
213   return GetName(aName);
214 }
215 
216 NS_IMETHODIMP
StartSelectionEquals(SelectionState * aSelState,bool * aResult)217 PlaceholderTransaction::StartSelectionEquals(SelectionState* aSelState,
218                                              bool* aResult)
219 {
220   // determine if starting selection matches the given selection state.
221   // note that we only care about collapsed selections.
222   NS_ENSURE_TRUE(aResult && aSelState, NS_ERROR_NULL_POINTER);
223   if (!mStartSel->IsCollapsed() || !aSelState->IsCollapsed()) {
224     *aResult = false;
225     return NS_OK;
226   }
227   *aResult = mStartSel->IsEqual(aSelState);
228   return NS_OK;
229 }
230 
231 NS_IMETHODIMP
EndPlaceHolderBatch()232 PlaceholderTransaction::EndPlaceHolderBatch()
233 {
234   mAbsorb = false;
235 
236   if (mForwarding) {
237     nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryReferent(mForwarding);
238     if (plcTxn) {
239       plcTxn->EndPlaceHolderBatch();
240     }
241   }
242   // remember our selection state.
243   return RememberEndingSelection();
244 }
245 
246 NS_IMETHODIMP
ForwardEndBatchTo(nsIAbsorbingTransaction * aForwardingAddress)247 PlaceholderTransaction::ForwardEndBatchTo(
248                           nsIAbsorbingTransaction* aForwardingAddress)
249 {
250   mForwarding = do_GetWeakReference(aForwardingAddress);
251   return NS_OK;
252 }
253 
254 NS_IMETHODIMP
Commit()255 PlaceholderTransaction::Commit()
256 {
257   mCommitted = true;
258   return NS_OK;
259 }
260 
261 nsresult
RememberEndingSelection()262 PlaceholderTransaction::RememberEndingSelection()
263 {
264   RefPtr<Selection> selection = mEditorBase->GetSelection();
265   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
266   mEndSel.SaveSelection(selection);
267   return NS_OK;
268 }
269 
270 } // namespace mozilla
271