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
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/
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
15 * The Original Code is mozilla.org code.
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.
23 * Daniel Glazman <glazman@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsHTMLEditRules_h__
40 #define nsHTMLEditRules_h__
42 #include "nsTextEditRules.h"
43 #include "nsIHTMLEditRules.h"
44 #include "nsIEditActionListener.h"
45 #include "nsCOMArray.h"
48 #include "nsEditorUtils.h"
49 #include "TypeInState.h"
50 #include "nsReadableUtils.h"
57 struct StyleCache
: public PropItem
61 StyleCache() : PropItem(), mPresent(PR_FALSE
) {
62 MOZ_COUNT_CTOR(StyleCache
);
65 StyleCache(nsIAtom
*aTag
, const nsAString
&aAttr
, const nsAString
&aValue
) :
66 PropItem(aTag
, aAttr
, aValue
), mPresent(PR_FALSE
) {
67 MOZ_COUNT_CTOR(StyleCache
);
71 MOZ_COUNT_DTOR(StyleCache
);
76 #define SIZE_STYLE_TABLE 19
78 class nsHTMLEditRules
: public nsIHTMLEditRules
, public nsTextEditRules
, public nsIEditActionListener
82 NS_DECL_ISUPPORTS_INHERITED
85 virtual ~nsHTMLEditRules();
88 // nsIEditRules methods
89 NS_IMETHOD
Init(nsPlaintextEditor
*aEditor
, PRUint32 aFlags
);
90 NS_IMETHOD
BeforeEdit(PRInt32 action
, nsIEditor::EDirection aDirection
);
91 NS_IMETHOD
AfterEdit(PRInt32 action
, nsIEditor::EDirection aDirection
);
92 NS_IMETHOD
WillDoAction(nsISelection
*aSelection
, nsRulesInfo
*aInfo
, PRBool
*aCancel
, PRBool
*aHandled
);
93 NS_IMETHOD
DidDoAction(nsISelection
*aSelection
, nsRulesInfo
*aInfo
, nsresult aResult
);
95 // nsIHTMLEditRules methods
97 NS_IMETHOD
GetListState(PRBool
*aMixed
, PRBool
*aOL
, PRBool
*aUL
, PRBool
*aDL
);
98 NS_IMETHOD
GetListItemState(PRBool
*aMixed
, PRBool
*aLI
, PRBool
*aDT
, PRBool
*aDD
);
99 NS_IMETHOD
GetIndentState(PRBool
*aCanIndent
, PRBool
*aCanOutdent
);
100 NS_IMETHOD
GetAlignment(PRBool
*aMixed
, nsIHTMLEditor::EAlignment
*aAlign
);
101 NS_IMETHOD
GetParagraphState(PRBool
*aMixed
, nsAString
&outFormat
);
102 NS_IMETHOD
MakeSureElemStartsOrEndsOnCR(nsIDOMNode
*aNode
);
104 // nsIEditActionListener methods
106 NS_IMETHOD
WillCreateNode(const nsAString
& aTag
, nsIDOMNode
*aParent
, PRInt32 aPosition
);
107 NS_IMETHOD
DidCreateNode(const nsAString
& aTag
, nsIDOMNode
*aNode
, nsIDOMNode
*aParent
, PRInt32 aPosition
, nsresult aResult
);
108 NS_IMETHOD
WillInsertNode(nsIDOMNode
*aNode
, nsIDOMNode
*aParent
, PRInt32 aPosition
);
109 NS_IMETHOD
DidInsertNode(nsIDOMNode
*aNode
, nsIDOMNode
*aParent
, PRInt32 aPosition
, nsresult aResult
);
110 NS_IMETHOD
WillDeleteNode(nsIDOMNode
*aChild
);
111 NS_IMETHOD
DidDeleteNode(nsIDOMNode
*aChild
, nsresult aResult
);
112 NS_IMETHOD
WillSplitNode(nsIDOMNode
*aExistingRightNode
, PRInt32 aOffset
);
113 NS_IMETHOD
DidSplitNode(nsIDOMNode
*aExistingRightNode
, PRInt32 aOffset
, nsIDOMNode
*aNewLeftNode
, nsresult aResult
);
114 NS_IMETHOD
WillJoinNodes(nsIDOMNode
*aLeftNode
, nsIDOMNode
*aRightNode
, nsIDOMNode
*aParent
);
115 NS_IMETHOD
DidJoinNodes(nsIDOMNode
*aLeftNode
, nsIDOMNode
*aRightNode
, nsIDOMNode
*aParent
, nsresult aResult
);
116 NS_IMETHOD
WillInsertText(nsIDOMCharacterData
*aTextNode
, PRInt32 aOffset
, const nsAString
&aString
);
117 NS_IMETHOD
DidInsertText(nsIDOMCharacterData
*aTextNode
, PRInt32 aOffset
, const nsAString
&aString
, nsresult aResult
);
118 NS_IMETHOD
WillDeleteText(nsIDOMCharacterData
*aTextNode
, PRInt32 aOffset
, PRInt32 aLength
);
119 NS_IMETHOD
DidDeleteText(nsIDOMCharacterData
*aTextNode
, PRInt32 aOffset
, PRInt32 aLength
, nsresult aResult
);
120 NS_IMETHOD
WillDeleteRange(nsIDOMRange
*aRange
);
121 NS_IMETHOD
DidDeleteRange(nsIDOMRange
*aRange
);
122 NS_IMETHOD
WillDeleteSelection(nsISelection
*aSelection
);
123 NS_IMETHOD
DidDeleteSelection(nsISelection
*aSelection
);
139 // nsHTMLEditRules implementation methods
140 nsresult
WillInsert(nsISelection
*aSelection
, PRBool
*aCancel
);
142 nsresult
DidInsert(nsISelection
*aSelection
, nsresult aResult
);
144 nsresult
WillInsertText( PRInt32 aAction
,
145 nsISelection
*aSelection
,
148 const nsAString
*inString
,
149 nsAString
*outString
,
151 nsresult
WillLoadHTML(nsISelection
*aSelection
, PRBool
*aCancel
);
152 nsresult
WillInsertBreak(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
*aHandled
);
153 nsresult
StandardBreakImpl(nsIDOMNode
*aNode
, PRInt32 aOffset
, nsISelection
*aSelection
);
154 nsresult
DidInsertBreak(nsISelection
*aSelection
, nsresult aResult
);
155 nsresult
SplitMailCites(nsISelection
*aSelection
, PRBool aPlaintext
, PRBool
*aHandled
);
156 nsresult
WillDeleteSelection(nsISelection
*aSelection
, nsIEditor::EDirection aAction
,
157 PRBool
*aCancel
, PRBool
*aHandled
);
158 nsresult
DidDeleteSelection(nsISelection
*aSelection
,
159 nsIEditor::EDirection aDir
,
161 nsresult
InsertBRIfNeeded(nsISelection
*aSelection
);
162 nsresult
GetGoodSelPointForNode(nsIDOMNode
*aNode
, nsIEditor::EDirection aAction
,
163 nsCOMPtr
<nsIDOMNode
> *outSelNode
, PRInt32
*outSelOffset
);
164 nsresult
JoinBlocks(nsCOMPtr
<nsIDOMNode
> *aLeftBlock
, nsCOMPtr
<nsIDOMNode
> *aRightBlock
, PRBool
*aCanceled
);
165 nsresult
MoveBlock(nsIDOMNode
*aLeft
, nsIDOMNode
*aRight
, PRInt32 aLeftOffset
, PRInt32 aRightOffset
);
166 nsresult
MoveNodeSmart(nsIDOMNode
*aSource
, nsIDOMNode
*aDest
, PRInt32
*aOffset
);
167 nsresult
MoveContents(nsIDOMNode
*aSource
, nsIDOMNode
*aDest
, PRInt32
*aOffset
);
168 nsresult
DeleteNonTableElements(nsIDOMNode
*aNode
);
169 nsresult
WillMakeList(nsISelection
*aSelection
, const nsAString
*aListType
, PRBool aEntireList
, const nsAString
*aBulletType
, PRBool
*aCancel
, PRBool
*aHandled
, const nsAString
*aItemType
=nsnull
);
170 nsresult
WillRemoveList(nsISelection
*aSelection
, PRBool aOrderd
, PRBool
*aCancel
, PRBool
*aHandled
);
171 nsresult
WillIndent(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
*aHandled
);
172 nsresult
WillCSSIndent(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
*aHandled
);
173 nsresult
WillHTMLIndent(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
*aHandled
);
174 nsresult
WillOutdent(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
*aHandled
);
175 nsresult
WillAlign(nsISelection
*aSelection
, const nsAString
*alignType
, PRBool
*aCancel
, PRBool
*aHandled
);
176 nsresult
WillAbsolutePosition(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
* aHandled
);
177 nsresult
WillRemoveAbsolutePosition(nsISelection
*aSelection
, PRBool
*aCancel
, PRBool
* aHandled
);
178 nsresult
WillRelativeChangeZIndex(nsISelection
*aSelection
, PRInt32 aChange
, PRBool
*aCancel
, PRBool
* aHandled
);
179 nsresult
WillMakeDefListItem(nsISelection
*aSelection
, const nsAString
*aBlockType
, PRBool aEntireList
, PRBool
*aCancel
, PRBool
*aHandled
);
180 nsresult
WillMakeBasicBlock(nsISelection
*aSelection
, const nsAString
*aBlockType
, PRBool
*aCancel
, PRBool
*aHandled
);
181 nsresult
DidMakeBasicBlock(nsISelection
*aSelection
, nsRulesInfo
*aInfo
, nsresult aResult
);
182 nsresult
DidAbsolutePosition();
183 nsresult
AlignInnerBlocks(nsIDOMNode
*aNode
, const nsAString
*alignType
);
184 nsresult
AlignBlockContents(nsIDOMNode
*aNode
, const nsAString
*alignType
);
185 nsresult
AppendInnerFormatNodes(nsCOMArray
<nsIDOMNode
>& aArray
,
187 nsresult
GetFormatString(nsIDOMNode
*aNode
, nsAString
&outFormat
);
188 nsresult
GetInnerContent(nsIDOMNode
*aNode
, nsCOMArray
<nsIDOMNode
>& outArrayOfNodes
, PRInt32
*aIndex
, PRBool aList
= PR_TRUE
, PRBool aTble
= PR_TRUE
);
189 nsCOMPtr
<nsIDOMNode
> IsInListItem(nsIDOMNode
*aNode
);
190 nsresult
ReturnInHeader(nsISelection
*aSelection
, nsIDOMNode
*aHeader
, nsIDOMNode
*aTextNode
, PRInt32 aOffset
);
191 nsresult
ReturnInParagraph(nsISelection
*aSelection
, nsIDOMNode
*aHeader
, nsIDOMNode
*aTextNode
, PRInt32 aOffset
, PRBool
*aCancel
, PRBool
*aHandled
);
192 nsresult
SplitParagraph(nsIDOMNode
*aPara
,
194 nsISelection
*aSelection
,
195 nsCOMPtr
<nsIDOMNode
> *aSelNode
,
197 nsresult
ReturnInListItem(nsISelection
*aSelection
, nsIDOMNode
*aHeader
, nsIDOMNode
*aTextNode
, PRInt32 aOffset
);
198 nsresult
AfterEditInner(PRInt32 action
, nsIEditor::EDirection aDirection
);
199 nsresult
RemovePartOfBlock(nsIDOMNode
*aBlock
,
200 nsIDOMNode
*aStartChild
,
201 nsIDOMNode
*aEndChild
,
202 nsCOMPtr
<nsIDOMNode
> *aLeftNode
= 0,
203 nsCOMPtr
<nsIDOMNode
> *aRightNode
= 0);
204 nsresult
SplitBlock(nsIDOMNode
*aBlock
,
205 nsIDOMNode
*aStartChild
,
206 nsIDOMNode
*aEndChild
,
207 nsCOMPtr
<nsIDOMNode
> *aLeftNode
= 0,
208 nsCOMPtr
<nsIDOMNode
> *aRightNode
= 0,
209 nsCOMPtr
<nsIDOMNode
> *aMiddleNode
= 0);
210 nsresult
OutdentPartOfBlock(nsIDOMNode
*aBlock
,
211 nsIDOMNode
*aStartChild
,
212 nsIDOMNode
*aEndChild
,
213 PRBool aIsBlockIndentedWithCSS
,
214 nsCOMPtr
<nsIDOMNode
> *aLeftNode
= 0,
215 nsCOMPtr
<nsIDOMNode
> *aRightNode
= 0);
216 nsresult
ConvertListType(nsIDOMNode
*aList
, nsCOMPtr
<nsIDOMNode
> *outList
, const nsAString
& aListType
, const nsAString
& aItemType
);
217 nsresult
CreateStyleForInsertText(nsISelection
*aSelection
, nsIDOMDocument
*aDoc
);
218 nsresult
IsEmptyBlock(nsIDOMNode
*aNode
,
219 PRBool
*outIsEmptyBlock
,
220 PRBool aMozBRDoesntCount
= PR_FALSE
,
221 PRBool aListItemsNotEmpty
= PR_FALSE
);
222 nsresult
CheckForEmptyBlock(nsIDOMNode
*aStartNode
,
223 nsIDOMNode
*aBodyNode
,
224 nsISelection
*aSelection
,
226 nsresult
CheckForInvisibleBR(nsIDOMNode
*aBlock
, nsHTMLEditRules::BRLocation aWhere
,
227 nsCOMPtr
<nsIDOMNode
> *outBRNode
, PRInt32 aOffset
=0);
228 PRBool
ExpandSelectionForDeletion(nsISelection
*aSelection
);
229 PRBool
IsFirstNode(nsIDOMNode
*aNode
);
230 PRBool
IsLastNode(nsIDOMNode
*aNode
);
232 PRBool
AtStartOfBlock(nsIDOMNode
*aNode
, PRInt32 aOffset
, nsIDOMNode
*aBlock
);
233 PRBool
AtEndOfBlock(nsIDOMNode
*aNode
, PRInt32 aOffset
, nsIDOMNode
*aBlock
);
235 nsresult
NormalizeSelection(nsISelection
*inSelection
);
236 nsresult
GetPromotedPoint(RulesEndpoint aWhere
, nsIDOMNode
*aNode
, PRInt32 aOffset
,
237 PRInt32 actionID
, nsCOMPtr
<nsIDOMNode
> *outNode
, PRInt32
*outOffset
);
238 nsresult
GetPromotedRanges(nsISelection
*inSelection
,
239 nsCOMArray
<nsIDOMRange
> &outArrayOfRanges
,
240 PRInt32 inOperationType
);
241 nsresult
PromoteRange(nsIDOMRange
*inRange
, PRInt32 inOperationType
);
242 nsresult
GetNodesForOperation(nsCOMArray
<nsIDOMRange
>& inArrayOfRanges
,
243 nsCOMArray
<nsIDOMNode
>& outArrayOfNodes
,
244 PRInt32 inOperationType
,
245 PRBool aDontTouchContent
=PR_FALSE
);
246 nsresult
GetChildNodesForOperation(nsIDOMNode
*inNode
,
247 nsCOMArray
<nsIDOMNode
>& outArrayOfNodes
);
248 nsresult
GetNodesFromPoint(DOMPoint point
,
250 nsCOMArray
<nsIDOMNode
>& arrayOfNodes
,
251 PRBool dontTouchContent
);
252 nsresult
GetNodesFromSelection(nsISelection
*selection
,
254 nsCOMArray
<nsIDOMNode
>& arrayOfNodes
,
255 PRBool aDontTouchContent
=PR_FALSE
);
256 nsresult
GetListActionNodes(nsCOMArray
<nsIDOMNode
> &outArrayOfNodes
, PRBool aEntireList
, PRBool aDontTouchContent
=PR_FALSE
);
257 nsresult
GetDefinitionListItemTypes(nsIDOMNode
*aNode
, PRBool
&aDT
, PRBool
&aDD
);
258 nsresult
GetParagraphFormatNodes(nsCOMArray
<nsIDOMNode
>& outArrayOfNodes
, PRBool aDontTouchContent
=PR_FALSE
);
259 nsresult
LookInsideDivBQandList(nsCOMArray
<nsIDOMNode
>& aNodeArray
);
260 nsresult
BustUpInlinesAtRangeEndpoints(nsRangeStore
&inRange
);
261 nsresult
BustUpInlinesAtBRs(nsIDOMNode
*inNode
,
262 nsCOMArray
<nsIDOMNode
>& outArrayOfNodes
);
263 nsCOMPtr
<nsIDOMNode
> GetHighestInlineParent(nsIDOMNode
* aNode
);
264 nsresult
MakeTransitionList(nsCOMArray
<nsIDOMNode
>& inArrayOfNodes
,
265 nsVoidArray
&inTransitionArray
);
266 nsresult
RemoveBlockStyle(nsCOMArray
<nsIDOMNode
>& arrayOfNodes
);
267 nsresult
ApplyBlockStyle(nsCOMArray
<nsIDOMNode
>& arrayOfNodes
, const nsAString
*aBlockTag
);
268 nsresult
MakeBlockquote(nsCOMArray
<nsIDOMNode
>& arrayOfNodes
);
269 nsresult
SplitAsNeeded(const nsAString
*aTag
, nsCOMPtr
<nsIDOMNode
> *inOutParent
, PRInt32
*inOutOffset
);
270 nsresult
AddTerminatingBR(nsIDOMNode
*aBlock
);
271 nsresult
JoinNodesSmart( nsIDOMNode
*aNodeLeft
,
272 nsIDOMNode
*aNodeRight
,
273 nsCOMPtr
<nsIDOMNode
> *aOutMergeParent
,
274 PRInt32
*aOutMergeOffset
);
275 nsresult
GetTopEnclosingMailCite(nsIDOMNode
*aNode
, nsCOMPtr
<nsIDOMNode
> *aOutCiteNode
, PRBool aPlaintext
);
276 nsresult
PopListItem(nsIDOMNode
*aListItem
, PRBool
*aOutOfList
);
277 nsresult
RemoveListStructure(nsIDOMNode
*aList
);
278 nsresult
CacheInlineStyles(nsIDOMNode
*aNode
);
279 nsresult
ReapplyCachedStyles();
280 nsresult
ClearCachedStyles();
281 nsresult
AdjustSpecialBreaks(PRBool aSafeToAskFrames
= PR_FALSE
);
282 nsresult
AdjustWhitespace(nsISelection
*aSelection
);
283 nsresult
PinSelectionToNewBlock(nsISelection
*aSelection
);
284 nsresult
CheckInterlinePosition(nsISelection
*aSelection
);
285 nsresult
AdjustSelection(nsISelection
*aSelection
, nsIEditor::EDirection aAction
);
286 nsresult
FindNearSelectableNode(nsIDOMNode
*aSelNode
,
288 nsIEditor::EDirection
&aDirection
,
289 nsCOMPtr
<nsIDOMNode
> *outSelectableNode
);
290 nsresult
InDifferentTableElements(nsIDOMNode
*aNode1
, nsIDOMNode
*aNode2
, PRBool
*aResult
);
291 nsresult
RemoveEmptyNodes();
292 nsresult
SelectionEndpointInNode(nsIDOMNode
*aNode
, PRBool
*aResult
);
293 nsresult
UpdateDocChangeRange(nsIDOMRange
*aRange
);
294 nsresult
ConfirmSelectionInBody();
295 nsresult
InsertMozBRIfNeeded(nsIDOMNode
*aNode
);
296 PRBool
IsEmptyInline(nsIDOMNode
*aNode
);
297 PRBool
ListIsEmptyLine(nsCOMArray
<nsIDOMNode
> &arrayOfNodes
);
298 nsresult
RemoveAlignment(nsIDOMNode
* aNode
, const nsAString
& aAlignType
, PRBool aChildrenOnly
);
299 nsresult
MakeSureElemStartsOrEndsOnCR(nsIDOMNode
*aNode
, PRBool aStarts
);
300 nsresult
AlignBlock(nsIDOMElement
* aElement
, const nsAString
* aAlignType
, PRBool aContentsOnly
);
301 nsresult
RelativeChangeIndentationOfElementNode(nsIDOMNode
*aNode
, PRInt8 aRelativeChange
);
305 nsHTMLEditor
*mHTMLEditor
;
306 nsCOMPtr
<nsIDOMRange
> mDocChangeRange
;
307 PRPackedBool mListenerEnabled
;
308 PRPackedBool mReturnInEmptyLIKillsList
;
309 PRPackedBool mDidDeleteSelection
;
310 PRPackedBool mDidRangedDelete
;
311 nsCOMPtr
<nsIDOMRange
> mUtilRange
;
312 PRUint32 mJoinOffset
; // need to remember an int across willJoin/didJoin...
313 nsCOMPtr
<nsIDOMNode
> mNewBlock
;
314 nsRangeStore mRangeItem
;
315 StyleCache mCachedStyles
[SIZE_STYLE_TABLE
];
318 nsresult
NS_NewHTMLEditRules(nsIEditRules
** aInstancePtrResult
);
320 #endif //nsHTMLEditRules_h__