2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef nsEditorUtils_h__
|
|
|
|
#define nsEditorUtils_h__
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsEditor.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIEditor.h"
|
2012-07-12 23:33:42 -07:00
|
|
|
#include "nscore.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-12 23:33:42 -07:00
|
|
|
class nsIAtom;
|
|
|
|
class nsIContentIterator;
|
|
|
|
class nsIDOMDocument;
|
|
|
|
class nsIDOMRange;
|
|
|
|
class nsISelection;
|
|
|
|
template <class E> class nsCOMArray;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for batching a collection of txns inside a
|
|
|
|
* placeholder txn.
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoPlaceHolderBatch
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIEditor> mEd;
|
|
|
|
public:
|
|
|
|
nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd))
|
|
|
|
{ if (mEd) mEd->BeginPlaceHolderTransaction(atom); }
|
|
|
|
~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for batching a collection of txns.
|
|
|
|
* Note: I changed this to use placeholder batching so that we get
|
|
|
|
* proper selection save/restore across undo/redo.
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoEditBatch : public nsAutoPlaceHolderBatch
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2012-07-30 07:20:58 -07:00
|
|
|
nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nullptr) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsAutoEditBatch() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for saving/restoring selection. Note that this
|
|
|
|
* assumes that the nodes involved are still around afterwards!
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoSelectionReset
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/** ref-counted reference to the selection that we are supposed to restore */
|
2012-07-23 03:27:22 -07:00
|
|
|
nsRefPtr<mozilla::Selection> mSel;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsEditor *mEd; // non-owning ref to nsEditor
|
|
|
|
|
|
|
|
public:
|
|
|
|
/** constructor responsible for remembering all state needed to restore aSel */
|
2012-07-23 03:27:22 -07:00
|
|
|
nsAutoSelectionReset(mozilla::Selection* aSel, nsEditor* aEd);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/** destructor restores mSel to its former state */
|
|
|
|
~nsAutoSelectionReset();
|
|
|
|
|
|
|
|
/** Abort: cancel selection saver */
|
|
|
|
void Abort();
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for StartOperation()/EndOperation() sandwich
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoRules
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2012-08-12 11:28:26 -07:00
|
|
|
nsAutoRules(nsEditor *ed, EditAction action,
|
2012-05-05 11:52:29 -07:00
|
|
|
nsIEditor::EDirection aDirection) :
|
2011-10-17 07:59:28 -07:00
|
|
|
mEd(ed), mDoNothing(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mEd && !mEd->mAction) // mAction will already be set if this is nested call
|
|
|
|
{
|
|
|
|
mEd->StartOperation(action, aDirection);
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
else mDoNothing = true; // nested calls will end up here
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
~nsAutoRules()
|
|
|
|
{
|
|
|
|
if (mEd && !mDoNothing)
|
|
|
|
{
|
|
|
|
mEd->EndOperation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsEditor *mEd;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mDoNothing;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for turning off active selection adjustment
|
|
|
|
* by low level transactions
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoTxnsConserveSelection
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mEd)
|
|
|
|
{
|
|
|
|
mOldState = mEd->GetShouldTxnSetSelection();
|
2011-10-17 07:59:28 -07:00
|
|
|
mEd->SetShouldTxnSetSelection(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsAutoTxnsConserveSelection()
|
|
|
|
{
|
|
|
|
if (mEd)
|
|
|
|
{
|
|
|
|
mEd->SetShouldTxnSetSelection(mOldState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsEditor *mEd;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mOldState;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************
|
|
|
|
* stack based helper class for batching reflow and paint requests.
|
|
|
|
*/
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsAutoUpdateViewBatch
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mEd, "null mEd pointer!");
|
|
|
|
|
|
|
|
if (mEd)
|
|
|
|
mEd->BeginUpdateViewBatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsAutoUpdateViewBatch()
|
|
|
|
{
|
|
|
|
if (mEd)
|
|
|
|
mEd->EndUpdateViewBatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsEditor *mEd;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* some helper classes for iterating the dom tree
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
class nsBoolDomIterFunctor
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool operator()(nsIDOMNode* aNode)=0;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsDOMIterator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDOMIterator();
|
|
|
|
virtual ~nsDOMIterator();
|
|
|
|
|
|
|
|
nsresult Init(nsIDOMRange* aRange);
|
|
|
|
nsresult Init(nsIDOMNode* aNode);
|
|
|
|
nsresult AppendList(nsBoolDomIterFunctor& functor,
|
|
|
|
nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsIContentIterator> mIter;
|
|
|
|
};
|
|
|
|
|
2013-04-11 20:20:27 -07:00
|
|
|
class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsDOMSubtreeIterator();
|
|
|
|
virtual ~nsDOMSubtreeIterator();
|
|
|
|
|
|
|
|
nsresult Init(nsIDOMRange* aRange);
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsTrivialFunctor : public nsBoolDomIterFunctor
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool operator()(nsIDOMNode* aNode) // used to build list of all nodes iterator covers
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* general dom point utility struct
|
|
|
|
*****************************************************************************/
|
2013-04-11 20:20:27 -07:00
|
|
|
struct MOZ_STACK_CLASS DOMPoint
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t offset;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
DOMPoint() : node(0),offset(0) {}
|
2012-08-22 08:56:38 -07:00
|
|
|
DOMPoint(nsIDOMNode *aNode, int32_t aOffset) :
|
2007-03-22 10:30:00 -07:00
|
|
|
node(aNode),offset(aOffset) {}
|
2012-08-22 08:56:38 -07:00
|
|
|
void SetPoint(nsIDOMNode *aNode, int32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
node = aNode; offset = aOffset;
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void GetPoint(nsCOMPtr<nsIDOMNode> &aNode, int32_t &aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aNode = node; aOffset = offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class nsEditorUtils
|
|
|
|
{
|
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset = 0);
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool IsLeafNode(nsIDOMNode *aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class nsIDOMEvent;
|
|
|
|
class nsISimpleEnumerator;
|
2012-07-12 23:33:42 -07:00
|
|
|
class nsITransferable;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsEditorHookUtils
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsITransferable *aTrans);
|
|
|
|
private:
|
|
|
|
static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
|
|
|
|
nsISimpleEnumerator **aEnumerator);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsEditorUtils_h__
|