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
|
|
|
|
2016-07-07 05:49:42 +03:00
|
|
|
#include "EditorUtils.h"
|
2014-12-02 08:07:42 +03:00
|
|
|
|
2015-08-05 15:28:27 +03:00
|
|
|
#include "mozilla/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
|
|
|
|
2016-07-07 07:27:31 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
1999-08-09 05:34:04 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
2016-07-07 07:27:31 +03:00
|
|
|
* AutoSelectionRestorer
|
1999-08-09 05:34:04 +04:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer::AutoSelectionRestorer(
|
|
|
|
Selection* aSelection,
|
|
|
|
nsEditor* aEditor
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
|
|
|
|
: mEditor(nullptr)
|
2015-05-28 18:58:42 +03:00
|
|
|
{
|
2015-08-25 21:04:12 +03:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2016-07-07 07:27:31 +03:00
|
|
|
if (NS_WARN_IF(!aSelection) || NS_WARN_IF(!aEditor)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aEditor->ArePreservingSelection()) {
|
|
|
|
// We already have initialized mSavedSel, so this must be nested call.
|
|
|
|
return;
|
1999-08-09 05:34:04 +04:00
|
|
|
}
|
2016-07-07 07:27:31 +03:00
|
|
|
mSelection = aSelection;
|
|
|
|
mEditor = aEditor;
|
|
|
|
mEditor->PreserveSelectionAcrossActions(mSelection);
|
1999-08-09 05:34:04 +04:00
|
|
|
}
|
|
|
|
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer::~AutoSelectionRestorer()
|
1999-08-09 05:34:04 +04:00
|
|
|
{
|
2016-07-07 07:27:31 +03:00
|
|
|
NS_ASSERTION(!mSelection || mEditor,
|
|
|
|
"mEditor should be non-null when mSelection is");
|
|
|
|
// mSelection will be null if this was nested call.
|
|
|
|
if (mSelection && mEditor->ArePreservingSelection()) {
|
|
|
|
mEditor->RestorePreservedSelection(mSelection);
|
1999-08-09 05:34:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
void
|
2016-07-07 07:27:31 +03:00
|
|
|
AutoSelectionRestorer::Abort()
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2016-07-07 07:27:31 +03:00
|
|
|
NS_ASSERTION(!mSelection || mEditor,
|
|
|
|
"mEditor should be non-null when mSelection is");
|
|
|
|
if (mSelection) {
|
|
|
|
mEditor->StopPreservingSelection();
|
|
|
|
}
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* some helper classes for iterating the dom tree
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2016-06-23 11:30:39 +03:00
|
|
|
DOMIterator::DOMIterator(nsINode& aNode MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2015-08-25 21:04:12 +03:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
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
|
2016-06-23 11:30:39 +03:00
|
|
|
DOMIterator::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
|
|
|
|
2016-06-23 11:30:39 +03:00
|
|
|
DOMIterator::DOMIterator(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
2015-08-25 21:04:12 +03:00
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
2000-08-26 08:03:50 +04:00
|
|
|
}
|
|
|
|
|
2016-06-23 11:30:39 +03:00
|
|
|
DOMIterator::~DOMIterator()
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
void
|
2016-06-23 11:39:47 +03:00
|
|
|
DOMIterator::AppendList(const BoolDomIterFunctor& functor,
|
2016-06-23 11:30:39 +03:00
|
|
|
nsTArray<OwningNonNull<nsINode>>& arrayOfNodes) const
|
2015-04-24 14:27:35 +03:00
|
|
|
{
|
|
|
|
// Iterate through dom and build list
|
|
|
|
for (; !mIter->IsDone(); mIter->Next()) {
|
|
|
|
nsCOMPtr<nsINode> node = mIter->GetCurrentNode();
|
|
|
|
|
|
|
|
if (functor(node)) {
|
|
|
|
arrayOfNodes.AppendElement(*node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 11:25:03 +03:00
|
|
|
DOMSubtreeIterator::DOMSubtreeIterator(
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_IN_IMPL)
|
2016-06-23 11:30:39 +03:00
|
|
|
: DOMIterator(MOZ_GUARD_OBJECT_NOTIFIER_ONLY_PARAM_TO_PARENT)
|
2015-05-22 16:58:30 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-06-23 11:25:03 +03:00
|
|
|
DOMSubtreeIterator::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
|
|
|
|
2016-06-23 11:25:03 +03:00
|
|
|
DOMSubtreeIterator::~DOMSubtreeIterator()
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* some general purpose editor utils
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2014-04-28 19:34:05 +04:00
|
|
|
bool
|
2016-07-07 05:49:42 +03:00
|
|
|
EditorUtils::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
|
2016-07-07 05:49:42 +03:00
|
|
|
EditorUtils::IsDescendantOf(nsIDOMNode* aNode,
|
|
|
|
nsIDOMNode* aParent,
|
|
|
|
int32_t* aOffset)
|
2014-04-28 19:34:05 +04:00
|
|
|
{
|
|
|
|
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
|
2016-07-07 05:49:42 +03:00
|
|
|
EditorUtils::IsLeafNode(nsIDOMNode* aNode)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
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
|
2016-06-23 10:59:15 +03:00
|
|
|
EditorHookUtils::GetHookEnumeratorFromDocument(nsIDOMDocument* aDoc,
|
|
|
|
nsISimpleEnumerator** aResult)
|
2003-04-26 01:05:32 +04:00
|
|
|
{
|
|
|
|
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
|
2016-06-23 10:59:15 +03:00
|
|
|
EditorHookUtils::DoInsertionHook(nsIDOMDocument* aDoc,
|
|
|
|
nsIDOMEvent* aDropEvent,
|
|
|
|
nsITransferable *aTrans)
|
2003-04-26 01:05:32 +04:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2016-06-23 10:59:15 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|