1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla 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/MPL/
*
* 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 of 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 MPL, 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 MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef nsTextEditRules_h__
#define nsTextEditRules_h__
#include "nsCOMPtr.h"
#include "nsPlaintextEditor.h"
#include "nsIDOMNode.h"
#include "nsEditRules.h"
/** Object that encapsulates HTML text-specific editing rules.
*
* To be a good citizen, edit rules must live by these restrictions:
* 1. All data manipulation is through the editor.
* Content nodes in the document tree must <B>not</B> be manipulated directly.
* Content nodes in document fragments that are not part of the document itself
* may be manipulated at will. Operations on document fragments must <B>not</B>
* go through the editor.
* 2. Selection must not be explicitly set by the rule method.
* Any manipulation of Selection must be done by the editor.
*/
class nsTextEditRules : public nsIEditRules
{
public:
NS_DECL_ISUPPORTS
nsTextEditRules();
virtual ~nsTextEditRules();
// nsIEditRules methods
NS_IMETHOD Init(nsPlaintextEditor *aEditor, PRUint32 aFlags);
NS_IMETHOD BeforeEdit(PRInt32 action, nsIEditor::EDirection aDirection);
NS_IMETHOD AfterEdit(PRInt32 action, nsIEditor::EDirection aDirection);
NS_IMETHOD WillDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, PRBool *aCancel, PRBool *aHandled);
NS_IMETHOD DidDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, nsresult aResult);
NS_IMETHOD GetFlags(PRUint32 *aFlags);
NS_IMETHOD SetFlags(PRUint32 aFlags);
NS_IMETHOD DocumentIsEmpty(PRBool *aDocumentIsEmpty);
// nsTextEditRules action id's
enum
{
kDefault = 0,
// any editor that has a txn mgr
kUndo = 1000,
kRedo = 1001,
// text actions
kInsertText = 2000,
kInsertTextIME = 2001,
kDeleteSelection = 2002,
kSetTextProperty = 2003,
kRemoveTextProperty = 2004,
kOutputText = 2005,
// html only action
kInsertBreak = 3000,
kMakeList = 3001,
kIndent = 3002,
kOutdent = 3003,
kAlign = 3004,
kMakeBasicBlock = 3005,
kRemoveList = 3006,
kMakeDefListItem = 3007,
kInsertElement = 3008,
kLoadHTML = 3013,
kSetAbsolutePosition = 3015,
kRemoveAbsolutePosition = 3016,
kDecreaseZIndex = 3017,
kIncreaseZIndex = 3018
};
public:
nsresult ResetIMETextPWBuf();
protected:
// nsTextEditRules implementation methods
nsresult WillInsertText( PRInt32 aAction,
nsISelection *aSelection,
PRBool *aCancel,
PRBool *aHandled,
const nsAString *inString,
nsAString *outString,
PRInt32 aMaxLength);
nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
nsresult WillInsertBreak(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
nsresult WillInsert(nsISelection *aSelection, PRBool *aCancel);
nsresult DidInsert(nsISelection *aSelection, nsresult aResult);
nsresult WillDeleteSelection(nsISelection *aSelection,
nsIEditor::EDirection aCollapsedAction,
PRBool *aCancel,
PRBool *aHandled);
nsresult DidDeleteSelection(nsISelection *aSelection,
nsIEditor::EDirection aCollapsedAction,
nsresult aResult);
nsresult WillSetTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult DidSetTextProperty(nsISelection *aSelection, nsresult aResult);
nsresult WillRemoveTextProperty(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult DidRemoveTextProperty(nsISelection *aSelection, nsresult aResult);
nsresult WillUndo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult DidUndo(nsISelection *aSelection, nsresult aResult);
nsresult WillRedo(nsISelection *aSelection, PRBool *aCancel, PRBool *aHandled);
nsresult DidRedo(nsISelection *aSelection, nsresult aResult);
/** called prior to nsIEditor::OutputToString
* @param aSelection
* @param aInFormat the format requested for the output, a MIME type
* @param aOutText the string to use for output, if aCancel is set to true
* @param aOutCancel if set to PR_TRUE, the caller should cancel the operation
* and use aOutText as the result.
*/
nsresult WillOutputText(nsISelection *aSelection,
const nsAString *aInFormat,
nsAString *aOutText,
PRBool *aOutCancel,
PRBool *aHandled);
nsresult DidOutputText(nsISelection *aSelection, nsresult aResult);
// helper functions
/** replaces newllines with breaks, if needed. acts on doc portion in aRange */
nsresult ReplaceNewlines(nsIDOMRange *aRange);
/** creates a trailing break in the text doc if there is not one already */
nsresult CreateTrailingBRIfNeeded();
/** creates a bogus text node if the document has no editable content */
nsresult CreateBogusNodeIfNeeded(nsISelection *aSelection);
/** returns a truncated insertion string if insertion would place us
over aMaxLength */
nsresult TruncateInsertionIfNeeded(nsISelection *aSelection,
const nsAString *aInString,
nsAString *aOutString,
PRInt32 aMaxLength);
/** Echo's the insertion text into the password buffer, and converts
insertion text to '*'s */
nsresult EchoInsertionToPWBuff(PRInt32 aStart, PRInt32 aEnd, nsAString *aOutString);
/** Remove IME composition text from password buffer */
nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);
nsresult CreateMozBR(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outBRNode);
nsresult CheckBidiLevelForDeletion(nsIDOMNode *aSelNode,
PRInt32 aSelOffset,
nsIEditor::EDirection aAction,
PRBool *aCancel);
nsIDOMNode *GetBody();
// data members
nsPlaintextEditor *mEditor; // note that we do not refcount the editor
nsString mPasswordText; // a buffer we use to store the real value of password editors
nsString mPasswordIMEText; // a buffer we use to track the IME composition string
PRUint32 mPasswordIMEIndex;
nsCOMPtr<nsIDOMNode> mBogusNode; // magic node acts as placeholder in empty doc
nsCOMPtr<nsIDOMNode> mBody; // cached root node
nsCOMPtr<nsIDOMNode> mCachedSelectionNode; // cached selected node
PRInt32 mCachedSelectionOffset; // cached selected offset
PRUint32 mFlags;
PRUint32 mActionNesting;
PRPackedBool mLockRulesSniffing;
PRPackedBool mDidExplicitlySetInterline;
PRInt32 mTheAction; // the top level editor action
// friends
friend class nsAutoLockRulesSniffing;
};
class nsTextRulesInfo : public nsRulesInfo
{
public:
nsTextRulesInfo(int aAction) :
nsRulesInfo(aAction),
inString(0),
outString(0),
outputFormat(0),
maxLength(-1),
collapsedAction(nsIEditor::eNext),
bOrdered(PR_FALSE),
entireList(PR_FALSE),
bulletType(0),
alignType(0),
blockType(0),
insertElement(0)
{};
virtual ~nsTextRulesInfo() {};
// kInsertText
const nsAString *inString;
nsAString *outString;
const nsAString *outputFormat;
PRInt32 maxLength;
// kDeleteSelection
nsIEditor::EDirection collapsedAction;
// kMakeList
PRBool bOrdered;
PRBool entireList;
const nsAString *bulletType;
// kAlign
const nsAString *alignType;
// kMakeBasicBlock
const nsAString *blockType;
// kInsertElement
const nsIDOMElement* insertElement;
};
/***************************************************************************
* stack based helper class for StartOperation()/EndOperation() sandwich.
* this class sets a bool letting us know to ignore any rules sniffing
* that tries to occur reentrantly.
*/
class nsAutoLockRulesSniffing
{
public:
nsAutoLockRulesSniffing(nsTextEditRules *rules) : mRules(rules)
{if (mRules) mRules->mLockRulesSniffing = PR_TRUE;}
~nsAutoLockRulesSniffing()
{if (mRules) mRules->mLockRulesSniffing = PR_FALSE;}
protected:
nsTextEditRules *mRules;
};
/***************************************************************************
* stack based helper class for turning on/off the edit listener.
*/
class nsAutoLockListener
{
public:
nsAutoLockListener(PRPackedBool *enabled) : mEnabled(enabled)
{if (mEnabled) { mOldState=*mEnabled; *mEnabled = PR_FALSE;}}
~nsAutoLockListener()
{if (mEnabled) *mEnabled = mOldState;}
protected:
PRPackedBool *mEnabled;
PRPackedBool mOldState;
};
nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
#endif //nsTextEditRules_h__
|