mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-01 06:35:42 +00:00
388 lines
12 KiB
C++
388 lines
12 KiB
C++
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
|
*
|
|
* The contents of this file are subject to the Netscape 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/NPL/
|
|
*
|
|
* 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.
|
|
*
|
|
* The Original Code is mozilla.org code.
|
|
*
|
|
* The Initial Developer of the Original Code is
|
|
* Netscape Communications Corporation.
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
* the Initial Developer. All Rights Reserved.
|
|
*
|
|
* Contributor(s):
|
|
*
|
|
*
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
* either 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"),
|
|
* 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
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
* 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
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
*
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
#include "nsEditorUtils.h"
|
|
#include "nsIDOMDocument.h"
|
|
#include "nsIDOMRange.h"
|
|
#include "nsIContent.h"
|
|
#include "nsLayoutCID.h"
|
|
#include "nsIEnumerator.h"
|
|
#include "nsISupportsArray.h"
|
|
|
|
// hooks
|
|
#include "nsIClipboardDragDropHooks.h"
|
|
#include "nsIClipboardDragDropHookList.h"
|
|
#include "nsISimpleEnumerator.h"
|
|
#include "nsIDocShell.h"
|
|
#include "nsIDocument.h"
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
|
|
/******************************************************************************
|
|
* nsAutoSelectionReset
|
|
*****************************************************************************/
|
|
|
|
nsAutoSelectionReset::nsAutoSelectionReset(nsISelection *aSel, nsEditor *aEd) :
|
|
mSel(nsnull)
|
|
,mEd(nsnull)
|
|
{
|
|
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.
|
|
mSel = do_QueryInterface(aSel);
|
|
mEd = aEd;
|
|
if (mSel)
|
|
{
|
|
mEd->PreserveSelectionAcrossActions(mSel);
|
|
}
|
|
}
|
|
|
|
nsAutoSelectionReset::~nsAutoSelectionReset()
|
|
{
|
|
if (mSel && mEd->ArePreservingSelection()) // mSel will be null if this was nested call
|
|
{
|
|
mEd->RestorePreservedSelection(mSel);
|
|
}
|
|
}
|
|
|
|
void
|
|
nsAutoSelectionReset::Abort()
|
|
{
|
|
mEd->StopPreservingSelection();
|
|
}
|
|
|
|
|
|
/******************************************************************************
|
|
* some helper classes for iterating the dom tree
|
|
*****************************************************************************/
|
|
|
|
static NS_DEFINE_IID(kSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
|
|
static NS_DEFINE_IID(kContentIteratorCID, NS_CONTENTITERATOR_CID);
|
|
|
|
nsDOMIterator::nsDOMIterator() :
|
|
mIter(nsnull)
|
|
{
|
|
}
|
|
|
|
nsDOMIterator::~nsDOMIterator()
|
|
{
|
|
}
|
|
|
|
nsresult
|
|
nsDOMIterator::Init(nsIDOMRange* aRange)
|
|
{
|
|
nsresult res = nsComponentManager::CreateInstance(kContentIteratorCID,
|
|
nsnull,
|
|
NS_GET_IID(nsIContentIterator),
|
|
getter_AddRefs(mIter));
|
|
if (NS_FAILED(res)) return res;
|
|
return mIter->Init(aRange);
|
|
}
|
|
|
|
nsresult
|
|
nsDOMIterator::Init(nsIDOMNode* aNode)
|
|
{
|
|
nsresult res = nsComponentManager::CreateInstance(kContentIteratorCID,
|
|
nsnull,
|
|
NS_GET_IID(nsIContentIterator),
|
|
getter_AddRefs(mIter));
|
|
if (NS_FAILED(res)) return res;
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
return mIter->Init(content);
|
|
}
|
|
|
|
void
|
|
nsDOMIterator::ForEach(nsDomIterFunctor& functor) const
|
|
{
|
|
nsCOMPtr<nsIContent> content;
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
nsresult res;
|
|
|
|
// iterate through dom
|
|
while (NS_ENUMERATOR_FALSE == mIter->IsDone())
|
|
{
|
|
res = mIter->CurrentNode(getter_AddRefs(content));
|
|
if (NS_FAILED(res)) return;
|
|
node = do_QueryInterface(content);
|
|
if (!node) return;
|
|
functor(node);
|
|
res = mIter->Next();
|
|
if (NS_FAILED(res)) return;
|
|
}
|
|
}
|
|
|
|
nsresult
|
|
nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
|
|
nsCOMArray<nsIDOMNode>& arrayOfNodes) const
|
|
{
|
|
nsCOMPtr<nsIContent> content;
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
nsresult res;
|
|
|
|
// iterate through dom and build list
|
|
while (NS_ENUMERATOR_FALSE == mIter->IsDone())
|
|
{
|
|
res = mIter->CurrentNode(getter_AddRefs(content));
|
|
if (NS_FAILED(res)) return res;
|
|
node = do_QueryInterface(content);
|
|
if (!node) return NS_ERROR_NULL_POINTER;
|
|
if (functor(node))
|
|
{
|
|
arrayOfNodes.AppendObject(node);
|
|
}
|
|
res = mIter->Next();
|
|
if (NS_FAILED(res)) return res;
|
|
}
|
|
return NS_OK;
|
|
}
|
|
|
|
nsDOMSubtreeIterator::nsDOMSubtreeIterator()
|
|
{
|
|
}
|
|
|
|
nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
|
|
{
|
|
}
|
|
|
|
nsresult
|
|
nsDOMSubtreeIterator::Init(nsIDOMRange* aRange)
|
|
{
|
|
nsresult res = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
|
|
nsnull,
|
|
NS_GET_IID(nsIContentIterator),
|
|
getter_AddRefs(mIter));
|
|
if (NS_FAILED(res)) return res;
|
|
return mIter->Init(aRange);
|
|
}
|
|
|
|
nsresult
|
|
nsDOMSubtreeIterator::Init(nsIDOMNode* aNode)
|
|
{
|
|
nsresult res = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
|
|
nsnull,
|
|
NS_GET_IID(nsIContentIterator),
|
|
getter_AddRefs(mIter));
|
|
if (NS_FAILED(res)) return res;
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
return mIter->Init(content);
|
|
}
|
|
|
|
/******************************************************************************
|
|
* some general purpose editor utils
|
|
*****************************************************************************/
|
|
|
|
PRBool
|
|
nsEditorUtils::IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, PRInt32 *aOffset)
|
|
{
|
|
if (!aNode && !aParent) return PR_FALSE;
|
|
if (aNode == aParent) return PR_FALSE;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, node = do_QueryInterface(aNode);
|
|
nsresult res;
|
|
|
|
do
|
|
{
|
|
res = node->GetParentNode(getter_AddRefs(parent));
|
|
if (NS_FAILED(res)) return PR_FALSE;
|
|
if (parent.get() == aParent)
|
|
{
|
|
if (aOffset)
|
|
{
|
|
nsCOMPtr<nsIContent> pCon(do_QueryInterface(parent));
|
|
nsCOMPtr<nsIContent> cCon(do_QueryInterface(node));
|
|
if (pCon && cCon)
|
|
{
|
|
pCon->IndexOf(cCon, *aOffset);
|
|
}
|
|
}
|
|
return PR_TRUE;
|
|
}
|
|
node = parent;
|
|
} while (parent);
|
|
|
|
return PR_FALSE;
|
|
}
|
|
|
|
PRBool
|
|
nsEditorUtils::IsLeafNode(nsIDOMNode *aNode)
|
|
{
|
|
if (!aNode) return PR_FALSE;
|
|
PRBool hasChildren = PR_FALSE;
|
|
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);
|
|
if (!doc) return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsISupports> isupp;
|
|
doc->GetContainer(getter_AddRefs(isupp));
|
|
nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(isupp);
|
|
nsCOMPtr<nsIClipboardDragDropHookList> hookObj = do_GetInterface(docShell);
|
|
if (!hookObj) return NS_ERROR_FAILURE;
|
|
|
|
return hookObj->GetHookEnumerator(aResult);
|
|
}
|
|
|
|
PRBool
|
|
nsEditorHookUtils::DoAllowDragHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDragEvent)
|
|
{
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
|
if (!enumerator)
|
|
return PR_TRUE;
|
|
|
|
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 canDrag = PR_TRUE;
|
|
nsresult hookres = override->AllowStartDrag(aDragEvent, &canDrag);
|
|
NS_ASSERTION(NS_SUCCEEDED(hookres), "hook failure in AllowStartDrag");
|
|
if (!canDrag)
|
|
return PR_FALSE;
|
|
}
|
|
}
|
|
|
|
return PR_TRUE;
|
|
}
|
|
|
|
PRBool
|
|
nsEditorHookUtils::DoDragHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
|
|
nsITransferable *aTrans)
|
|
{
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
|
if (!enumerator)
|
|
return PR_TRUE;
|
|
|
|
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 canInvokeDrag = PR_TRUE;
|
|
nsresult hookResult = override->OnCopyOrDrag(aEvent, aTrans, &canInvokeDrag);
|
|
NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnCopyOrDrag");
|
|
if (!canInvokeDrag)
|
|
return PR_FALSE;
|
|
}
|
|
}
|
|
|
|
return PR_TRUE;
|
|
}
|
|
|
|
PRBool
|
|
nsEditorHookUtils::DoAllowDropHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
|
|
nsIDragSession *aSession)
|
|
{
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
|
if (!enumerator)
|
|
return PR_TRUE;
|
|
|
|
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 allowDrop = PR_TRUE;
|
|
nsresult hookResult = override->AllowDrop(aEvent, aSession, &allowDrop);
|
|
NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in AllowDrop");
|
|
if (!allowDrop)
|
|
return PR_FALSE;
|
|
}
|
|
}
|
|
|
|
return PR_TRUE;
|
|
}
|
|
|
|
PRBool
|
|
nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,
|
|
nsITransferable *aTrans)
|
|
{
|
|
nsCOMPtr<nsISimpleEnumerator> enumerator;
|
|
GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
|
|
if (!enumerator)
|
|
return PR_TRUE;
|
|
|
|
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;
|
|
nsresult hookResult = override->OnPasteOrDrop(aDropEvent, aTrans, &doInsert);
|
|
NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnPasteOrDrop");
|
|
if (!doInsert)
|
|
return PR_FALSE;
|
|
}
|
|
}
|
|
|
|
return PR_TRUE;
|
|
}
|