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-08-09 05:34:04 +04:00
|
|
|
|
2012-07-23 14:27:22 +04:00
|
|
|
#include "mozilla/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
1999-08-09 05:34:04 +04:00
|
|
|
#include "nsEditorUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsIClipboardDragDropHookList.h"
|
2003-04-26 01:05:32 +04:00
|
|
|
// hooks
|
|
|
|
#include "nsIClipboardDragDropHooks.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
2003-04-26 01:05:32 +04:00
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
|
|
|
|
class nsIDOMRange;
|
|
|
|
class nsISupports;
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2012-07-23 14:27:22 +04:00
|
|
|
using namespace mozilla;
|
1999-08-09 05:34:04 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsAutoSelectionReset
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2012-07-23 14:27:22 +04:00
|
|
|
nsAutoSelectionReset::nsAutoSelectionReset(Selection* aSel, nsEditor* aEd)
|
2012-07-30 18:20:58 +04:00
|
|
|
: mSel(nullptr), mEd(nullptr)
|
1999-08-09 05:34:04 +04:00
|
|
|
{
|
2000-03-24 03:26:47 +03:00
|
|
|
if (!aSel || !aEd) return; // not much we can do, bail.
|
2000-08-26 08:03:50 +04:00
|
|
|
if (aEd->ArePreservingSelection()) return; // we already have initted mSavedSel, so this must be nested call.
|
2012-07-23 14:27:22 +04:00
|
|
|
mSel = aSel;
|
2000-03-24 03:26:47 +03:00
|
|
|
mEd = aEd;
|
1999-08-09 05:34:04 +04:00
|
|
|
if (mSel)
|
|
|
|
{
|
2000-08-26 08:03:50 +04:00
|
|
|
mEd->PreserveSelectionAcrossActions(mSel);
|
1999-08-09 05:34:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoSelectionReset::~nsAutoSelectionReset()
|
|
|
|
{
|
2008-01-08 07:11:24 +03:00
|
|
|
NS_ASSERTION(!mSel || mEd, "mEd should be non-null when mSel is");
|
2000-08-26 08:03:50 +04:00
|
|
|
if (mSel && mEd->ArePreservingSelection()) // mSel will be null if this was nested call
|
1999-08-09 05:34:04 +04:00
|
|
|
{
|
2000-08-26 08:03:50 +04:00
|
|
|
mEd->RestorePreservedSelection(mSel);
|
1999-08-09 05:34:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
void
|
|
|
|
nsAutoSelectionReset::Abort()
|
|
|
|
{
|
2008-01-08 07:11:24 +03:00
|
|
|
NS_ASSERTION(!mSel || mEd, "mEd should be non-null when mSel is");
|
|
|
|
if (mSel)
|
|
|
|
mEd->StopPreservingSelection();
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* some helper classes for iterating the dom tree
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
nsDOMIterator::nsDOMIterator() :
|
2012-07-30 18:20:58 +04:00
|
|
|
mIter(nullptr)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMIterator::~nsDOMIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMIterator::Init(nsIDOMRange* aRange)
|
|
|
|
{
|
2003-07-09 00:22:56 +04:00
|
|
|
nsresult res;
|
|
|
|
mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
|
2000-08-26 08:03:50 +04:00
|
|
|
return mIter->Init(aRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMIterator::Init(nsIDOMNode* aNode)
|
|
|
|
{
|
2003-07-09 00:22:56 +04:00
|
|
|
nsresult res;
|
|
|
|
mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
|
2000-08-26 08:03:50 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
return mIter->Init(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
|
2002-11-12 22:40:11 +03:00
|
|
|
nsCOMArray<nsIDOMNode>& arrayOfNodes) const
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
|
|
|
|
// iterate through dom and build list
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!mIter->IsDone())
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
node = do_QueryInterface(mIter->GetCurrentNode());
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
if (functor(node))
|
|
|
|
{
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.AppendObject(node);
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Next();
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMSubtreeIterator::nsDOMSubtreeIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMSubtreeIterator::Init(nsIDOMRange* aRange)
|
|
|
|
{
|
2003-07-09 00:22:56 +04:00
|
|
|
nsresult res;
|
|
|
|
mIter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
|
2000-08-26 08:03:50 +04:00
|
|
|
return mIter->Init(aRange);
|
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* some general purpose editor utils
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-12-22 04:51:14 +03:00
|
|
|
nsEditorUtils::IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 *aOffset)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode || aParent, false);
|
|
|
|
if (aNode == aParent) return false;
|
2002-12-22 04:51:14 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, node = do_QueryInterface(aNode);
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
res = node->GetParentNode(getter_AddRefs(parent));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, false);
|
2003-09-27 08:18:26 +04:00
|
|
|
if (parent == aParent)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
|
|
|
if (aOffset)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> pCon(do_QueryInterface(parent));
|
|
|
|
nsCOMPtr<nsIContent> cCon(do_QueryInterface(node));
|
2003-09-27 08:18:26 +04:00
|
|
|
if (pCon)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
2003-09-27 08:18:26 +04:00
|
|
|
*aOffset = pCon->IndexOf(cCon);
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
|
|
|
node = parent;
|
|
|
|
} while (parent);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-12-22 04:51:14 +03:00
|
|
|
nsEditorUtils::IsLeafNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasChildren = false;
|
2003-07-18 18:12:51 +04:00
|
|
|
if (aNode)
|
|
|
|
aNode->HasChildNodes(&hasChildren);
|
2002-12-22 04:51:14 +03:00
|
|
|
return !hasChildren;
|
|
|
|
}
|
2003-04-26 01:05:32 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* utility methods for drag/drop/copy/paste hooks
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsEditorHookUtils::GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
|
|
|
|
nsISimpleEnumerator **aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2003-10-22 10:09:48 +04:00
|
|
|
nsCOMPtr<nsISupports> container = doc->GetContainer();
|
|
|
|
nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(container);
|
2003-04-26 01:05:32 +04:00
|
|
|
nsCOMPtr<nsIClipboardDragDropHookList> hookObj = do_GetInterface(docShell);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(hookObj, NS_ERROR_FAILURE);
|
2003-04-26 01:05:32 +04:00
|
|
|
|
|
|
|
return hookObj->GetHookEnumerator(aResult);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2003-04-26 01:05:32 +04:00
|
|
|
nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,
|
|
|
|
nsITransferable *aTrans)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(enumerator, true);
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasMoreHooks = false;
|
2003-04-26 01:05:32 +04:00
|
|
|
while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> isupp;
|
|
|
|
if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
|
|
|
|
break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
|
|
|
|
if (override)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doInsert = true;
|
2010-11-04 23:44:46 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult hookResult =
|
|
|
|
#endif
|
|
|
|
override->OnPasteOrDrop(aDropEvent, aTrans, &doInsert);
|
2003-04-26 01:05:32 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnPasteOrDrop");
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(doInsert, false);
|
2003-04-26 01:05:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-04-26 01:05:32 +04:00
|
|
|
}
|