editor/libeditor/base/nsEditorUtils.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/Selection.h"
michael@0 7 #include "nsCOMArray.h"
michael@0 8 #include "nsComponentManagerUtils.h"
michael@0 9 #include "nsEditorUtils.h"
michael@0 10 #include "nsError.h"
michael@0 11 #include "nsIClipboardDragDropHookList.h"
michael@0 12 // hooks
michael@0 13 #include "nsIClipboardDragDropHooks.h"
michael@0 14 #include "nsIContent.h"
michael@0 15 #include "nsIContentIterator.h"
michael@0 16 #include "nsIDOMDocument.h"
michael@0 17 #include "nsIDocShell.h"
michael@0 18 #include "nsIDocument.h"
michael@0 19 #include "nsIInterfaceRequestorUtils.h"
michael@0 20 #include "nsINode.h"
michael@0 21 #include "nsISimpleEnumerator.h"
michael@0 22
michael@0 23 class nsIDOMRange;
michael@0 24 class nsISupports;
michael@0 25
michael@0 26 using namespace mozilla;
michael@0 27 using namespace mozilla::dom;
michael@0 28
michael@0 29 /******************************************************************************
michael@0 30 * nsAutoSelectionReset
michael@0 31 *****************************************************************************/
michael@0 32
michael@0 33 nsAutoSelectionReset::nsAutoSelectionReset(Selection* aSel, nsEditor* aEd)
michael@0 34 : mSel(nullptr), mEd(nullptr)
michael@0 35 {
michael@0 36 if (!aSel || !aEd) return; // not much we can do, bail.
michael@0 37 if (aEd->ArePreservingSelection()) return; // we already have initted mSavedSel, so this must be nested call.
michael@0 38 mSel = aSel;
michael@0 39 mEd = aEd;
michael@0 40 if (mSel)
michael@0 41 {
michael@0 42 mEd->PreserveSelectionAcrossActions(mSel);
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 nsAutoSelectionReset::~nsAutoSelectionReset()
michael@0 47 {
michael@0 48 NS_ASSERTION(!mSel || mEd, "mEd should be non-null when mSel is");
michael@0 49 if (mSel && mEd->ArePreservingSelection()) // mSel will be null if this was nested call
michael@0 50 {
michael@0 51 mEd->RestorePreservedSelection(mSel);
michael@0 52 }
michael@0 53 }
michael@0 54
michael@0 55 void
michael@0 56 nsAutoSelectionReset::Abort()
michael@0 57 {
michael@0 58 NS_ASSERTION(!mSel || mEd, "mEd should be non-null when mSel is");
michael@0 59 if (mSel)
michael@0 60 mEd->StopPreservingSelection();
michael@0 61 }
michael@0 62
michael@0 63
michael@0 64 /******************************************************************************
michael@0 65 * some helper classes for iterating the dom tree
michael@0 66 *****************************************************************************/
michael@0 67
michael@0 68 nsDOMIterator::nsDOMIterator() :
michael@0 69 mIter(nullptr)
michael@0 70 {
michael@0 71 }
michael@0 72
michael@0 73 nsDOMIterator::~nsDOMIterator()
michael@0 74 {
michael@0 75 }
michael@0 76
michael@0 77 nsresult
michael@0 78 nsDOMIterator::Init(nsIDOMRange* aRange)
michael@0 79 {
michael@0 80 nsresult res;
michael@0 81 mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
michael@0 82 NS_ENSURE_SUCCESS(res, res);
michael@0 83 NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
michael@0 84 return mIter->Init(aRange);
michael@0 85 }
michael@0 86
michael@0 87 nsresult
michael@0 88 nsDOMIterator::Init(nsIDOMNode* aNode)
michael@0 89 {
michael@0 90 nsresult res;
michael@0 91 mIter = do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
michael@0 92 NS_ENSURE_SUCCESS(res, res);
michael@0 93 NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
michael@0 94 nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
michael@0 95 return mIter->Init(content);
michael@0 96 }
michael@0 97
michael@0 98 nsresult
michael@0 99 nsDOMIterator::AppendList(nsBoolDomIterFunctor& functor,
michael@0 100 nsCOMArray<nsIDOMNode>& arrayOfNodes) const
michael@0 101 {
michael@0 102 nsCOMPtr<nsIDOMNode> node;
michael@0 103
michael@0 104 // iterate through dom and build list
michael@0 105 while (!mIter->IsDone())
michael@0 106 {
michael@0 107 node = do_QueryInterface(mIter->GetCurrentNode());
michael@0 108 NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
michael@0 109
michael@0 110 if (functor(node))
michael@0 111 {
michael@0 112 arrayOfNodes.AppendObject(node);
michael@0 113 }
michael@0 114 mIter->Next();
michael@0 115 }
michael@0 116 return NS_OK;
michael@0 117 }
michael@0 118
michael@0 119 nsDOMSubtreeIterator::nsDOMSubtreeIterator()
michael@0 120 {
michael@0 121 }
michael@0 122
michael@0 123 nsDOMSubtreeIterator::~nsDOMSubtreeIterator()
michael@0 124 {
michael@0 125 }
michael@0 126
michael@0 127 nsresult
michael@0 128 nsDOMSubtreeIterator::Init(nsIDOMRange* aRange)
michael@0 129 {
michael@0 130 nsresult res;
michael@0 131 mIter = do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
michael@0 132 NS_ENSURE_SUCCESS(res, res);
michael@0 133 NS_ENSURE_TRUE(mIter, NS_ERROR_FAILURE);
michael@0 134 return mIter->Init(aRange);
michael@0 135 }
michael@0 136
michael@0 137 /******************************************************************************
michael@0 138 * some general purpose editor utils
michael@0 139 *****************************************************************************/
michael@0 140
michael@0 141 bool
michael@0 142 nsEditorUtils::IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset)
michael@0 143 {
michael@0 144 NS_ENSURE_TRUE(aNode || aParent, false);
michael@0 145 if (aNode == aParent) return false;
michael@0 146
michael@0 147 nsCOMPtr<nsIDOMNode> parent, node = do_QueryInterface(aNode);
michael@0 148 nsresult res;
michael@0 149
michael@0 150 do
michael@0 151 {
michael@0 152 res = node->GetParentNode(getter_AddRefs(parent));
michael@0 153 NS_ENSURE_SUCCESS(res, false);
michael@0 154 if (parent == aParent)
michael@0 155 {
michael@0 156 if (aOffset)
michael@0 157 {
michael@0 158 nsCOMPtr<nsIContent> pCon(do_QueryInterface(parent));
michael@0 159 nsCOMPtr<nsIContent> cCon(do_QueryInterface(node));
michael@0 160 if (pCon)
michael@0 161 {
michael@0 162 *aOffset = pCon->IndexOf(cCon);
michael@0 163 }
michael@0 164 }
michael@0 165 return true;
michael@0 166 }
michael@0 167 node = parent;
michael@0 168 } while (parent);
michael@0 169
michael@0 170 return false;
michael@0 171 }
michael@0 172
michael@0 173 bool
michael@0 174 nsEditorUtils::IsLeafNode(nsIDOMNode *aNode)
michael@0 175 {
michael@0 176 bool hasChildren = false;
michael@0 177 if (aNode)
michael@0 178 aNode->HasChildNodes(&hasChildren);
michael@0 179 return !hasChildren;
michael@0 180 }
michael@0 181
michael@0 182 /******************************************************************************
michael@0 183 * utility methods for drag/drop/copy/paste hooks
michael@0 184 *****************************************************************************/
michael@0 185
michael@0 186 nsresult
michael@0 187 nsEditorHookUtils::GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
michael@0 188 nsISimpleEnumerator **aResult)
michael@0 189 {
michael@0 190 nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc);
michael@0 191 NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
michael@0 192
michael@0 193 nsCOMPtr<nsIDocShell> docShell = doc->GetDocShell();
michael@0 194 nsCOMPtr<nsIClipboardDragDropHookList> hookObj = do_GetInterface(docShell);
michael@0 195 NS_ENSURE_TRUE(hookObj, NS_ERROR_FAILURE);
michael@0 196
michael@0 197 return hookObj->GetHookEnumerator(aResult);
michael@0 198 }
michael@0 199
michael@0 200 bool
michael@0 201 nsEditorHookUtils::DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aDropEvent,
michael@0 202 nsITransferable *aTrans)
michael@0 203 {
michael@0 204 nsCOMPtr<nsISimpleEnumerator> enumerator;
michael@0 205 GetHookEnumeratorFromDocument(aDoc, getter_AddRefs(enumerator));
michael@0 206 NS_ENSURE_TRUE(enumerator, true);
michael@0 207
michael@0 208 bool hasMoreHooks = false;
michael@0 209 while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMoreHooks)) && hasMoreHooks)
michael@0 210 {
michael@0 211 nsCOMPtr<nsISupports> isupp;
michael@0 212 if (NS_FAILED(enumerator->GetNext(getter_AddRefs(isupp))))
michael@0 213 break;
michael@0 214
michael@0 215 nsCOMPtr<nsIClipboardDragDropHooks> override = do_QueryInterface(isupp);
michael@0 216 if (override)
michael@0 217 {
michael@0 218 bool doInsert = true;
michael@0 219 #ifdef DEBUG
michael@0 220 nsresult hookResult =
michael@0 221 #endif
michael@0 222 override->OnPasteOrDrop(aDropEvent, aTrans, &doInsert);
michael@0 223 NS_ASSERTION(NS_SUCCEEDED(hookResult), "hook failure in OnPasteOrDrop");
michael@0 224 NS_ENSURE_TRUE(doInsert, false);
michael@0 225 }
michael@0 226 }
michael@0 227
michael@0 228 return true;
michael@0 229 }

mercurial