editor/libeditor/base/nsEditorUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/base/nsEditorUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,246 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#ifndef nsEditorUtils_h__
    1.11 +#define nsEditorUtils_h__
    1.12 +
    1.13 +
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsDebug.h"
    1.16 +#include "nsEditor.h"
    1.17 +#include "nsIDOMNode.h"
    1.18 +#include "nsIEditor.h"
    1.19 +#include "nscore.h"
    1.20 +
    1.21 +class nsIAtom;
    1.22 +class nsIContentIterator;
    1.23 +class nsIDOMDocument;
    1.24 +class nsIDOMRange;
    1.25 +class nsISelection;
    1.26 +template <class E> class nsCOMArray;
    1.27 +
    1.28 +/***************************************************************************
    1.29 + * stack based helper class for batching a collection of txns inside a 
    1.30 + * placeholder txn.
    1.31 + */
    1.32 +class MOZ_STACK_CLASS nsAutoPlaceHolderBatch
    1.33 +{
    1.34 +  private:
    1.35 +    nsCOMPtr<nsIEditor> mEd;
    1.36 +  public:
    1.37 +    nsAutoPlaceHolderBatch( nsIEditor *aEd, nsIAtom *atom) : mEd(do_QueryInterface(aEd)) 
    1.38 +                   { if (mEd) mEd->BeginPlaceHolderTransaction(atom); }
    1.39 +    ~nsAutoPlaceHolderBatch() { if (mEd) mEd->EndPlaceHolderTransaction(); }
    1.40 +};
    1.41 +
    1.42 +/***************************************************************************
    1.43 + * stack based helper class for batching a collection of txns.  
    1.44 + * Note: I changed this to use placeholder batching so that we get
    1.45 + * proper selection save/restore across undo/redo.
    1.46 + */
    1.47 +class MOZ_STACK_CLASS nsAutoEditBatch : public nsAutoPlaceHolderBatch
    1.48 +{
    1.49 +  public:
    1.50 +    nsAutoEditBatch( nsIEditor *aEd) : nsAutoPlaceHolderBatch(aEd,nullptr)  {}
    1.51 +    ~nsAutoEditBatch() {}
    1.52 +};
    1.53 +
    1.54 +/***************************************************************************
    1.55 + * stack based helper class for saving/restoring selection.  Note that this
    1.56 + * assumes that the nodes involved are still around afterwards!
    1.57 + */
    1.58 +class MOZ_STACK_CLASS nsAutoSelectionReset
    1.59 +{
    1.60 +  private:
    1.61 +    /** ref-counted reference to the selection that we are supposed to restore */
    1.62 +    nsRefPtr<mozilla::dom::Selection> mSel;
    1.63 +    nsEditor *mEd;  // non-owning ref to nsEditor
    1.64 +
    1.65 +  public:
    1.66 +    /** constructor responsible for remembering all state needed to restore aSel */
    1.67 +    nsAutoSelectionReset(mozilla::dom::Selection* aSel, nsEditor* aEd);
    1.68 +    
    1.69 +    /** destructor restores mSel to its former state */
    1.70 +    ~nsAutoSelectionReset();
    1.71 +
    1.72 +    /** Abort: cancel selection saver */
    1.73 +    void Abort();
    1.74 +};
    1.75 +
    1.76 +/***************************************************************************
    1.77 + * stack based helper class for StartOperation()/EndOperation() sandwich
    1.78 + */
    1.79 +class MOZ_STACK_CLASS nsAutoRules
    1.80 +{
    1.81 +  public:
    1.82 +  
    1.83 +  nsAutoRules(nsEditor *ed, EditAction action,
    1.84 +              nsIEditor::EDirection aDirection) :
    1.85 +         mEd(ed), mDoNothing(false)
    1.86 +  { 
    1.87 +    if (mEd && !mEd->mAction) // mAction will already be set if this is nested call
    1.88 +    {
    1.89 +      mEd->StartOperation(action, aDirection);
    1.90 +    }
    1.91 +    else mDoNothing = true; // nested calls will end up here
    1.92 +  }
    1.93 +  ~nsAutoRules() 
    1.94 +  {
    1.95 +    if (mEd && !mDoNothing) 
    1.96 +    {
    1.97 +      mEd->EndOperation();
    1.98 +    }
    1.99 +  }
   1.100 +  
   1.101 +  protected:
   1.102 +  nsEditor *mEd;
   1.103 +  bool mDoNothing;
   1.104 +};
   1.105 +
   1.106 +
   1.107 +/***************************************************************************
   1.108 + * stack based helper class for turning off active selection adjustment
   1.109 + * by low level transactions
   1.110 + */
   1.111 +class MOZ_STACK_CLASS nsAutoTxnsConserveSelection
   1.112 +{
   1.113 +  public:
   1.114 +  
   1.115 +  nsAutoTxnsConserveSelection(nsEditor *ed) : mEd(ed), mOldState(true)
   1.116 +  {
   1.117 +    if (mEd) 
   1.118 +    {
   1.119 +      mOldState = mEd->GetShouldTxnSetSelection();
   1.120 +      mEd->SetShouldTxnSetSelection(false);
   1.121 +    }
   1.122 +  }
   1.123 +  
   1.124 +  ~nsAutoTxnsConserveSelection() 
   1.125 +  {
   1.126 +    if (mEd) 
   1.127 +    {
   1.128 +      mEd->SetShouldTxnSetSelection(mOldState);
   1.129 +    }
   1.130 +  }
   1.131 +  
   1.132 +  protected:
   1.133 +  nsEditor *mEd;
   1.134 +  bool mOldState;
   1.135 +};
   1.136 +
   1.137 +/***************************************************************************
   1.138 + * stack based helper class for batching reflow and paint requests.
   1.139 + */
   1.140 +class MOZ_STACK_CLASS nsAutoUpdateViewBatch
   1.141 +{
   1.142 +  public:
   1.143 +  
   1.144 +  nsAutoUpdateViewBatch(nsEditor *ed) : mEd(ed)
   1.145 +  {
   1.146 +    NS_ASSERTION(mEd, "null mEd pointer!");
   1.147 +
   1.148 +    if (mEd) 
   1.149 +      mEd->BeginUpdateViewBatch();
   1.150 +  }
   1.151 +  
   1.152 +  ~nsAutoUpdateViewBatch() 
   1.153 +  {
   1.154 +    if (mEd) 
   1.155 +      mEd->EndUpdateViewBatch();
   1.156 +  }
   1.157 +  
   1.158 +  protected:
   1.159 +  nsEditor *mEd;
   1.160 +};
   1.161 +
   1.162 +/******************************************************************************
   1.163 + * some helper classes for iterating the dom tree
   1.164 + *****************************************************************************/
   1.165 +
   1.166 +class nsBoolDomIterFunctor 
   1.167 +{
   1.168 +  public:
   1.169 +    virtual bool operator()(nsIDOMNode* aNode)=0;
   1.170 +};
   1.171 +
   1.172 +class MOZ_STACK_CLASS nsDOMIterator
   1.173 +{
   1.174 +  public:
   1.175 +    nsDOMIterator();
   1.176 +    virtual ~nsDOMIterator();
   1.177 +    
   1.178 +    nsresult Init(nsIDOMRange* aRange);
   1.179 +    nsresult Init(nsIDOMNode* aNode);
   1.180 +    nsresult AppendList(nsBoolDomIterFunctor& functor,
   1.181 +                        nsCOMArray<nsIDOMNode>& arrayOfNodes) const;
   1.182 +  protected:
   1.183 +    nsCOMPtr<nsIContentIterator> mIter;
   1.184 +};
   1.185 +
   1.186 +class MOZ_STACK_CLASS nsDOMSubtreeIterator : public nsDOMIterator
   1.187 +{
   1.188 +  public:
   1.189 +    nsDOMSubtreeIterator();
   1.190 +    virtual ~nsDOMSubtreeIterator();
   1.191 +
   1.192 +    nsresult Init(nsIDOMRange* aRange);
   1.193 +};
   1.194 +
   1.195 +class nsTrivialFunctor : public nsBoolDomIterFunctor
   1.196 +{
   1.197 +  public:
   1.198 +    virtual bool operator()(nsIDOMNode* aNode)  // used to build list of all nodes iterator covers
   1.199 +    {
   1.200 +      return true;
   1.201 +    }
   1.202 +};
   1.203 +
   1.204 +
   1.205 +/******************************************************************************
   1.206 + * general dom point utility struct
   1.207 + *****************************************************************************/
   1.208 +struct MOZ_STACK_CLASS DOMPoint
   1.209 +{
   1.210 +  nsCOMPtr<nsIDOMNode> node;
   1.211 +  int32_t offset;
   1.212 +  
   1.213 +  DOMPoint() : node(0),offset(0) {}
   1.214 +  DOMPoint(nsIDOMNode *aNode, int32_t aOffset) : 
   1.215 +                 node(aNode),offset(aOffset) {}
   1.216 +  void SetPoint(nsIDOMNode *aNode, int32_t aOffset)
   1.217 +  {
   1.218 +    node = aNode; offset = aOffset;
   1.219 +  }
   1.220 +  void GetPoint(nsCOMPtr<nsIDOMNode> &aNode, int32_t &aOffset)
   1.221 +  {
   1.222 +    aNode = node; aOffset = offset;
   1.223 +  }
   1.224 +};
   1.225 +
   1.226 +
   1.227 +class nsEditorUtils
   1.228 +{
   1.229 +  public:
   1.230 +    static bool IsDescendantOf(nsIDOMNode *aNode, nsIDOMNode *aParent, int32_t *aOffset = 0);
   1.231 +    static bool IsLeafNode(nsIDOMNode *aNode);
   1.232 +};
   1.233 +
   1.234 +
   1.235 +class nsIDOMEvent;
   1.236 +class nsISimpleEnumerator;
   1.237 +class nsITransferable;
   1.238 +
   1.239 +class nsEditorHookUtils
   1.240 +{
   1.241 +  public:
   1.242 +    static bool     DoInsertionHook(nsIDOMDocument *aDoc, nsIDOMEvent *aEvent,
   1.243 +                                    nsITransferable *aTrans);
   1.244 +  private:
   1.245 +    static nsresult GetHookEnumeratorFromDocument(nsIDOMDocument *aDoc,
   1.246 +                                                  nsISimpleEnumerator **aEnumerator);
   1.247 +};
   1.248 +
   1.249 +#endif // nsEditorUtils_h__

mercurial