editor/libeditor/base/nsEditorUtils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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
michael@0 7 #ifndef nsEditorUtils_h__
michael@0 8 #define nsEditorUtils_h__
michael@0 9
michael@0 10
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "nsDebug.h"
michael@0 13 #include "nsEditor.h"
michael@0 14 #include "nsIDOMNode.h"
michael@0 15 #include "nsIEditor.h"
michael@0 16 #include "nscore.h"
michael@0 17
michael@0 18 class nsIAtom;
michael@0 19 class nsIContentIterator;
michael@0 20 class nsIDOMDocument;
michael@0 21 class nsIDOMRange;
michael@0 22 class nsISelection;
michael@0 23 template <class E> class nsCOMArray;
michael@0 24
michael@0 25 /***************************************************************************
michael@0 26 * stack based helper class for batching a collection of txns inside a
michael@0 27 * placeholder txn.
michael@0 28 */
michael@0 29 class MOZ_STACK_CLASS nsAutoPlaceHolderBatch
michael@0 30 {
michael@0 31 private:
michael@0 32 nsCOMPtr<nsIEditor> mEd;
michael@0 33 public:
michael@0 34 nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd))
michael@0 35 { if (mEd) mEd->BeginPlaceHolderTransaction(atom); }
michael@0 36 ~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); }
michael@0 37 };
michael@0 38
michael@0 39 /***************************************************************************
michael@0 40 * stack based helper class for batching a collection of txns.
michael@0 41 * Note: I changed this to use placeholder batching so that we get
michael@0 42 * proper selection save/restore across undo/redo.
michael@0 43 */
michael@0 44 class MOZ_STACK_CLASS nsAutoEditBatch : public nsAutoPlaceHolderBatch
michael@0 45 {
michael@0 46 public:
michael@0 47 nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nullptr) {}
michael@0 48 ~nsAutoEditBatch() {}
michael@0 49 };
michael@0 50
michael@0 51 /***************************************************************************
michael@0 52 * stack based helper class for saving/restoring selection. Note that this
michael@0 53 * assumes that the nodes involved are still around afterwards!
michael@0 54 */
michael@0 55 class MOZ_STACK_CLASS nsAutoSelectionReset
michael@0 56 {
michael@0 57 private:
michael@0 58 /** ref-counted reference to the selection that we are supposed to restore */
michael@0 59 nsRefPtr<mozilla::dom::Selection> mSel;
michael@0 60 nsEditor *mEd; // non-owning ref to nsEditor
michael@0 61
michael@0 62 public:
michael@0 63 /** constructor responsible for remembering all state needed to restore aSel */
michael@0 64 nsAutoSelectionReset(mozilla::dom::Selection* aSel, nsEditor* aEd);
michael@0 65
michael@0 66 /** destructor restores mSel to its former state */
michael@0 67 ~nsAutoSelectionReset();
michael@0 68
michael@0 69 /** Abort: cancel selection saver */
michael@0 70 void Abort();
michael@0 71 };
michael@0 72
michael@0 73 /***************************************************************************
michael@0 74 * stack based helper class for StartOperation()/EndOperation() sandwich
michael@0 75 */
michael@0 76 class MOZ_STACK_CLASS nsAutoRules
michael@0 77 {
michael@0 78 public:
michael@0 79
michael@0 80 nsAutoRules(nsEditor *ed, EditAction action,
michael@0 81 nsIEditor::EDirection aDirection) :
michael@0 82 mEd(ed), mDoNothing(false)
michael@0 83 {
michael@0 84 if (mEd && !mEd->mAction) // mAction will already be set if this is nested call
michael@0 85 {
michael@0 86 mEd->StartOperation(action, aDirection);
michael@0 87 }
michael@0 88 else mDoNothing = true; // nested calls will end up here
michael@0 89 }
michael@0 90 ~nsAutoRules()
michael@0 91 {
michael@0 92 if (mEd && !mDoNothing)
michael@0 93 {
michael@0 94 mEd->EndOperation();
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 protected:
michael@0 99 nsEditor *mEd;
michael@0 100 bool mDoNothing;
michael@0 101 };
michael@0 102
michael@0 103
michael@0 104 /***************************************************************************
michael@0 105 * stack based helper class for turning off active selection adjustment
michael@0 106 * by low level transactions
michael@0 107 */
michael@0 108 class MOZ_STACK_CLASS nsAutoTxnsConserveSelection
michael@0 109 {
michael@0 110 public:
michael@0 111
michael@0 112 nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true)
michael@0 113 {
michael@0 114 if (mEd)
michael@0 115 {
michael@0 116 mOldState = mEd->GetShouldTxnSetSelection();
michael@0 117 mEd->SetShouldTxnSetSelection(false);
michael@0 118 }
michael@0 119 }
michael@0 120
michael@0 121 ~nsAutoTxnsConserveSelection()
michael@0 122 {
michael@0 123 if (mEd)
michael@0 124 {
michael@0 125 mEd->SetShouldTxnSetSelection(mOldState);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 protected:
michael@0 130 nsEditor *mEd;
michael@0 131 bool mOldState;
michael@0 132 };
michael@0 133
michael@0 134 /***************************************************************************
michael@0 135 * stack based helper class for batching reflow and paint requests.
michael@0 136 */
michael@0 137 class MOZ_STACK_CLASS nsAutoUpdateViewBatch
michael@0 138 {
michael@0 139 public:
michael@0 140
michael@0 141 nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed)
michael@0 142 {
michael@0 143 NS_ASSERTION(mEd, "null mEd pointer!");
michael@0 144
michael@0 145 if (mEd)
michael@0 146 mEd->BeginUpdateViewBatch();
michael@0 147 }
michael@0 148
michael@0 149 ~nsAutoUpdateViewBatch()
michael@0 150 {
michael@0 151 if (mEd)
michael@0 152 mEd->EndUpdateViewBatch();
michael@0 153 }
michael@0 154
michael@0 155 protected:
michael@0 156 nsEditor *mEd;
michael@0 157 };
michael@0 158
michael@0 159 /******************************************************************************
michael@0 160 * some helper classes for iterating the dom tree
michael@0 161 *****************************************************************************/
michael@0 162
michael@0 163 class nsBoolDomIterFunctor
michael@0 164 {
michael@0 165 public:
michael@0 166 virtual bool operator()(nsIDOMNode* aNode)=0;
michael@0 167 };
michael@0 168
michael@0 169 class MOZ_STACK_CLASS nsDOMIterator
michael@0 170 {
michael@0 171 public:
michael@0 172 nsDOMIterator();
michael@0 173 virtual ~nsDOMIterator();
michael@0 174
michael@0 175 nsresult Init(nsIDOMRange* aRange);
michael@0 176 nsresult Init(nsIDOMNode* aNode);
michael@0 177 nsresult AppendList(nsBoolDomIterFunctor& functor,
michael@0 178 nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
michael@0 179 protected:
michael@0 180 nsCOMPtr<nsIContentIterator> mIter;
michael@0 181 };
michael@0 182
michael@0 183 class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator
michael@0 184 {
michael@0 185 public:
michael@0 186 nsDOMSubtreeIterator();
michael@0 187 virtual ~nsDOMSubtreeIterator();
michael@0 188
michael@0 189 nsresult Init(nsIDOMRange* aRange);
michael@0 190 };
michael@0 191
michael@0 192 class nsTrivialFunctor : public nsBoolDomIterFunctor
michael@0 193 {
michael@0 194 public:
michael@0 195 virtual bool operator()(nsIDOMNode* aNode) // used to build list of all nodes iterator covers
michael@0 196 {
michael@0 197 return true;
michael@0 198 }
michael@0 199 };
michael@0 200
michael@0 201
michael@0 202 /******************************************************************************
michael@0 203 * general dom point utility struct
michael@0 204 *****************************************************************************/
michael@0 205 struct MOZ_STACK_CLASS DOMPoint
michael@0 206 {
michael@0 207 nsCOMPtr<nsIDOMNode> node;
michael@0 208 int32_t offset;
michael@0 209
michael@0 210 DOMPoint() : node(0),offset(0) {}
michael@0 211 DOMPoint(nsIDOMNode *aNode, int32_t aOffset) :
michael@0 212 node(aNode),offset(aOffset) {}
michael@0 213 void SetPoint(nsIDOMNode *aNode, int32_t aOffset)
michael@0 214 {
michael@0 215 node = aNode; offset = aOffset;
michael@0 216 }
michael@0 217 void GetPoint(nsCOMPtr<nsIDOMNode> &aNode, int32_t &aOffset)
michael@0 218 {
michael@0 219 aNode = node; aOffset = offset;
michael@0 220 }
michael@0 221 };
michael@0 222
michael@0 223
michael@0 224 class nsEditorUtils
michael@0 225 {
michael@0 226 public:
michael@0 227 static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset = 0);
michael@0 228 static bool IsLeafNode(nsIDOMNode *aNode);
michael@0 229 };
michael@0 230
michael@0 231
michael@0 232 class nsIDOMEvent;
michael@0 233 class nsISimpleEnumerator;
michael@0 234 class nsITransferable;
michael@0 235
michael@0 236 class nsEditorHookUtils
michael@0 237 {
michael@0 238 public:
michael@0 239 static bool DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
michael@0 240 nsITransferable *aTrans);
michael@0 241 private:
michael@0 242 static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
michael@0 243 nsISimpleEnumerator **aEnumerator);
michael@0 244 };
michael@0 245
michael@0 246 #endif // nsEditorUtils_h__

mercurial