1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsTextEditRules_h__
39 : #define nsTextEditRules_h__
40 :
41 : #include "nsCOMPtr.h"
42 :
43 : #include "nsPlaintextEditor.h"
44 : #include "nsIDOMNode.h"
45 :
46 : #include "nsEditRules.h"
47 : #include "nsITimer.h"
48 :
49 : /** Object that encapsulates HTML text-specific editing rules.
50 : *
51 : * To be a good citizen, edit rules must live by these restrictions:
52 : * 1. All data manipulation is through the editor.
53 : * Content nodes in the document tree must <B>not</B> be manipulated directly.
54 : * Content nodes in document fragments that are not part of the document itself
55 : * may be manipulated at will. Operations on document fragments must <B>not</B>
56 : * go through the editor.
57 : * 2. Selection must not be explicitly set by the rule method.
58 : * Any manipulation of Selection must be done by the editor.
59 : */
60 : class nsTextEditRules : public nsIEditRules, public nsITimerCallback
61 : {
62 : public:
63 : NS_DECL_NSITIMERCALLBACK
64 0 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
65 1464 : NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextEditRules, nsIEditRules)
66 :
67 : nsTextEditRules();
68 : virtual ~nsTextEditRules();
69 :
70 : // nsIEditRules methods
71 : NS_IMETHOD Init(nsPlaintextEditor *aEditor);
72 : NS_IMETHOD DetachEditor();
73 : NS_IMETHOD BeforeEdit(PRInt32 action, nsIEditor::EDirection aDirection);
74 : NS_IMETHOD AfterEdit(PRInt32 action, nsIEditor::EDirection aDirection);
75 : NS_IMETHOD WillDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, bool *aCancel, bool *aHandled);
76 : NS_IMETHOD DidDoAction(nsISelection *aSelection, nsRulesInfo *aInfo, nsresult aResult);
77 : NS_IMETHOD DocumentIsEmpty(bool *aDocumentIsEmpty);
78 : NS_IMETHOD DocumentModified();
79 :
80 : // nsTextEditRules action id's
81 : enum
82 : {
83 : kDefault = 0,
84 : // any editor that has a txn mgr
85 : kUndo = 1000,
86 : kRedo = 1001,
87 : // text actions
88 : kInsertText = 2000,
89 : kInsertTextIME = 2001,
90 : kDeleteSelection = 2002,
91 : kSetTextProperty = 2003,
92 : kRemoveTextProperty = 2004,
93 : kOutputText = 2005,
94 : // html only action
95 : kInsertBreak = 3000,
96 : kMakeList = 3001,
97 : kIndent = 3002,
98 : kOutdent = 3003,
99 : kAlign = 3004,
100 : kMakeBasicBlock = 3005,
101 : kRemoveList = 3006,
102 : kMakeDefListItem = 3007,
103 : kInsertElement = 3008,
104 : kLoadHTML = 3013,
105 : kSetAbsolutePosition = 3015,
106 : kRemoveAbsolutePosition = 3016,
107 : kDecreaseZIndex = 3017,
108 : kIncreaseZIndex = 3018
109 :
110 : };
111 :
112 : public:
113 : nsresult ResetIMETextPWBuf();
114 :
115 : /**
116 : * Handles the newline characters either according to aNewLineHandling
117 : * or to the default system prefs if aNewLineHandling is negative.
118 : *
119 : * @param aString the string to be modified in place.
120 : * @param aNewLineHandling determine the desired type of newline handling:
121 : * * negative values:
122 : * handle newlines according to platform defaults.
123 : * * nsIPlaintextEditor::eNewlinesReplaceWithSpaces:
124 : * replace newlines with spaces.
125 : * * nsIPlaintextEditor::eNewlinesStrip:
126 : * remove newlines from the string.
127 : * * nsIPlaintextEditor::eNewlinesReplaceWithCommas:
128 : * replace newlines with commas.
129 : * * nsIPlaintextEditor::eNewlinesStripSurroundingWhitespace:
130 : * collapse newlines and surrounding whitespace characters and
131 : * remove them from the string.
132 : * * nsIPlaintextEditor::eNewlinesPasteIntact:
133 : * only remove the leading and trailing newlines.
134 : * * nsIPlaintextEditor::eNewlinesPasteToFirst or any other value:
135 : * remove the first newline and all characters following it.
136 : */
137 : static void HandleNewLines(nsString &aString, PRInt32 aNewLineHandling);
138 :
139 : /**
140 : * Prepare a string buffer for being displayed as the contents of a password
141 : * field. This function uses the platform-specific character for representing
142 : * characters entered into password fields.
143 : *
144 : * @param aOutString the output string. When this function returns,
145 : * aOutString will contain aLength password characters.
146 : * @param aLength the number of password characters that aOutString should
147 : * contain.
148 : */
149 : static nsresult FillBufWithPWChars(nsAString *aOutString, PRInt32 aLength);
150 :
151 : protected:
152 :
153 : // nsTextEditRules implementation methods
154 : nsresult WillInsertText( PRInt32 aAction,
155 : nsISelection *aSelection,
156 : bool *aCancel,
157 : bool *aHandled,
158 : const nsAString *inString,
159 : nsAString *outString,
160 : PRInt32 aMaxLength);
161 : nsresult DidInsertText(nsISelection *aSelection, nsresult aResult);
162 : nsresult GetTopEnclosingPre(nsIDOMNode *aNode, nsIDOMNode** aOutPreNode);
163 :
164 : nsresult WillInsertBreak(nsISelection *aSelection, bool *aCancel,
165 : bool *aHandled, PRInt32 aMaxLength);
166 : nsresult DidInsertBreak(nsISelection *aSelection, nsresult aResult);
167 :
168 : nsresult WillInsert(nsISelection *aSelection, bool *aCancel);
169 : nsresult DidInsert(nsISelection *aSelection, nsresult aResult);
170 :
171 : nsresult WillDeleteSelection(nsISelection *aSelection,
172 : nsIEditor::EDirection aCollapsedAction,
173 : bool *aCancel,
174 : bool *aHandled);
175 : nsresult DidDeleteSelection(nsISelection *aSelection,
176 : nsIEditor::EDirection aCollapsedAction,
177 : nsresult aResult);
178 :
179 : nsresult WillSetTextProperty(nsISelection *aSelection, bool *aCancel, bool *aHandled);
180 : nsresult DidSetTextProperty(nsISelection *aSelection, nsresult aResult);
181 :
182 : nsresult WillRemoveTextProperty(nsISelection *aSelection, bool *aCancel, bool *aHandled);
183 : nsresult DidRemoveTextProperty(nsISelection *aSelection, nsresult aResult);
184 :
185 : nsresult WillUndo(nsISelection *aSelection, bool *aCancel, bool *aHandled);
186 : nsresult DidUndo(nsISelection *aSelection, nsresult aResult);
187 :
188 : nsresult WillRedo(nsISelection *aSelection, bool *aCancel, bool *aHandled);
189 : nsresult DidRedo(nsISelection *aSelection, nsresult aResult);
190 :
191 : /** called prior to nsIEditor::OutputToString
192 : * @param aSelection
193 : * @param aInFormat the format requested for the output, a MIME type
194 : * @param aOutText the string to use for output, if aCancel is set to true
195 : * @param aOutCancel if set to true, the caller should cancel the operation
196 : * and use aOutText as the result.
197 : */
198 : nsresult WillOutputText(nsISelection *aSelection,
199 : const nsAString *aInFormat,
200 : nsAString *aOutText,
201 : bool *aOutCancel,
202 : bool *aHandled);
203 :
204 : nsresult DidOutputText(nsISelection *aSelection, nsresult aResult);
205 :
206 :
207 : // helper functions
208 :
209 : /** check for and replace a redundant trailing break */
210 : nsresult RemoveRedundantTrailingBR();
211 :
212 : /** creates a trailing break in the text doc if there is not one already */
213 : nsresult CreateTrailingBRIfNeeded();
214 :
215 : /** creates a bogus text node if the document has no editable content */
216 : nsresult CreateBogusNodeIfNeeded(nsISelection *aSelection);
217 :
218 : /** returns a truncated insertion string if insertion would place us
219 : over aMaxLength */
220 : nsresult TruncateInsertionIfNeeded(nsISelection *aSelection,
221 : const nsAString *aInString,
222 : nsAString *aOutString,
223 : PRInt32 aMaxLength,
224 : bool *aTruncated);
225 :
226 : /** Remove IME composition text from password buffer */
227 : nsresult RemoveIMETextFromPWBuf(PRUint32 &aStart, nsAString *aIMEString);
228 :
229 : nsresult CreateMozBR(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outBRNode);
230 :
231 : nsresult CheckBidiLevelForDeletion(nsISelection *aSelection,
232 : nsIDOMNode *aSelNode,
233 : PRInt32 aSelOffset,
234 : nsIEditor::EDirection aAction,
235 : bool *aCancel);
236 :
237 : nsresult HideLastPWInput();
238 :
239 : nsresult CollapseSelectionToTrailingBRIfNeeded(nsISelection *aSelection);
240 :
241 0 : bool IsPasswordEditor() const
242 : {
243 0 : return mEditor ? mEditor->IsPasswordEditor() : false;
244 : }
245 0 : bool IsSingleLineEditor() const
246 : {
247 0 : return mEditor ? mEditor->IsSingleLineEditor() : false;
248 : }
249 0 : bool IsPlaintextEditor() const
250 : {
251 0 : return mEditor ? mEditor->IsPlaintextEditor() : false;
252 : }
253 0 : bool IsReadonly() const
254 : {
255 0 : return mEditor ? mEditor->IsReadonly() : false;
256 : }
257 0 : bool IsDisabled() const
258 : {
259 0 : return mEditor ? mEditor->IsDisabled() : false;
260 : }
261 0 : bool IsMailEditor() const
262 : {
263 0 : return mEditor ? mEditor->IsMailEditor() : false;
264 : }
265 0 : bool DontEchoPassword() const
266 : {
267 0 : return mEditor ? mEditor->DontEchoPassword() : false;
268 : }
269 :
270 : // data members
271 : nsPlaintextEditor *mEditor; // note that we do not refcount the editor
272 : nsString mPasswordText; // a buffer we use to store the real value of password editors
273 : nsString mPasswordIMEText; // a buffer we use to track the IME composition string
274 : PRUint32 mPasswordIMEIndex;
275 : nsCOMPtr<nsIDOMNode> mBogusNode; // magic node acts as placeholder in empty doc
276 : nsCOMPtr<nsIDOMNode> mCachedSelectionNode; // cached selected node
277 : PRInt32 mCachedSelectionOffset; // cached selected offset
278 : PRUint32 mActionNesting;
279 : bool mLockRulesSniffing;
280 : bool mDidExplicitlySetInterline;
281 : bool mDeleteBidiImmediately; // in bidirectional text, delete
282 : // characters not visually
283 : // adjacent to the caret without
284 : // moving the caret first.
285 : PRInt32 mTheAction; // the top level editor action
286 : nsCOMPtr<nsITimer> mTimer;
287 : PRUint32 mLastStart, mLastLength;
288 :
289 : // friends
290 : friend class nsAutoLockRulesSniffing;
291 :
292 : };
293 :
294 :
295 :
296 : class nsTextRulesInfo : public nsRulesInfo
297 : {
298 : public:
299 :
300 0 : nsTextRulesInfo(int aAction) :
301 : nsRulesInfo(aAction),
302 : inString(0),
303 : outString(0),
304 : outputFormat(0),
305 : maxLength(-1),
306 : collapsedAction(nsIEditor::eNext),
307 : bOrdered(false),
308 : entireList(false),
309 : bulletType(0),
310 : alignType(0),
311 : blockType(0),
312 0 : insertElement(0)
313 0 : {}
314 :
315 0 : virtual ~nsTextRulesInfo() {}
316 :
317 : // kInsertText
318 : const nsAString *inString;
319 : nsAString *outString;
320 : const nsAString *outputFormat;
321 : PRInt32 maxLength;
322 :
323 : // kDeleteSelection
324 : nsIEditor::EDirection collapsedAction;
325 :
326 : // kMakeList
327 : bool bOrdered;
328 : bool entireList;
329 : const nsAString *bulletType;
330 :
331 : // kAlign
332 : const nsAString *alignType;
333 :
334 : // kMakeBasicBlock
335 : const nsAString *blockType;
336 :
337 : // kInsertElement
338 : const nsIDOMElement* insertElement;
339 : };
340 :
341 :
342 : /***************************************************************************
343 : * stack based helper class for StartOperation()/EndOperation() sandwich.
344 : * this class sets a bool letting us know to ignore any rules sniffing
345 : * that tries to occur reentrantly.
346 : */
347 : class nsAutoLockRulesSniffing
348 : {
349 : public:
350 :
351 0 : nsAutoLockRulesSniffing(nsTextEditRules *rules) : mRules(rules)
352 0 : {if (mRules) mRules->mLockRulesSniffing = true;}
353 0 : ~nsAutoLockRulesSniffing()
354 0 : {if (mRules) mRules->mLockRulesSniffing = false;}
355 :
356 : protected:
357 : nsTextEditRules *mRules;
358 : };
359 :
360 :
361 :
362 : /***************************************************************************
363 : * stack based helper class for turning on/off the edit listener.
364 : */
365 : class nsAutoLockListener
366 : {
367 : public:
368 :
369 0 : nsAutoLockListener(bool *enabled) : mEnabled(enabled)
370 0 : {if (mEnabled) { mOldState=*mEnabled; *mEnabled = false;}}
371 0 : ~nsAutoLockListener()
372 0 : {if (mEnabled) *mEnabled = mOldState;}
373 :
374 : protected:
375 : bool *mEnabled;
376 : bool mOldState;
377 : };
378 :
379 : #endif //nsTextEditRules_h__
|