2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-03-15 03:57:32 +03:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
#include "PlaceholderTransaction.h"
|
2016-07-07 09:42:08 +03:00
|
|
|
|
|
|
|
#include "CompositionTransaction.h"
|
2016-07-08 07:10:13 +03:00
|
|
|
#include "mozilla/EditorBase.h"
|
2016-07-07 09:42:08 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2017-02-09 08:26:10 +03:00
|
|
|
#include "mozilla/Move.h"
|
2008-03-13 21:54:01 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2012-07-23 14:27:22 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
namespace mozilla {
|
1999-03-15 03:57:32 +03:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
using namespace dom;
|
|
|
|
|
2017-02-09 08:26:10 +03:00
|
|
|
PlaceholderTransaction::PlaceholderTransaction(
|
2017-07-29 09:43:39 +03:00
|
|
|
EditorBase& aEditorBase, nsAtom* aName, Maybe<SelectionState>&& aSelState)
|
2017-12-18 12:08:43 +03:00
|
|
|
: mEditorBase(&aEditorBase),
|
2016-07-07 09:42:08 +03:00
|
|
|
mForwarding(nullptr),
|
|
|
|
mCompositionTransaction(nullptr),
|
2018-05-30 22:15:35 +03:00
|
|
|
mStartSel(*std::move(aSelState)),
|
2017-12-18 12:08:43 +03:00
|
|
|
mAbsorb(true),
|
2016-07-07 09:42:08 +03:00
|
|
|
mCommitted(false) {
|
2017-02-09 08:26:10 +03:00
|
|
|
mName = aName;
|
1999-03-15 03:57:32 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
PlaceholderTransaction::~PlaceholderTransaction() {}
|
2014-07-09 01:23:18 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PlaceholderTransaction)
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PlaceholderTransaction,
|
2016-07-07 11:11:30 +03:00
|
|
|
EditAggregateTransaction)
|
2017-03-21 13:00:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditorBase);
|
2017-07-29 09:43:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStartSel);
|
2016-03-05 01:11:37 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEndSel);
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PlaceholderTransaction,
|
2016-07-07 11:11:30 +03:00
|
|
|
EditAggregateTransaction)
|
2017-03-21 13:00:36 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditorBase);
|
2017-07-29 09:43:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartSel);
|
2016-03-05 01:11:37 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndSel);
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PlaceholderTransaction)
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAbsorbingTransaction)
|
2016-07-07 11:11:30 +03:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditAggregateTransaction)
|
2009-05-09 08:59:25 +04:00
|
|
|
|
2016-07-07 11:11:30 +03:00
|
|
|
NS_IMPL_ADDREF_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PlaceholderTransaction, EditAggregateTransaction)
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::DoTransaction() { return NS_OK; }
|
1999-03-15 03:57:32 +03:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::UndoTransaction() {
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:11:30 +03:00
|
|
|
// Undo transactions.
|
|
|
|
nsresult rv = EditAggregateTransaction::UndoTransaction();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2000-01-31 13:30:12 +03:00
|
|
|
// now restore selection
|
2017-03-21 13:00:36 +03:00
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2017-07-29 09:43:39 +03:00
|
|
|
return mStartSel.RestoreSelection(selection);
|
2000-01-31 13:30:12 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::RedoTransaction() {
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:11:30 +03:00
|
|
|
// Redo transactions.
|
|
|
|
nsresult rv = EditAggregateTransaction::RedoTransaction();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2000-01-31 13:30:12 +03:00
|
|
|
// now restore selection
|
2017-03-21 13:00:36 +03:00
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2003-06-30 18:46:41 +04:00
|
|
|
return mEndSel.RestoreSelection(selection);
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::Merge(nsITransaction* aTransaction, bool* aDidMerge) {
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aDidMerge && aTransaction, NS_ERROR_NULL_POINTER);
|
1999-09-30 00:08:15 +04:00
|
|
|
|
1999-03-15 03:57:32 +03:00
|
|
|
// set out param default value
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidMerge = false;
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
if (mForwarding) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"tried to merge into a placeholder that was in "
|
|
|
|
"forwarding mode!");
|
1999-09-30 00:08:15 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
// XXX: hack, not safe! need nsIEditTransaction!
|
|
|
|
EditTransactionBase* editTransactionBase = (EditTransactionBase*)aTransaction;
|
2002-10-09 03:06:38 +04:00
|
|
|
// determine if this incoming txn is a placeholder txn
|
2016-07-08 03:48:34 +03:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> absorbingTransaction =
|
|
|
|
do_QueryObject(editTransactionBase);
|
2002-10-09 03:06:38 +04:00
|
|
|
|
2016-07-08 03:48:34 +03:00
|
|
|
// We are absorbing all transactions if mAbsorb is lit.
|
|
|
|
if (mAbsorb) {
|
2016-07-07 09:42:08 +03:00
|
|
|
RefPtr<CompositionTransaction> otherTransaction =
|
|
|
|
do_QueryObject(aTransaction);
|
|
|
|
if (otherTransaction) {
|
|
|
|
// special handling for CompositionTransaction's: they need to merge with
|
|
|
|
// any previous CompositionTransaction in this placeholder, if possible.
|
|
|
|
if (!mCompositionTransaction) {
|
1999-10-26 22:54:47 +04:00
|
|
|
// this is the first IME txn in the placeholder
|
2016-07-07 09:42:08 +03:00
|
|
|
mCompositionTransaction = otherTransaction;
|
2016-07-08 03:48:34 +03:00
|
|
|
AppendChild(editTransactionBase);
|
2016-07-07 09:42:08 +03:00
|
|
|
} else {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool didMerge;
|
2016-07-07 09:42:08 +03:00
|
|
|
mCompositionTransaction->Merge(otherTransaction, &didMerge);
|
|
|
|
if (!didMerge) {
|
2007-11-10 07:24:42 +03:00
|
|
|
// it wouldn't merge. Earlier IME txn is already committed and will
|
2002-10-09 03:06:38 +04:00
|
|
|
// not absorb further IME txns. So just stack this one after it
|
|
|
|
// and remember it as a candidate for further merges.
|
2016-07-07 09:42:08 +03:00
|
|
|
mCompositionTransaction = otherTransaction;
|
2016-07-08 03:48:34 +03:00
|
|
|
AppendChild(editTransactionBase);
|
1999-10-26 22:54:47 +04:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 03:48:34 +03:00
|
|
|
} else if (!absorbingTransaction) {
|
2016-07-07 09:42:08 +03:00
|
|
|
// See bug 171243: just drop incoming placeholders on the floor.
|
|
|
|
// Their children will be swallowed by this preexisting one.
|
2016-07-08 03:48:34 +03:00
|
|
|
AppendChild(editTransactionBase);
|
1999-10-26 22:54:47 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidMerge = true;
|
2000-01-31 13:30:12 +03:00
|
|
|
// RememberEndingSelection();
|
2015-05-28 18:58:42 +03:00
|
|
|
// efficiency hack: no need to remember selection here, as we haven't yet
|
2007-11-11 05:43:32 +03:00
|
|
|
// finished the initial batch and we know we will be told when the batch
|
2000-01-31 13:30:12 +03:00
|
|
|
// ends. we can remeber the selection then.
|
2016-07-08 03:48:34 +03:00
|
|
|
} else {
|
|
|
|
// merge typing or IME or deletion transactions if the selection matches
|
2016-10-24 05:27:45 +03:00
|
|
|
if ((mName.get() == nsGkAtoms::TypingTxnName ||
|
|
|
|
mName.get() == nsGkAtoms::IMETxnName ||
|
|
|
|
mName.get() == nsGkAtoms::DeleteTxnName) &&
|
|
|
|
!mCommitted) {
|
2016-07-08 03:48:34 +03:00
|
|
|
if (absorbingTransaction) {
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> atom;
|
2016-07-08 03:48:34 +03:00
|
|
|
absorbingTransaction->GetTxnName(getter_AddRefs(atom));
|
|
|
|
if (atom && atom == mName) {
|
2005-06-21 07:42:39 +04:00
|
|
|
// check if start selection of next placeholder matches
|
|
|
|
// end selection of this placeholder
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isSame;
|
2016-07-08 03:48:34 +03:00
|
|
|
absorbingTransaction->StartSelectionEquals(&mEndSel, &isSame);
|
|
|
|
if (isSame) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mAbsorb = true; // we need to start absorbing again
|
2016-07-08 03:48:34 +03:00
|
|
|
absorbingTransaction->ForwardEndBatchTo(this);
|
|
|
|
// AppendChild(editTransactionBase);
|
2005-06-21 07:42:39 +04:00
|
|
|
// see bug 171243: we don't need to merge placeholders
|
|
|
|
// into placeholders. We just reactivate merging in the
|
|
|
|
// pre-existing placeholder and drop the new one on the floor. The
|
|
|
|
// EndPlaceHolderBatch() call on the new placeholder will be
|
|
|
|
// forwarded to this older one.
|
|
|
|
RememberEndingSelection();
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidMerge = true;
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
1999-03-15 03:57:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-30 18:46:41 +04:00
|
|
|
return NS_OK;
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
2017-10-03 01:05:19 +03:00
|
|
|
PlaceholderTransaction::GetTxnName(nsAtom** aName) { return GetName(aName); }
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
2016-07-07 12:15:53 +03:00
|
|
|
PlaceholderTransaction::StartSelectionEquals(SelectionState* aSelState,
|
2016-07-07 10:27:23 +03:00
|
|
|
bool* aResult) {
|
2000-01-31 13:30:12 +03:00
|
|
|
// determine if starting selection matches the given selection state.
|
|
|
|
// note that we only care about collapsed selections.
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aResult && aSelState, NS_ERROR_NULL_POINTER);
|
2017-07-29 09:43:39 +03:00
|
|
|
if (!mStartSel.IsCollapsed() || !aSelState->IsCollapsed()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aResult = false;
|
2000-01-31 13:30:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-07-29 09:43:39 +03:00
|
|
|
*aResult = mStartSel.IsEqual(aSelState);
|
1999-09-30 00:08:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::EndPlaceHolderBatch() {
|
2011-10-17 18:59:28 +04:00
|
|
|
mAbsorb = false;
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2016-10-24 05:27:45 +03:00
|
|
|
if (mForwarding) {
|
1999-09-30 00:08:15 +04:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryReferent(mForwarding);
|
2016-10-24 05:27:45 +03:00
|
|
|
if (plcTxn) {
|
|
|
|
plcTxn->EndPlaceHolderBatch();
|
|
|
|
}
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
2000-01-31 13:30:12 +03:00
|
|
|
// remember our selection state.
|
|
|
|
return RememberEndingSelection();
|
2003-09-08 02:05:34 +04:00
|
|
|
}
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::ForwardEndBatchTo(
|
|
|
|
nsIAbsorbingTransaction* aForwardingAddress) {
|
2003-07-25 23:06:59 +04:00
|
|
|
mForwarding = do_GetWeakReference(aForwardingAddress);
|
1999-09-30 00:08:15 +04:00
|
|
|
return NS_OK;
|
1999-03-15 03:57:32 +03:00
|
|
|
}
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTransaction::Commit() {
|
2011-10-17 18:59:28 +04:00
|
|
|
mCommitted = true;
|
1999-10-26 22:54:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
nsresult PlaceholderTransaction::RememberEndingSelection() {
|
2017-03-21 13:00:36 +03:00
|
|
|
if (NS_WARN_IF(!mEditorBase)) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Selection> selection = mEditorBase->GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-07-23 14:27:22 +04:00
|
|
|
mEndSel.SaveSelection(selection);
|
|
|
|
return NS_OK;
|
2000-01-31 13:30:12 +03:00
|
|
|
}
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2016-07-07 10:27:23 +03:00
|
|
|
} // namespace mozilla
|