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