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
|
|
|
|
|
|
|
#include "PlaceholderTxn.h"
|
2001-04-06 03:48:01 +04:00
|
|
|
#include "nsEditor.h"
|
1999-10-26 22:54:47 +04:00
|
|
|
#include "IMETextTxn.h"
|
2008-03-13 21:54:01 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2012-07-23 14:27:22 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2014-04-10 20:09:40 +04:00
|
|
|
using namespace mozilla::dom;
|
1999-03-15 03:57:32 +03:00
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
PlaceholderTxn::PlaceholderTxn() : EditAggregateTxn(),
|
|
|
|
mAbsorb(true),
|
2012-07-30 18:20:58 +04:00
|
|
|
mForwarding(nullptr),
|
|
|
|
mIMETextTxn(nullptr),
|
2011-10-17 18:59:28 +04:00
|
|
|
mCommitted(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mStartSel(nullptr),
|
2000-06-30 08:58:34 +04:00
|
|
|
mEndSel(),
|
2012-07-30 18:20:58 +04:00
|
|
|
mEditor(nullptr)
|
1999-03-15 03:57:32 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-09 01:23:18 +04:00
|
|
|
PlaceholderTxn::~PlaceholderTxn()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PlaceholderTxn)
|
|
|
|
|
2009-05-09 08:59:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PlaceholderTxn,
|
|
|
|
EditAggregateTxn)
|
|
|
|
tmp->mStartSel->DoUnlink();
|
|
|
|
tmp->mEndSel.DoUnlink();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PlaceholderTxn,
|
|
|
|
EditAggregateTxn)
|
|
|
|
tmp->mStartSel->DoTraverse(cb);
|
|
|
|
tmp->mEndSel.DoTraverse(cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PlaceholderTxn)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIAbsorbingTransaction)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(EditAggregateTxn)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(PlaceholderTxn, EditAggregateTxn)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(PlaceholderTxn, EditAggregateTxn)
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2012-07-23 14:27:22 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PlaceholderTxn::Init(nsIAtom* aName, nsSelectionState* aSelState,
|
|
|
|
nsEditor* aEditor)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aEditor && aSelState, NS_ERROR_NULL_POINTER);
|
1999-09-30 00:08:15 +04:00
|
|
|
|
|
|
|
mName = aName;
|
2000-01-31 13:30:12 +03:00
|
|
|
mStartSel = aSelState;
|
2000-06-30 08:58:34 +04:00
|
|
|
mEditor = aEditor;
|
1999-09-30 00:08:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::DoTransaction(void)
|
1999-03-15 03:57:32 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::UndoTransaction(void)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
2000-01-31 13:30:12 +03:00
|
|
|
// undo txns
|
2001-03-09 17:23:59 +03:00
|
|
|
nsresult res = EditAggregateTxn::UndoTransaction();
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mStartSel, NS_ERROR_NULL_POINTER);
|
2003-06-30 18:46:41 +04:00
|
|
|
|
2000-01-31 13:30:12 +03:00
|
|
|
// now restore selection
|
2014-11-02 15:04:13 +03:00
|
|
|
nsRefPtr<Selection> selection = mEditor->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 mStartSel->RestoreSelection(selection);
|
2000-01-31 13:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-09 17:23:59 +03:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::RedoTransaction(void)
|
2000-01-31 13:30:12 +03:00
|
|
|
{
|
|
|
|
// redo txns
|
2001-03-09 17:23:59 +03:00
|
|
|
nsresult res = EditAggregateTxn::RedoTransaction();
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2000-01-31 13:30:12 +03:00
|
|
|
// now restore selection
|
2014-11-02 15:04:13 +03:00
|
|
|
nsRefPtr<Selection> selection = mEditor->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
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::Merge(nsITransaction *aTransaction, bool *aDidMerge)
|
1999-03-15 03:57:32 +03:00
|
|
|
{
|
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
|
|
|
|
|
|
|
if (mForwarding)
|
1999-03-15 03:57:32 +03:00
|
|
|
{
|
1999-09-30 00:08:15 +04:00
|
|
|
NS_NOTREACHED("tried to merge into a placeholder that was in forwarding mode!");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-04-05 00:50:25 +04:00
|
|
|
// check to see if aTransaction is one of the editor's
|
|
|
|
// private transactions. If not, we want to avoid merging
|
|
|
|
// the foreign transaction into our placeholder since we
|
|
|
|
// don't know what it does.
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIEditorTransaction> pTxn = do_QueryInterface(aTransaction);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(pTxn, NS_OK); // it's foreign so just bail!
|
2003-04-05 00:50:25 +04:00
|
|
|
|
1999-09-30 00:08:15 +04:00
|
|
|
EditTxn *editTxn = (EditTxn*)aTransaction; //XXX: hack, not safe! need nsIEditTransaction!
|
2002-10-09 03:06:38 +04:00
|
|
|
// determine if this incoming txn is a placeholder txn
|
2013-06-12 11:00:08 +04:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryObject(editTxn);
|
2002-10-09 03:06:38 +04:00
|
|
|
|
|
|
|
// we are absorbing all txn's if mAbsorb is lit.
|
|
|
|
if (mAbsorb)
|
2015-05-28 18:58:42 +03:00
|
|
|
{
|
2014-08-29 15:43:24 +04:00
|
|
|
nsRefPtr<IMETextTxn> otherTxn = do_QueryObject(aTransaction);
|
|
|
|
if (otherTxn) {
|
1999-10-26 22:54:47 +04:00
|
|
|
// special handling for IMETextTxn's: they need to merge with any previous
|
|
|
|
// IMETextTxn in this placeholder, if possible.
|
2015-05-28 18:58:42 +03:00
|
|
|
if (!mIMETextTxn)
|
1999-10-26 22:54:47 +04:00
|
|
|
{
|
|
|
|
// this is the first IME txn in the placeholder
|
|
|
|
mIMETextTxn =otherTxn;
|
|
|
|
AppendChild(editTxn);
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
else
|
1999-10-26 22:54:47 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool didMerge;
|
2001-03-09 17:23:59 +03:00
|
|
|
mIMETextTxn->Merge(otherTxn, &didMerge);
|
1999-10-26 22:54:47 +04:00
|
|
|
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.
|
1999-10-26 22:54:47 +04:00
|
|
|
mIMETextTxn =otherTxn;
|
|
|
|
AppendChild(editTxn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-09 03:06:38 +04:00
|
|
|
else if (!plcTxn) // see bug 171243: just drop incoming placeholders on the floor.
|
|
|
|
{ // their children will be swallowed by this preexisting one.
|
1999-10-26 22:54:47 +04:00
|
|
|
AppendChild(editTxn);
|
|
|
|
}
|
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 ends.
|
2000-01-31 13:30:12 +03:00
|
|
|
// we can remeber the selection then.
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
|
|
|
else
|
1999-10-26 22:54:47 +04:00
|
|
|
{ // merge typing or IME or deletion transactions if the selection matches
|
2008-03-13 21:54:01 +03:00
|
|
|
if (((mName.get() == nsGkAtoms::TypingTxnName) ||
|
|
|
|
(mName.get() == nsGkAtoms::IMETxnName) ||
|
2015-05-28 18:58:42 +03:00
|
|
|
(mName.get() == nsGkAtoms::DeleteTxnName))
|
|
|
|
&& !mCommitted )
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
2013-06-12 11:00:08 +04:00
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryObject(editTxn);
|
|
|
|
if (plcTxn) {
|
2005-06-21 07:42:39 +04:00
|
|
|
nsCOMPtr<nsIAtom> atom;
|
|
|
|
plcTxn->GetTxnName(getter_AddRefs(atom));
|
|
|
|
if (atom && (atom == mName))
|
1999-03-15 03:57:32 +03:00
|
|
|
{
|
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;
|
2005-06-21 07:42:39 +04:00
|
|
|
plcTxn->StartSelectionEquals(&mEndSel, &isSame);
|
|
|
|
if (isSame)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mAbsorb = true; // we need to start absorbing again
|
2005-06-21 07:42:39 +04:00
|
|
|
plcTxn->ForwardEndBatchTo(this);
|
|
|
|
// AppendChild(editTxn);
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2002-03-24 01:16:54 +03:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::GetTxnDescription(nsAString& aString)
|
2001-03-09 17:23:59 +03:00
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
aString.AssignLiteral("PlaceholderTxn: ");
|
2001-03-09 17:23:59 +03:00
|
|
|
|
|
|
|
if (mName)
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
mName->ToString(name);
|
|
|
|
aString += name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-09-30 00:08:15 +04:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::GetTxnName(nsIAtom **aName)
|
|
|
|
{
|
|
|
|
return GetName(aName);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::StartSelectionEquals(nsSelectionState *aSelState, bool *aResult)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
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);
|
2000-01-31 13:30:12 +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;
|
|
|
|
}
|
|
|
|
*aResult = mStartSel->IsEqual(aSelState);
|
1999-09-30 00:08:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP PlaceholderTxn::EndPlaceHolderBatch()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mAbsorb = false;
|
2015-05-28 18:58:42 +03:00
|
|
|
|
|
|
|
if (mForwarding)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAbsorbingTransaction> plcTxn = do_QueryReferent(mForwarding);
|
|
|
|
if (plcTxn) plcTxn->EndPlaceHolderBatch();
|
|
|
|
}
|
2015-05-28 18:58:42 +03: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
|
|
|
|
|
|
|
NS_IMETHODIMP PlaceholderTxn::ForwardEndBatchTo(nsIAbsorbingTransaction *aForwardingAddress)
|
2015-05-28 18:58:42 +03:00
|
|
|
{
|
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
|
|
|
|
1999-10-26 22:54:47 +04:00
|
|
|
NS_IMETHODIMP PlaceholderTxn::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
|
|
|
|
2015-01-02 09:14:13 +03:00
|
|
|
nsresult PlaceholderTxn::RememberEndingSelection()
|
2000-01-31 13:30:12 +03:00
|
|
|
{
|
2012-07-23 14:27:22 +04:00
|
|
|
nsRefPtr<Selection> selection = mEditor->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
|
|
|
|