2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-09 05:34:04 +04:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
1999-08-09 05:34:04 +04:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-08-09 05:34:04 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 02:53:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-26 02:53:13 +04:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 02:53:13 +04:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 02:53:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-08-09 05:34:04 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsEditorUtils.h"
|
1999-09-12 05:30:53 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2000-08-26 08:03:50 +04:00
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsLayoutCID.h"
|
1999-08-09 05:34:04 +04:00
|
|
|
|
2003-04-26 01:05:32 +04:00
|
|
|
// hooks
|
|
|
|
#include "nsIClipboardDragDropHooks.h"
|
|
|
|
#include "nsIClipboardDragDropHookList.h"
|
|
|
|
#include "nsISimpleEnumerator.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
|
1999-08-09 05:34:04 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsAutoSelectionReset
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsAutoSelectionReset::nsAutoSelectionReset(nsISelection *aSel, nsEditor *aEd) :
|
2000-03-24 03:26:47 +03:00
|
|
|
mSel(nsnull)
|
|
|
|
,mEd(nsnull)
|
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.
|
1999-08-09 05:34:04 +04:00
|
|
|
mSel = do_QueryInterface(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() :
|
|
|
|
mIter(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMIterator::ForEach(nsDomIterFunctor& functor) const
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
|
|
|
|
// iterate through dom
|
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());
|
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
functor(node);
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Next();
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsDOMSubtreeIterator::Init(nsIDOMNode* aNode)
|
|
|
|
{
|
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
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
return mIter->Init(content);
|
|
|
|
}
|
1999-08-09 05:34:04 +04:00
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* some general purpose editor utils
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsEditorUtils::IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 *aOffset)
|
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aNode || aParent, PR_FALSE);
|
2002-12-22 04:51:14 +03:00
|
|
|
if (aNode == aParent) return PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, node = do_QueryInterface(aNode);
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
res = node->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, PR_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
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
node = parent;
|
|
|
|
} while (parent);
|
|
|
|
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsEditorUtils::IsLeafNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
PRBool hasChildren = PR_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);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,
|
|
|
|
nsITransferable *aTrans)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(enumerator, PR_TRUE);
|
2003-04-26 01:05:32 +04:00
|
|
|
|
|
|
|
PRBool hasMoreHooks = PR_FALSE;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
PRBool doInsert = PR_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");
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(doInsert, PR_FALSE);
|
2003-04-26 01:05:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|