editor/libeditor/html/nsHTMLEditor.h

changeset 2
7e26c7da4463
equal deleted inserted replaced
-1:000000000000 0:1e381bbf3355
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #ifndef nsHTMLEditor_h__
7 #define nsHTMLEditor_h__
8
9 #include "nsCOMPtr.h"
10 #include "nsCOMArray.h"
11 #include "nsPlaintextEditor.h"
12 #include "nsIEditor.h"
13 #include "nsIHTMLEditor.h"
14 #include "nsITableEditor.h"
15 #include "nsIEditorMailSupport.h"
16 #include "nsIEditorStyleSheets.h"
17
18 #include "nsEditor.h"
19 #include "nsIDOMElement.h"
20 #include "nsIDOMEventListener.h"
21 #include "nsICSSLoaderObserver.h"
22
23 #include "nsEditRules.h"
24
25 #include "nsEditProperty.h"
26 #include "nsHTMLCSSUtils.h"
27
28 #include "nsHTMLObjectResizer.h"
29 #include "nsIHTMLAbsPosEditor.h"
30 #include "nsIHTMLInlineTableEditor.h"
31 #include "nsIHTMLObjectResizeListener.h"
32 #include "nsIHTMLObjectResizer.h"
33
34 #include "nsIDocumentObserver.h"
35
36 #include "nsPoint.h"
37 #include "nsTArray.h"
38 #include "nsAutoPtr.h"
39 #include "nsAttrName.h"
40 #include "nsStubMutationObserver.h"
41
42 #include "mozilla/Attributes.h"
43 #include "mozilla/dom/Element.h"
44
45 class nsIDOMKeyEvent;
46 class nsITransferable;
47 class nsIDocumentEncoder;
48 class nsIClipboard;
49 class TypeInState;
50 class nsIContentFilter;
51 class nsIURL;
52 class nsILinkHandler;
53 class nsTableOuterFrame;
54 struct PropItem;
55
56 namespace mozilla {
57 namespace widget {
58 struct IMEState;
59 } // namespace widget
60 } // namespace mozilla
61
62 /**
63 * The HTML editor implementation.<br>
64 * Use to edit HTML document represented as a DOM tree.
65 */
66 class nsHTMLEditor : public nsPlaintextEditor,
67 public nsIHTMLEditor,
68 public nsIHTMLObjectResizer,
69 public nsIHTMLAbsPosEditor,
70 public nsITableEditor,
71 public nsIHTMLInlineTableEditor,
72 public nsIEditorStyleSheets,
73 public nsICSSLoaderObserver,
74 public nsStubMutationObserver
75 {
76 typedef enum {eNoOp, eReplaceParent=1, eInsertParent=2} BlockTransformationType;
77
78 public:
79
80 enum ResizingRequestID
81 {
82 kX = 0,
83 kY = 1,
84 kWidth = 2,
85 kHeight = 3
86 };
87
88 // see nsIHTMLEditor for documentation
89
90 //Interfaces for addref and release and queryinterface
91 //NOTE macro used is for classes that inherit from
92 // another class. Only the base class should use NS_DECL_ISUPPORTS
93 NS_DECL_ISUPPORTS_INHERITED
94 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsHTMLEditor, nsPlaintextEditor)
95
96
97 nsHTMLEditor();
98 virtual ~nsHTMLEditor();
99
100 bool GetReturnInParagraphCreatesNewParagraph();
101
102 /* ------------ nsPlaintextEditor overrides -------------- */
103 NS_IMETHOD GetIsDocumentEditable(bool *aIsDocumentEditable);
104 NS_IMETHOD BeginningOfDocument();
105 virtual nsresult HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent);
106 virtual already_AddRefed<nsIContent> GetFocusedContent();
107 virtual already_AddRefed<nsIContent> GetFocusedContentForIME();
108 virtual bool IsActiveInDOMWindow();
109 virtual already_AddRefed<mozilla::dom::EventTarget> GetDOMEventTarget();
110 virtual mozilla::dom::Element* GetEditorRoot() MOZ_OVERRIDE;
111 virtual already_AddRefed<nsIContent> FindSelectionRoot(nsINode *aNode);
112 virtual bool IsAcceptableInputEvent(nsIDOMEvent* aEvent);
113 virtual already_AddRefed<nsIContent> GetInputEventTargetContent();
114 virtual bool IsEditable(nsIContent *aNode);
115 using nsEditor::IsEditable;
116
117 /* ------------ nsStubMutationObserver overrides --------- */
118 NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
119 NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
120 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
121
122 /* ------------ nsIEditorIMESupport overrides ------------ */
123 NS_IMETHOD GetPreferredIMEState(mozilla::widget::IMEState *aState);
124
125 /* ------------ nsIHTMLEditor methods -------------- */
126
127 NS_DECL_NSIHTMLEDITOR
128
129 /* ------------ nsIHTMLObjectResizer methods -------------- */
130 /* -------- Implemented in nsHTMLObjectResizer.cpp -------- */
131 NS_DECL_NSIHTMLOBJECTRESIZER
132
133 /* ------------ nsIHTMLAbsPosEditor methods -------------- */
134 /* -------- Implemented in nsHTMLAbsPosition.cpp --------- */
135 NS_DECL_NSIHTMLABSPOSEDITOR
136
137 /* ------------ nsIHTMLInlineTableEditor methods -------------- */
138 /* ------- Implemented in nsHTMLInlineTableEditor.cpp --------- */
139 NS_DECL_NSIHTMLINLINETABLEEDITOR
140
141 /* ------------ nsIHTMLEditor methods -------------- */
142 NS_IMETHOD CopyLastEditableChildStyles(nsIDOMNode *aPreviousBlock, nsIDOMNode *aNewBlock,
143 nsIDOMNode **aOutBrNode);
144
145 NS_IMETHOD LoadHTML(const nsAString &aInputString);
146
147 nsresult GetCSSBackgroundColorState(bool *aMixed, nsAString &aOutColor,
148 bool aBlockLevel);
149 NS_IMETHOD GetHTMLBackgroundColorState(bool *aMixed, nsAString &outColor);
150
151 /* ------------ nsIEditorStyleSheets methods -------------- */
152
153 NS_IMETHOD AddStyleSheet(const nsAString & aURL);
154 NS_IMETHOD ReplaceStyleSheet(const nsAString& aURL);
155 NS_IMETHOD RemoveStyleSheet(const nsAString &aURL);
156
157 NS_IMETHOD AddOverrideStyleSheet(const nsAString & aURL);
158 NS_IMETHOD ReplaceOverrideStyleSheet(const nsAString& aURL);
159 NS_IMETHOD RemoveOverrideStyleSheet(const nsAString &aURL);
160
161 NS_IMETHOD EnableStyleSheet(const nsAString& aURL, bool aEnable);
162
163 /* ------------ nsIEditorMailSupport methods -------------- */
164
165 NS_DECL_NSIEDITORMAILSUPPORT
166
167 /* ------------ nsITableEditor methods -------------- */
168
169 NS_IMETHOD InsertTableCell(int32_t aNumber, bool aAfter);
170 NS_IMETHOD InsertTableColumn(int32_t aNumber, bool aAfter);
171 NS_IMETHOD InsertTableRow(int32_t aNumber, bool aAfter);
172 NS_IMETHOD DeleteTable();
173 NS_IMETHOD DeleteTableCell(int32_t aNumber);
174 NS_IMETHOD DeleteTableCellContents();
175 NS_IMETHOD DeleteTableColumn(int32_t aNumber);
176 NS_IMETHOD DeleteTableRow(int32_t aNumber);
177 NS_IMETHOD SelectTableCell();
178 NS_IMETHOD SelectBlockOfCells(nsIDOMElement *aStartCell, nsIDOMElement *aEndCell);
179 NS_IMETHOD SelectTableRow();
180 NS_IMETHOD SelectTableColumn();
181 NS_IMETHOD SelectTable();
182 NS_IMETHOD SelectAllTableCells();
183 NS_IMETHOD SwitchTableCellHeaderType(nsIDOMElement *aSourceCell, nsIDOMElement **aNewCell);
184 NS_IMETHOD JoinTableCells(bool aMergeNonContiguousContents);
185 NS_IMETHOD SplitTableCell();
186 NS_IMETHOD NormalizeTable(nsIDOMElement *aTable);
187 NS_IMETHOD GetCellIndexes(nsIDOMElement *aCell,
188 int32_t* aRowIndex, int32_t* aColIndex);
189 NS_IMETHOD GetTableSize(nsIDOMElement *aTable,
190 int32_t* aRowCount, int32_t* aColCount);
191 NS_IMETHOD GetCellAt(nsIDOMElement* aTable, int32_t aRowIndex, int32_t aColIndex, nsIDOMElement **aCell);
192 NS_IMETHOD GetCellDataAt(nsIDOMElement* aTable,
193 int32_t aRowIndex, int32_t aColIndex,
194 nsIDOMElement **aCell,
195 int32_t* aStartRowIndex, int32_t* aStartColIndex,
196 int32_t* aRowSpan, int32_t* aColSpan,
197 int32_t* aActualRowSpan, int32_t* aActualColSpan,
198 bool* aIsSelected);
199 NS_IMETHOD GetFirstRow(nsIDOMElement* aTableElement, nsIDOMNode** aRowNode);
200 NS_IMETHOD GetNextRow(nsIDOMNode* aCurrentRowNode, nsIDOMNode** aRowNode);
201 NS_IMETHOD GetLastCellInRow(nsIDOMNode* aRowNode, nsIDOMNode** aCellNode);
202
203 NS_IMETHOD SetSelectionAfterTableEdit(nsIDOMElement* aTable, int32_t aRow, int32_t aCol,
204 int32_t aDirection, bool aSelected);
205 NS_IMETHOD GetSelectedOrParentTableElement(nsAString& aTagName,
206 int32_t *aSelectedCount,
207 nsIDOMElement** aTableElement);
208 NS_IMETHOD GetSelectedCellsType(nsIDOMElement *aElement, uint32_t *aSelectionType);
209
210 nsresult GetCellFromRange(nsIDOMRange *aRange, nsIDOMElement **aCell);
211
212 // Finds the first selected cell in first range of selection
213 // This is in the *order of selection*, not order in the table
214 // (i.e., each cell added to selection is added in another range
215 // in the selection's rangelist, independent of location in table)
216 // aRange is optional: returns the range around the cell
217 NS_IMETHOD GetFirstSelectedCell(nsIDOMRange **aRange, nsIDOMElement **aCell);
218 // Get next cell until no more are found. Always use GetFirstSelected cell first
219 // aRange is optional: returns the range around the cell
220 NS_IMETHOD GetNextSelectedCell(nsIDOMRange **aRange, nsIDOMElement **aCell);
221
222 // Upper-left-most selected cell in table
223 NS_IMETHOD GetFirstSelectedCellInTable(int32_t *aRowIndex, int32_t *aColIndex, nsIDOMElement **aCell);
224
225 /* miscellaneous */
226 // This sets background on the appropriate container element (table, cell,)
227 // or calls into nsTextEditor to set the page background
228 NS_IMETHOD SetCSSBackgroundColor(const nsAString& aColor);
229 NS_IMETHOD SetHTMLBackgroundColor(const nsAString& aColor);
230
231 /* ------------ Block methods moved from nsEditor -------------- */
232 static already_AddRefed<nsIDOMNode> GetBlockNodeParent(nsIDOMNode *aNode);
233
234 void IsNextCharInNodeWhitespace(nsIContent* aContent,
235 int32_t aOffset,
236 bool* outIsSpace,
237 bool* outIsNBSP,
238 nsIContent** outNode = nullptr,
239 int32_t* outOffset = 0);
240 void IsPrevCharInNodeWhitespace(nsIContent* aContent,
241 int32_t aOffset,
242 bool* outIsSpace,
243 bool* outIsNBSP,
244 nsIContent** outNode = nullptr,
245 int32_t* outOffset = 0);
246
247 /* ------------ Overrides of nsEditor interface methods -------------- */
248
249 nsresult EndUpdateViewBatch();
250
251 /** prepare the editor for use */
252 NS_IMETHOD Init(nsIDOMDocument *aDoc, nsIContent *aRoot,
253 nsISelectionController *aSelCon, uint32_t aFlags,
254 const nsAString& aValue);
255 NS_IMETHOD PreDestroy(bool aDestroyingFrames);
256
257 /** Internal, static version */
258 // aElement must not be null.
259 static bool NodeIsBlockStatic(const mozilla::dom::Element* aElement);
260 static nsresult NodeIsBlockStatic(nsIDOMNode *aNode, bool *aIsBlock);
261 protected:
262 using nsEditor::IsBlockNode;
263 virtual bool IsBlockNode(nsINode *aNode);
264
265 public:
266 NS_IMETHOD SetFlags(uint32_t aFlags);
267
268 NS_IMETHOD Paste(int32_t aSelectionType);
269 NS_IMETHOD CanPaste(int32_t aSelectionType, bool *aCanPaste);
270
271 NS_IMETHOD PasteTransferable(nsITransferable *aTransferable);
272 NS_IMETHOD CanPasteTransferable(nsITransferable *aTransferable, bool *aCanPaste);
273
274 NS_IMETHOD DebugUnitTests(int32_t *outNumTests, int32_t *outNumTestsFailed);
275
276 /** All editor operations which alter the doc should be prefaced
277 * with a call to StartOperation, naming the action and direction */
278 NS_IMETHOD StartOperation(EditAction opID,
279 nsIEditor::EDirection aDirection);
280
281 /** All editor operations which alter the doc should be followed
282 * with a call to EndOperation */
283 NS_IMETHOD EndOperation();
284
285 /** returns true if aParentTag can contain a child of type aChildTag */
286 virtual bool TagCanContainTag(nsIAtom* aParentTag, nsIAtom* aChildTag);
287
288 /** returns true if aNode is a container */
289 virtual bool IsContainer(nsIDOMNode *aNode);
290
291 /** make the given selection span the entire document */
292 NS_IMETHOD SelectEntireDocument(nsISelection *aSelection);
293
294 NS_IMETHOD SetAttributeOrEquivalent(nsIDOMElement * aElement,
295 const nsAString & aAttribute,
296 const nsAString & aValue,
297 bool aSuppressTransaction);
298 NS_IMETHOD RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
299 const nsAString & aAttribute,
300 bool aSuppressTransaction);
301
302 /** join together any adjacent editable text nodes in the range */
303 NS_IMETHOD CollapseAdjacentTextNodes(nsIDOMRange *aInRange);
304
305 virtual bool AreNodesSameType(nsIContent* aNode1, nsIContent* aNode2)
306 MOZ_OVERRIDE;
307
308 NS_IMETHOD DeleteSelectionImpl(EDirection aAction,
309 EStripWrappers aStripWrappers);
310 nsresult DeleteNode(nsINode* aNode);
311 NS_IMETHODIMP DeleteNode(nsIDOMNode * aNode);
312 NS_IMETHODIMP DeleteText(nsIDOMCharacterData *aTextNode,
313 uint32_t aOffset,
314 uint32_t aLength);
315 NS_IMETHOD InsertTextImpl(const nsAString& aStringToInsert,
316 nsCOMPtr<nsIDOMNode> *aInOutNode,
317 int32_t *aInOutOffset,
318 nsIDOMDocument *aDoc);
319 NS_IMETHOD_(bool) IsModifiableNode(nsIDOMNode *aNode);
320 virtual bool IsModifiableNode(nsINode *aNode);
321
322 NS_IMETHOD GetIsSelectionEditable(bool* aIsSelectionEditable);
323
324 NS_IMETHOD SelectAll();
325
326 NS_IMETHOD GetRootElement(nsIDOMElement **aRootElement);
327
328 /* ------------ nsICSSLoaderObserver -------------- */
329 NS_IMETHOD StyleSheetLoaded(nsCSSStyleSheet*aSheet, bool aWasAlternate,
330 nsresult aStatus);
331
332 /* ------------ Utility Routines, not part of public API -------------- */
333 NS_IMETHOD TypedText(const nsAString& aString, ETypingAction aAction);
334 nsresult InsertNodeAtPoint( nsIDOMNode *aNode,
335 nsCOMPtr<nsIDOMNode> *ioParent,
336 int32_t *ioOffset,
337 bool aNoEmptyNodes);
338
339 // Use this to assure that selection is set after attribute nodes when
340 // trying to collapse selection at begining of a block node
341 // e.g., when setting at beginning of a table cell
342 // This will stop at a table, however, since we don't want to
343 // "drill down" into nested tables.
344 // aSelection is optional -- if null, we get current seletion
345 nsresult CollapseSelectionToDeepestNonTableFirstChild(nsISelection *aSelection, nsIDOMNode *aNode);
346
347 /**
348 * aNode must be a non-null text node.
349 * outIsEmptyNode must be non-null.
350 */
351 nsresult IsVisTextNode(nsIContent* aNode,
352 bool* outIsEmptyNode,
353 bool aSafeToAskFrames);
354 nsresult IsEmptyNode(nsIDOMNode *aNode, bool *outIsEmptyBlock,
355 bool aMozBRDoesntCount = false,
356 bool aListOrCellNotEmpty = false,
357 bool aSafeToAskFrames = false);
358 nsresult IsEmptyNode(nsINode* aNode, bool* outIsEmptyBlock,
359 bool aMozBRDoesntCount = false,
360 bool aListOrCellNotEmpty = false,
361 bool aSafeToAskFrames = false);
362 nsresult IsEmptyNodeImpl(nsINode* aNode,
363 bool *outIsEmptyBlock,
364 bool aMozBRDoesntCount,
365 bool aListOrCellNotEmpty,
366 bool aSafeToAskFrames,
367 bool *aSeenBR);
368
369 // Returns TRUE if sheet was loaded, false if it wasn't
370 bool EnableExistingStyleSheet(const nsAString& aURL);
371
372 // Dealing with the internal style sheet lists:
373 NS_IMETHOD GetStyleSheetForURL(const nsAString &aURL,
374 nsCSSStyleSheet **_retval);
375 NS_IMETHOD GetURLForStyleSheet(nsCSSStyleSheet *aStyleSheet, nsAString &aURL);
376
377 // Add a url + known style sheet to the internal lists:
378 nsresult AddNewStyleSheetToList(const nsAString &aURL,
379 nsCSSStyleSheet *aStyleSheet);
380
381 nsresult RemoveStyleSheetFromList(const nsAString &aURL);
382
383 bool IsCSSEnabled()
384 {
385 // TODO: removal of mCSSAware and use only the presence of mHTMLCSSUtils
386 return mCSSAware && mHTMLCSSUtils && mHTMLCSSUtils->IsCSSPrefChecked();
387 }
388
389 static bool HasAttributes(mozilla::dom::Element* aElement)
390 {
391 MOZ_ASSERT(aElement);
392 uint32_t attrCount = aElement->GetAttrCount();
393 return attrCount > 1 ||
394 (1 == attrCount && !aElement->GetAttrNameAt(0)->Equals(nsGkAtoms::mozdirty));
395 }
396
397 protected:
398
399 NS_IMETHOD InitRules();
400
401 // Create the event listeners for the editor to install
402 virtual void CreateEventListeners();
403
404 virtual nsresult InstallEventListeners();
405 virtual void RemoveEventListeners();
406
407 bool ShouldReplaceRootElement();
408 void ResetRootElementAndEventTarget();
409 nsresult GetBodyElement(nsIDOMHTMLElement** aBody);
410 // Get the focused node of this editor.
411 // @return If the editor has focus, this returns the focused node.
412 // Otherwise, returns null.
413 already_AddRefed<nsINode> GetFocusedNode();
414
415 // Return TRUE if aElement is a table-related elemet and caret was set
416 bool SetCaretInTableCell(nsIDOMElement* aElement);
417
418 // key event helpers
419 NS_IMETHOD TabInTable(bool inIsShift, bool *outHandled);
420 NS_IMETHOD CreateBR(nsIDOMNode *aNode, int32_t aOffset,
421 nsCOMPtr<nsIDOMNode> *outBRNode, nsIEditor::EDirection aSelect = nsIEditor::eNone);
422
423 // Table Editing (implemented in nsTableEditor.cpp)
424
425 // Table utilities
426
427 // Insert a new cell after or before supplied aCell.
428 // Optional: If aNewCell supplied, returns the newly-created cell (addref'd, of course)
429 // This doesn't change or use the current selection
430 NS_IMETHOD InsertCell(nsIDOMElement *aCell, int32_t aRowSpan, int32_t aColSpan,
431 bool aAfter, bool aIsHeader, nsIDOMElement **aNewCell);
432
433 // Helpers that don't touch the selection or do batch transactions
434 NS_IMETHOD DeleteRow(nsIDOMElement *aTable, int32_t aRowIndex);
435 NS_IMETHOD DeleteColumn(nsIDOMElement *aTable, int32_t aColIndex);
436 NS_IMETHOD DeleteCellContents(nsIDOMElement *aCell);
437
438 // Move all contents from aCellToMerge into aTargetCell (append at end)
439 NS_IMETHOD MergeCells(nsCOMPtr<nsIDOMElement> aTargetCell, nsCOMPtr<nsIDOMElement> aCellToMerge, bool aDeleteCellToMerge);
440
441 NS_IMETHOD DeleteTable2(nsIDOMElement *aTable, nsISelection *aSelection);
442 NS_IMETHOD SetColSpan(nsIDOMElement *aCell, int32_t aColSpan);
443 NS_IMETHOD SetRowSpan(nsIDOMElement *aCell, int32_t aRowSpan);
444
445 // Helper used to get nsTableOuterFrame for a table.
446 nsTableOuterFrame* GetTableFrame(nsIDOMElement* aTable);
447 // Needed to do appropriate deleting when last cell or row is about to be deleted
448 // This doesn't count cells that don't start in the given row (are spanning from row above)
449 int32_t GetNumberOfCellsInRow(nsIDOMElement* aTable, int32_t rowIndex);
450 // Test if all cells in row or column at given index are selected
451 bool AllCellsInRowSelected(nsIDOMElement *aTable, int32_t aRowIndex, int32_t aNumberOfColumns);
452 bool AllCellsInColumnSelected(nsIDOMElement *aTable, int32_t aColIndex, int32_t aNumberOfRows);
453
454 bool IsEmptyCell(mozilla::dom::Element* aCell);
455
456 // Most insert methods need to get the same basic context data
457 // Any of the pointers may be null if you don't need that datum (for more efficiency)
458 // Input: *aCell is a known cell,
459 // if null, cell is obtained from the anchor node of the selection
460 // Returns NS_EDITOR_ELEMENT_NOT_FOUND if cell is not found even if aCell is null
461 NS_IMETHOD GetCellContext(nsISelection **aSelection,
462 nsIDOMElement **aTable,
463 nsIDOMElement **aCell,
464 nsIDOMNode **aCellParent, int32_t *aCellOffset,
465 int32_t *aRowIndex, int32_t *aColIndex);
466
467 NS_IMETHOD GetCellSpansAt(nsIDOMElement* aTable, int32_t aRowIndex, int32_t aColIndex,
468 int32_t& aActualRowSpan, int32_t& aActualColSpan);
469
470 NS_IMETHOD SplitCellIntoColumns(nsIDOMElement *aTable, int32_t aRowIndex, int32_t aColIndex,
471 int32_t aColSpanLeft, int32_t aColSpanRight, nsIDOMElement **aNewCell);
472
473 NS_IMETHOD SplitCellIntoRows(nsIDOMElement *aTable, int32_t aRowIndex, int32_t aColIndex,
474 int32_t aRowSpanAbove, int32_t aRowSpanBelow, nsIDOMElement **aNewCell);
475
476 nsresult CopyCellBackgroundColor(nsIDOMElement *destCell, nsIDOMElement *sourceCell);
477
478 // Reduce rowspan/colspan when cells span into nonexistent rows/columns
479 NS_IMETHOD FixBadRowSpan(nsIDOMElement *aTable, int32_t aRowIndex, int32_t& aNewRowCount);
480 NS_IMETHOD FixBadColSpan(nsIDOMElement *aTable, int32_t aColIndex, int32_t& aNewColCount);
481
482 // Fallback method: Call this after using ClearSelection() and you
483 // failed to set selection to some other content in the document
484 NS_IMETHOD SetSelectionAtDocumentStart(nsISelection *aSelection);
485
486 // End of Table Editing utilities
487
488 static nsCOMPtr<nsIDOMNode> GetEnclosingTable(nsIDOMNode *aNode);
489
490 /** content-based query returns true if <aProperty aAttribute=aValue> effects aNode
491 * If <aProperty aAttribute=aValue> contains aNode,
492 * but <aProperty aAttribute=SomeOtherValue> also contains aNode and the second is
493 * more deeply nested than the first, then the first does not effect aNode.
494 *
495 * @param aNode The target of the query
496 * @param aProperty The property that we are querying for
497 * @param aAttribute The attribute of aProperty, example: color in <FONT color="blue">
498 * May be null.
499 * @param aValue The value of aAttribute, example: blue in <FONT color="blue">
500 * May be null. Ignored if aAttribute is null.
501 * @param aIsSet [OUT] true if <aProperty aAttribute=aValue> effects aNode.
502 * @param outValue [OUT] the value of the attribute, if aIsSet is true
503 *
504 * The nsIContent variant returns aIsSet instead of using an out parameter.
505 */
506 bool IsTextPropertySetByContent(nsIContent* aContent,
507 nsIAtom* aProperty,
508 const nsAString* aAttribute,
509 const nsAString* aValue,
510 nsAString* outValue = nullptr);
511
512 void IsTextPropertySetByContent(nsIDOMNode* aNode,
513 nsIAtom* aProperty,
514 const nsAString* aAttribute,
515 const nsAString* aValue,
516 bool& aIsSet,
517 nsAString* outValue = nullptr);
518
519 // Methods for handling plaintext quotations
520 NS_IMETHOD PasteAsPlaintextQuotation(int32_t aSelectionType);
521
522 /** Insert a string as quoted text,
523 * replacing the selected text (if any).
524 * @param aQuotedText The string to insert.
525 * @param aAddCites Whether to prepend extra ">" to each line
526 * (usually true, unless those characters
527 * have already been added.)
528 * @return aNodeInserted The node spanning the insertion, if applicable.
529 * If aAddCites is false, this will be null.
530 */
531 NS_IMETHOD InsertAsPlaintextQuotation(const nsAString & aQuotedText,
532 bool aAddCites,
533 nsIDOMNode **aNodeInserted);
534
535 nsresult InsertObject(const char* aType, nsISupports* aObject, bool aIsSafe,
536 nsIDOMDocument *aSourceDoc,
537 nsIDOMNode *aDestinationNode,
538 int32_t aDestOffset,
539 bool aDoDeleteSelection);
540
541 // factored methods for handling insertion of data from transferables (drag&drop or clipboard)
542 NS_IMETHOD PrepareTransferable(nsITransferable **transferable);
543 NS_IMETHOD PrepareHTMLTransferable(nsITransferable **transferable, bool havePrivFlavor);
544 NS_IMETHOD InsertFromTransferable(nsITransferable *transferable,
545 nsIDOMDocument *aSourceDoc,
546 const nsAString & aContextStr,
547 const nsAString & aInfoStr,
548 nsIDOMNode *aDestinationNode,
549 int32_t aDestinationOffset,
550 bool aDoDeleteSelection);
551 nsresult InsertFromDataTransfer(mozilla::dom::DataTransfer *aDataTransfer,
552 int32_t aIndex,
553 nsIDOMDocument *aSourceDoc,
554 nsIDOMNode *aDestinationNode,
555 int32_t aDestOffset,
556 bool aDoDeleteSelection);
557 bool HavePrivateHTMLFlavor( nsIClipboard *clipboard );
558 nsresult ParseCFHTML(nsCString & aCfhtml, char16_t **aStuffToPaste, char16_t **aCfcontext);
559 nsresult DoContentFilterCallback(const nsAString &aFlavor,
560 nsIDOMDocument *aSourceDoc,
561 bool aWillDeleteSelection,
562 nsIDOMNode **aFragmentAsNode,
563 nsIDOMNode **aFragStartNode,
564 int32_t *aFragStartOffset,
565 nsIDOMNode **aFragEndNode,
566 int32_t *aFragEndOffset,
567 nsIDOMNode **aTargetNode,
568 int32_t *aTargetOffset,
569 bool *aDoContinue);
570
571 bool IsInLink(nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *outLink = nullptr);
572 nsresult StripFormattingNodes(nsIDOMNode *aNode, bool aOnlyList = false);
573 nsresult CreateDOMFragmentFromPaste(const nsAString & aInputString,
574 const nsAString & aContextStr,
575 const nsAString & aInfoStr,
576 nsCOMPtr<nsIDOMNode> *outFragNode,
577 nsCOMPtr<nsIDOMNode> *outStartNode,
578 nsCOMPtr<nsIDOMNode> *outEndNode,
579 int32_t *outStartOffset,
580 int32_t *outEndOffset,
581 bool aTrustedInput);
582 nsresult ParseFragment(const nsAString & aStr, nsIAtom* aContextLocalName,
583 nsIDocument* aTargetDoc,
584 nsCOMPtr<nsIDOMNode> *outNode,
585 bool aTrustedInput);
586 nsresult CreateListOfNodesToPaste(nsIDOMNode *aFragmentAsNode,
587 nsCOMArray<nsIDOMNode>& outNodeList,
588 nsIDOMNode *aStartNode,
589 int32_t aStartOffset,
590 nsIDOMNode *aEndNode,
591 int32_t aEndOffset);
592 nsresult CreateTagStack(nsTArray<nsString> &aTagStack,
593 nsIDOMNode *aNode);
594 nsresult GetListAndTableParents( bool aEnd,
595 nsCOMArray<nsIDOMNode>& aListOfNodes,
596 nsCOMArray<nsIDOMNode>& outArray);
597 nsresult DiscoverPartialListsAndTables(nsCOMArray<nsIDOMNode>& aPasteNodes,
598 nsCOMArray<nsIDOMNode>& aListsAndTables,
599 int32_t *outHighWaterMark);
600 nsresult ScanForListAndTableStructure(bool aEnd,
601 nsCOMArray<nsIDOMNode>& aNodes,
602 nsIDOMNode *aListOrTable,
603 nsCOMPtr<nsIDOMNode> *outReplaceNode);
604 nsresult ReplaceOrphanedStructure( bool aEnd,
605 nsCOMArray<nsIDOMNode>& aNodeArray,
606 nsCOMArray<nsIDOMNode>& aListAndTableArray,
607 int32_t aHighWaterMark);
608 nsIDOMNode* GetArrayEndpoint(bool aEnd, nsCOMArray<nsIDOMNode>& aNodeArray);
609
610 /* small utility routine to test if a break node is visible to user */
611 bool IsVisBreak(nsIDOMNode *aNode);
612
613 /* utility routine to possibly adjust the insertion position when
614 inserting a block level element */
615 void NormalizeEOLInsertPosition(nsIDOMNode *firstNodeToInsert,
616 nsCOMPtr<nsIDOMNode> *insertParentNode,
617 int32_t *insertOffset);
618
619 /* small utility routine to test the eEditorReadonly bit */
620 bool IsModifiable();
621
622 /* helpers for block transformations */
623 nsresult MakeDefinitionItem(const nsAString & aItemType);
624 nsresult InsertBasicBlock(const nsAString & aBlockType);
625
626 /* increase/decrease the font size of selection */
627 nsresult RelativeFontChange( int32_t aSizeChange);
628
629 /* helper routines for font size changing */
630 nsresult RelativeFontChangeOnTextNode( int32_t aSizeChange,
631 nsIDOMCharacterData *aTextNode,
632 int32_t aStartOffset,
633 int32_t aEndOffset);
634 nsresult RelativeFontChangeOnNode(int32_t aSizeChange, nsINode* aNode);
635 nsresult RelativeFontChangeHelper(int32_t aSizeChange, nsINode* aNode);
636
637 /* helper routines for inline style */
638 nsresult SetInlinePropertyOnTextNode( nsIDOMCharacterData *aTextNode,
639 int32_t aStartOffset,
640 int32_t aEndOffset,
641 nsIAtom *aProperty,
642 const nsAString *aAttribute,
643 const nsAString *aValue);
644 nsresult SetInlinePropertyOnNode( nsIDOMNode *aNode,
645 nsIAtom *aProperty,
646 const nsAString *aAttribute,
647 const nsAString *aValue);
648 nsresult SetInlinePropertyOnNode(nsIContent* aNode,
649 nsIAtom* aProperty,
650 const nsAString* aAttribute,
651 const nsAString* aValue);
652
653 nsresult PromoteInlineRange(nsIDOMRange *inRange);
654 nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsIDOMRange *inRange);
655 nsresult SplitStyleAboveRange(nsIDOMRange *aRange,
656 nsIAtom *aProperty,
657 const nsAString *aAttribute);
658 nsresult SplitStyleAbovePoint(nsCOMPtr<nsIDOMNode> *aNode,
659 int32_t *aOffset,
660 nsIAtom *aProperty,
661 const nsAString *aAttribute,
662 nsCOMPtr<nsIDOMNode> *outLeftNode = nullptr,
663 nsCOMPtr<nsIDOMNode> *outRightNode = nullptr);
664 nsresult ApplyDefaultProperties();
665 nsresult RemoveStyleInside(nsIDOMNode *aNode,
666 nsIAtom *aProperty,
667 const nsAString *aAttribute,
668 const bool aChildrenOnly = false);
669 nsresult RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAString *aAttribute);
670
671 bool NodeIsProperty(nsIDOMNode *aNode);
672 bool HasAttr(nsIDOMNode *aNode, const nsAString *aAttribute);
673 bool IsAtFrontOfNode(nsIDOMNode *aNode, int32_t aOffset);
674 bool IsAtEndOfNode(nsIDOMNode *aNode, int32_t aOffset);
675 bool IsOnlyAttribute(nsIDOMNode *aElement, const nsAString *aAttribute);
676 bool IsOnlyAttribute(const nsIContent* aElement, const nsAString& aAttribute);
677
678 nsresult RemoveBlockContainer(nsIDOMNode *inNode);
679
680 nsIContent* GetPriorHTMLSibling(nsINode* aNode);
681 nsresult GetPriorHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode);
682 nsIContent* GetPriorHTMLSibling(nsINode* aParent, int32_t aOffset);
683 nsresult GetPriorHTMLSibling(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode);
684
685 nsIContent* GetNextHTMLSibling(nsINode* aNode);
686 nsresult GetNextHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode);
687 nsIContent* GetNextHTMLSibling(nsINode* aParent, int32_t aOffset);
688 nsresult GetNextHTMLSibling(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode);
689
690 nsIContent* GetPriorHTMLNode(nsINode* aNode, bool aNoBlockCrossing = false);
691 nsresult GetPriorHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing = false);
692 nsIContent* GetPriorHTMLNode(nsINode* aParent, int32_t aOffset,
693 bool aNoBlockCrossing = false);
694 nsresult GetPriorHTMLNode(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing = false);
695
696 nsIContent* GetNextHTMLNode(nsINode* aNode, bool aNoBlockCrossing = false);
697 nsresult GetNextHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing = false);
698 nsIContent* GetNextHTMLNode(nsINode* aParent, int32_t aOffset,
699 bool aNoBlockCrossing = false);
700 nsresult GetNextHTMLNode(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing = false);
701
702 nsresult IsFirstEditableChild( nsIDOMNode *aNode, bool *aOutIsFirst);
703 nsresult IsLastEditableChild( nsIDOMNode *aNode, bool *aOutIsLast);
704 nsresult GetFirstEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstChild);
705 nsresult GetLastEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastChild);
706
707 nsresult GetFirstEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstLeaf);
708 nsresult GetLastEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastLeaf);
709
710 nsresult GetInlinePropertyBase(nsIAtom *aProperty,
711 const nsAString *aAttribute,
712 const nsAString *aValue,
713 bool *aFirst,
714 bool *aAny,
715 bool *aAll,
716 nsAString *outValue,
717 bool aCheckDefaults = true);
718 bool HasStyleOrIdOrClass(mozilla::dom::Element* aElement);
719 nsresult RemoveElementIfNoStyleOrIdOrClass(nsIDOMNode* aElement);
720
721 // Whether the outer window of the DOM event target has focus or not.
722 bool OurWindowHasFocus();
723
724 // This function is used to insert a string of HTML input optionally with some
725 // context information into the editable field. The HTML input either comes
726 // from a transferable object created as part of a drop/paste operation, or from
727 // the InsertHTML method. We may want the HTML input to be sanitized (for example,
728 // if it's coming from a transferable object), in which case aTrustedInput should
729 // be set to false, otherwise, the caller should set it to true, which means that
730 // the HTML will be inserted in the DOM verbatim.
731 //
732 // aClearStyle should be set to false if you want the paste to be affected by
733 // local style (e.g., for the insertHTML command).
734 nsresult DoInsertHTMLWithContext(const nsAString& aInputString,
735 const nsAString& aContextStr,
736 const nsAString& aInfoStr,
737 const nsAString& aFlavor,
738 nsIDOMDocument* aSourceDoc,
739 nsIDOMNode* aDestNode,
740 int32_t aDestOffset,
741 bool aDeleteSelection,
742 bool aTrustedInput,
743 bool aClearStyle = true);
744
745 nsresult ClearStyle(nsCOMPtr<nsIDOMNode>* aNode, int32_t* aOffset,
746 nsIAtom* aProperty, const nsAString* aAttribute);
747
748 // Data members
749 protected:
750
751 nsCOMArray<nsIContentFilter> mContentFilters;
752
753 nsRefPtr<TypeInState> mTypeInState;
754
755 bool mCRInParagraphCreatesParagraph;
756
757 bool mCSSAware;
758 nsAutoPtr<nsHTMLCSSUtils> mHTMLCSSUtils;
759
760 // Used by GetFirstSelectedCell and GetNextSelectedCell
761 int32_t mSelectedCellIndex;
762
763 nsString mLastStyleSheetURL;
764 nsString mLastOverrideStyleSheetURL;
765
766 // Maintain a list of associated style sheets and their urls.
767 nsTArray<nsString> mStyleSheetURLs;
768 nsTArray<nsRefPtr<nsCSSStyleSheet> > mStyleSheets;
769
770 // an array for holding default style settings
771 nsTArray<PropItem*> mDefaultStyles;
772
773 protected:
774
775 /* ANONYMOUS UTILS */
776 void RemoveListenerAndDeleteRef(const nsAString& aEvent,
777 nsIDOMEventListener* aListener,
778 bool aUseCapture,
779 nsIDOMElement* aElement,
780 nsIContent* aParentContent,
781 nsIPresShell* aShell);
782 void DeleteRefToAnonymousNode(nsIDOMElement* aElement,
783 nsIContent * aParentContent,
784 nsIPresShell* aShell);
785
786 nsresult ShowResizersInner(nsIDOMElement *aResizedElement);
787
788 // Returns the offset of an element's frame to its absolute containing block.
789 nsresult GetElementOrigin(nsIDOMElement * aElement, int32_t & aX, int32_t & aY);
790 nsresult GetPositionAndDimensions(nsIDOMElement * aElement,
791 int32_t & aX, int32_t & aY,
792 int32_t & aW, int32_t & aH,
793 int32_t & aBorderLeft,
794 int32_t & aBorderTop,
795 int32_t & aMarginLeft,
796 int32_t & aMarginTop);
797
798 /* PACKED BOOLEANS FOR RESIZING, ABSOLUTE POSITIONING AND */
799 /* INLINE TABLE EDITING */
800
801 // resizing
802 bool mIsObjectResizingEnabled;
803 bool mIsResizing;
804 bool mPreserveRatio;
805 bool mResizedObjectIsAnImage;
806
807 // absolute positioning
808 bool mIsAbsolutelyPositioningEnabled;
809 bool mResizedObjectIsAbsolutelyPositioned;
810
811 bool mGrabberClicked;
812 bool mIsMoving;
813
814 bool mSnapToGridEnabled;
815
816 // inline table editing
817 bool mIsInlineTableEditingEnabled;
818
819 /* RESIZING */
820
821 nsCOMPtr<nsIDOMElement> mTopLeftHandle;
822 nsCOMPtr<nsIDOMElement> mTopHandle;
823 nsCOMPtr<nsIDOMElement> mTopRightHandle;
824 nsCOMPtr<nsIDOMElement> mLeftHandle;
825 nsCOMPtr<nsIDOMElement> mRightHandle;
826 nsCOMPtr<nsIDOMElement> mBottomLeftHandle;
827 nsCOMPtr<nsIDOMElement> mBottomHandle;
828 nsCOMPtr<nsIDOMElement> mBottomRightHandle;
829
830 nsCOMPtr<nsIDOMElement> mActivatedHandle;
831
832 nsCOMPtr<nsIDOMElement> mResizingShadow;
833 nsCOMPtr<nsIDOMElement> mResizingInfo;
834
835 nsCOMPtr<nsIDOMElement> mResizedObject;
836
837 nsCOMPtr<nsIDOMEventListener> mMouseMotionListenerP;
838 nsCOMPtr<nsISelectionListener> mSelectionListenerP;
839 nsCOMPtr<nsIDOMEventListener> mResizeEventListenerP;
840
841 nsCOMArray<nsIHTMLObjectResizeListener> objectResizeEventListeners;
842
843 int32_t mOriginalX;
844 int32_t mOriginalY;
845
846 int32_t mResizedObjectX;
847 int32_t mResizedObjectY;
848 int32_t mResizedObjectWidth;
849 int32_t mResizedObjectHeight;
850
851 int32_t mResizedObjectMarginLeft;
852 int32_t mResizedObjectMarginTop;
853 int32_t mResizedObjectBorderLeft;
854 int32_t mResizedObjectBorderTop;
855
856 int32_t mXIncrementFactor;
857 int32_t mYIncrementFactor;
858 int32_t mWidthIncrementFactor;
859 int32_t mHeightIncrementFactor;
860
861 int8_t mInfoXIncrement;
862 int8_t mInfoYIncrement;
863
864 nsresult SetAllResizersPosition();
865
866 nsresult CreateResizer(nsIDOMElement ** aReturn, int16_t aLocation, nsIDOMNode * aParentNode);
867 void SetAnonymousElementPosition(int32_t aX, int32_t aY, nsIDOMElement *aResizer);
868
869 nsresult CreateShadow(nsIDOMElement ** aReturn, nsIDOMNode * aParentNode,
870 nsIDOMElement * aOriginalObject);
871 nsresult SetShadowPosition(nsIDOMElement * aShadow,
872 nsIDOMElement * aOriginalObject,
873 int32_t aOriginalObjectX,
874 int32_t aOriginalObjectY);
875
876 nsresult CreateResizingInfo(nsIDOMElement ** aReturn, nsIDOMNode * aParentNode);
877 nsresult SetResizingInfoPosition(int32_t aX, int32_t aY,
878 int32_t aW, int32_t aH);
879
880 int32_t GetNewResizingIncrement(int32_t aX, int32_t aY, int32_t aID);
881 nsresult StartResizing(nsIDOMElement * aHandle);
882 int32_t GetNewResizingX(int32_t aX, int32_t aY);
883 int32_t GetNewResizingY(int32_t aX, int32_t aY);
884 int32_t GetNewResizingWidth(int32_t aX, int32_t aY);
885 int32_t GetNewResizingHeight(int32_t aX, int32_t aY);
886 void HideShadowAndInfo();
887 void SetFinalSize(int32_t aX, int32_t aY);
888 void DeleteRefToAnonymousNode(nsIDOMNode * aNode);
889 void SetResizeIncrements(int32_t aX, int32_t aY, int32_t aW, int32_t aH, bool aPreserveRatio);
890 void HideAnonymousEditingUIs();
891
892 /* ABSOLUTE POSITIONING */
893
894 int32_t mPositionedObjectX;
895 int32_t mPositionedObjectY;
896 int32_t mPositionedObjectWidth;
897 int32_t mPositionedObjectHeight;
898
899 int32_t mPositionedObjectMarginLeft;
900 int32_t mPositionedObjectMarginTop;
901 int32_t mPositionedObjectBorderLeft;
902 int32_t mPositionedObjectBorderTop;
903
904 nsCOMPtr<nsIDOMElement> mAbsolutelyPositionedObject;
905 nsCOMPtr<nsIDOMElement> mGrabber;
906 nsCOMPtr<nsIDOMElement> mPositioningShadow;
907
908 int32_t mGridSize;
909
910 nsresult CreateGrabber(nsIDOMNode * aParentNode, nsIDOMElement ** aReturn);
911 nsresult StartMoving(nsIDOMElement * aHandle);
912 nsresult SetFinalPosition(int32_t aX, int32_t aY);
913 void AddPositioningOffset(int32_t & aX, int32_t & aY);
914 void SnapToGrid(int32_t & newX, int32_t & newY);
915 nsresult GrabberClicked();
916 nsresult EndMoving();
917 nsresult CheckPositionedElementBGandFG(nsIDOMElement * aElement,
918 nsAString & aReturn);
919
920 /* INLINE TABLE EDITING */
921
922 nsCOMPtr<nsIDOMElement> mInlineEditedCell;
923
924 nsCOMPtr<nsIDOMElement> mAddColumnBeforeButton;
925 nsCOMPtr<nsIDOMElement> mRemoveColumnButton;
926 nsCOMPtr<nsIDOMElement> mAddColumnAfterButton;
927
928 nsCOMPtr<nsIDOMElement> mAddRowBeforeButton;
929 nsCOMPtr<nsIDOMElement> mRemoveRowButton;
930 nsCOMPtr<nsIDOMElement> mAddRowAfterButton;
931
932 void AddMouseClickListener(nsIDOMElement * aElement);
933 void RemoveMouseClickListener(nsIDOMElement * aElement);
934
935 nsCOMPtr<nsILinkHandler> mLinkHandler;
936
937 public:
938
939 // friends
940 friend class nsHTMLEditRules;
941 friend class nsTextEditRules;
942 friend class nsWSRunObject;
943 friend class nsHTMLEditorEventListener;
944
945 private:
946 // Helpers
947 bool IsSimpleModifiableNode(nsIContent* aContent,
948 nsIAtom* aProperty,
949 const nsAString* aAttribute,
950 const nsAString* aValue);
951 nsresult SetInlinePropertyOnNodeImpl(nsIContent* aNode,
952 nsIAtom* aProperty,
953 const nsAString* aAttribute,
954 const nsAString* aValue);
955 typedef enum { eInserted, eAppended } InsertedOrAppended;
956 void DoContentInserted(nsIDocument* aDocument, nsIContent* aContainer,
957 nsIContent* aChild, int32_t aIndexInContainer,
958 InsertedOrAppended aInsertedOrAppended);
959 };
960 #endif //nsHTMLEditor_h__
961

mercurial