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
|
|
|
|
2014-12-02 08:07:42 +03:00
|
|
|
#include "nsEditorUtils.h"
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#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 nsISupports;
|
2014-11-02 15:04:13 +03:00
|
|
|
class nsRange;
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2012-07-23 14:27:22 +04:00
|
|
|
using namespace mozilla;
|
2014-04-10 20:09:40 +04:00
|
|
|
using namespace mozilla::dom;
|
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)
|
2015-05-28 18:58:42 +03: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
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-05-22 16:58:30 +03:00
|
|
|
nsDOMIterator::nsDOMIterator(nsINode& aNode)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2015-04-24 14:27:34 +03:00
|
|
|
mIter = NS_NewContentIterator();
|
2015-05-22 16:58:30 +03:00
|
|
|
DebugOnly<nsresult> res = mIter->Init(&aNode);
|
2015-04-24 14:27:34 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(res));
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
2015-04-24 14:27:34 +03:00
|
|
|
|
2015-05-22 16:58:30 +03:00
|
|
|
nsresult
|
|
|
|
nsDOMIterator::Init(nsRange& aRange)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2015-04-24 14:27:34 +03:00
|
|
|
mIter = NS_NewContentIterator();
|
2015-05-22 16:58:30 +03:00
|
|
|
return mIter->Init(&aRange);
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
2015-04-24 14:27:34 +03:00
|
|
|
|
|
|
|
nsDOMIterator::nsDOMIterator()
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:34 +03:00
|
|
|
nsDOMIterator::~nsDOMIterator()
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
void
|
|
|
|
nsDOMIterator::AppendList(const nsBoolDomIterFunctor& functor,
|
|
|
|
nsTArray<OwningNonNull<nsINode>>& arrayOfNodes) const
|
|
|
|
{
|
|
|
|
// Iterate through dom and build list
|
|
|
|
for (; !mIter->IsDone(); mIter->Next()) {
|
|
|
|
nsCOMPtr<nsINode> node = mIter->GetCurrentNode();
|
|
|
|
|
|
|
|
if (functor(node)) {
|
|
|
|
arrayOfNodes.AppendElement(*node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 16:58:30 +03:00
|
|
|
nsDOMSubtreeIterator::nsDOMSubtreeIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMSubtreeIterator::Init(nsRange& aRange)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2015-04-24 14:27:34 +03:00
|
|
|
mIter = NS_NewContentSubtreeIterator();
|
2015-05-22 16:58:30 +03:00
|
|
|
return mIter->Init(&aRange);
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
2015-04-24 14:27:34 +03:00
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* some general purpose editor utils
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-04-28 19:34:05 +04:00
|
|
|
bool
|
|
|
|
nsEditorUtils::IsDescendantOf(nsINode* aNode, nsINode* aParent, int32_t* aOffset)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
2014-04-28 19:34:05 +04:00
|
|
|
MOZ_ASSERT(aNode && aParent);
|
|
|
|
if (aNode == aParent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (nsCOMPtr<nsINode> node = aNode; node; node = node->GetParentNode()) {
|
|
|
|
if (node->GetParentNode() == aParent) {
|
|
|
|
if (aOffset) {
|
|
|
|
*aOffset = aParent->IndexOf(node);
|
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
|
|
|
}
|
2014-04-28 19:34:05 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
|
|
|
|
2014-04-28 19:34:05 +04:00
|
|
|
bool
|
|
|
|
nsEditorUtils::IsDescendantOf(nsIDOMNode* aNode, nsIDOMNode* aParent, int32_t* aOffset)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
|
|
|
|
NS_ENSURE_TRUE(node && parent, false);
|
|
|
|
return IsDescendantOf(node, parent, aOffset);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-11-15 20:32:12 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = doc->GetDocShell();
|
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
|
2015-05-28 18:58:42 +03:00
|
|
|
nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,
|
2003-04-26 01:05:32 +04:00
|
|
|
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
|
|
|
}
|