forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLEditor.h
2489 lines (2187 loc) · 99.2 KB
/
HTMLEditor.h
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_HTMLEditor_h
#define mozilla_HTMLEditor_h
#include "mozilla/Attributes.h"
#include "mozilla/ComposerCommandsUpdater.h"
#include "mozilla/CSSEditUtils.h"
#include "mozilla/ManualNAC.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/TextEditor.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/File.h"
#include "nsAttrName.h"
#include "nsCOMPtr.h"
#include "nsICSSLoaderObserver.h"
#include "nsIDocumentObserver.h"
#include "nsIDOMEventListener.h"
#include "nsIEditorMailSupport.h"
#include "nsIEditorStyleSheets.h"
#include "nsIHTMLAbsPosEditor.h"
#include "nsIHTMLEditor.h"
#include "nsIHTMLInlineTableEditor.h"
#include "nsIHTMLObjectResizer.h"
#include "nsITableEditor.h"
#include "nsPoint.h"
#include "nsStubMutationObserver.h"
#include "nsTArray.h"
class nsDocumentFragment;
class nsHTMLDocument;
class nsITransferable;
class nsIClipboard;
class nsILinkHandler;
class nsTableWrapperFrame;
class nsRange;
namespace mozilla {
class AutoSelectionSetterAfterTableEdit;
class AutoSetTemporaryAncestorLimiter;
class EmptyEditableFunctor;
class ResizerSelectionListener;
enum class EditSubAction : int32_t;
struct PropItem;
template <class T>
class OwningNonNull;
namespace dom {
class Blob;
class DocumentFragment;
class Event;
class MouseEvent;
} // namespace dom
namespace widget {
struct IMEState;
} // namespace widget
enum class ParagraphSeparator { div, p, br };
/**
* The HTML editor implementation.<br>
* Use to edit HTML document represented as a DOM tree.
*/
class HTMLEditor final : public TextEditor,
public nsIHTMLEditor,
public nsIHTMLObjectResizer,
public nsIHTMLAbsPosEditor,
public nsITableEditor,
public nsIHTMLInlineTableEditor,
public nsIEditorStyleSheets,
public nsStubMutationObserver,
public nsIEditorMailSupport {
public:
/****************************************************************************
* NOTE: DO NOT MAKE YOUR NEW METHODS PUBLIC IF they are called by other
* classes under libeditor except EditorEventListener and
* HTMLEditorEventListener because each public method which may fire
* eEditorInput event will need to instantiate new stack class for
* managing input type value of eEditorInput and cache some objects
* for smarter handling. In other words, when you add new root
* method to edit the DOM tree, you can make your new method public.
****************************************************************************/
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(HTMLEditor, TextEditor)
// nsStubMutationObserver overrides
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
// nsIHTMLEditor methods
NS_DECL_NSIHTMLEDITOR
// nsIHTMLObjectResizer methods (implemented in HTMLObjectResizer.cpp)
NS_DECL_NSIHTMLOBJECTRESIZER
// nsIHTMLAbsPosEditor methods (implemented in HTMLAbsPositionEditor.cpp)
NS_DECL_NSIHTMLABSPOSEDITOR
// nsIHTMLInlineTableEditor methods (implemented in HTMLInlineTableEditor.cpp)
NS_DECL_NSIHTMLINLINETABLEEDITOR
// nsIEditorStyleSheets methods
NS_DECL_NSIEDITORSTYLESHEETS
// nsIEditorMailSupport methods
NS_DECL_NSIEDITORMAILSUPPORT
// nsITableEditor methods
NS_DECL_NSITABLEEDITOR
// nsISelectionListener overrides
NS_DECL_NSISELECTIONLISTENER
HTMLEditor();
nsHTMLDocument* GetHTMLDocument() const;
virtual void PreDestroy(bool aDestroyingFrames) override;
bool GetReturnInParagraphCreatesNewParagraph();
// TextEditor overrides
virtual nsresult Init(nsIDocument& aDoc, Element* aRoot,
nsISelectionController* aSelCon, uint32_t aFlags,
const nsAString& aValue) override;
NS_IMETHOD BeginningOfDocument() override;
NS_IMETHOD SetFlags(uint32_t aFlags) override;
NS_IMETHOD CanPaste(int32_t aSelectionType, bool* aCanPaste) override;
NS_IMETHOD PasteTransferable(nsITransferable* aTransferable) override;
NS_IMETHOD DeleteNode(nsINode* aNode) override;
NS_IMETHOD InsertLineBreak() override;
virtual nsresult HandleKeyPressEvent(
WidgetKeyboardEvent* aKeyboardEvent) override;
virtual nsIContent* GetFocusedContent() override;
virtual already_AddRefed<nsIContent> GetFocusedContentForIME() override;
virtual bool IsActiveInDOMWindow() override;
virtual dom::EventTarget* GetDOMEventTarget() override;
virtual Element* FindSelectionRoot(nsINode* aNode) const override;
virtual bool IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) override;
virtual nsresult GetPreferredIMEState(widget::IMEState* aState) override;
/**
* PasteAsQuotationAsAction() pastes content in clipboard with newly created
* blockquote element. If the editor is in plaintext mode, will paste the
* content with appending ">" to start of each line.
*
* @param aClipboardType nsIClipboard::kGlobalClipboard or
* nsIClipboard::kSelectionClipboard.
* @param aDispatchPasteEvent true if this should dispatch ePaste event
* before pasting. Otherwise, false.
*/
virtual nsresult PasteAsQuotationAsAction(int32_t aClipboardType,
bool aDispatchPasteEvent) override;
/**
* Can we paste |aTransferable| or, if |aTransferable| is null, will a call
* to pasteTransferable later possibly succeed if given an instance of
* nsITransferable then? True if the doc is modifiable, and, if
* |aTransfeable| is non-null, we have pasteable data in |aTransfeable|.
*/
virtual bool CanPasteTransferable(nsITransferable* aTransferable) override;
/**
* InsertLineBreakAsAction() is called when user inputs a line break with
* Shift + Enter or something.
*/
virtual nsresult InsertLineBreakAsAction() override;
/**
* InsertParagraphSeparatorAsAction() is called when user tries to separate
* current paragraph with Enter key press in HTMLEditor or something.
*/
nsresult InsertParagraphSeparatorAsAction();
/**
* CreateElementWithDefaults() creates new element whose name is
* aTagName with some default attributes are set. Note that this is a
* public utility method. I.e., just creates element, not insert it
* into the DOM tree.
* NOTE: This is available for internal use too since this does not change
* the DOM tree nor undo transactions, and does not refer Selection,
* HTMLEditRules, etc.
*
* @param aTagName The new element's tag name. If the name is
* one of "href", "anchor" or "namedanchor",
* this creates an <a> element.
* @return Newly created element.
*/
already_AddRefed<Element> CreateElementWithDefaults(const nsAtom& aTagName);
/**
* Indent or outdent content around Selection.
*/
nsresult IndentAsAction();
nsresult OutdentAsAction();
/**
* event callback when a mouse button is pressed
* @param aX [IN] horizontal position of the pointer
* @param aY [IN] vertical position of the pointer
* @param aTarget [IN] the element triggering the event
* @param aMouseEvent [IN] the event
*/
nsresult OnMouseDown(int32_t aX, int32_t aY, Element* aTarget,
dom::Event* aMouseEvent);
/**
* event callback when a mouse button is released
* @param aX [IN] horizontal position of the pointer
* @param aY [IN] vertical position of the pointer
* @param aTarget [IN] the element triggering the event
*/
nsresult OnMouseUp(int32_t aX, int32_t aY, Element* aTarget);
/**
* event callback when the mouse pointer is moved
* @param aMouseEvent [IN] the event
*/
MOZ_CAN_RUN_SCRIPT nsresult OnMouseMove(dom::MouseEvent* aMouseEvent);
/**
* IsCSSEnabled() returns true if this editor treats styles with style
* attribute of HTML elements. Otherwise, if this editor treats all styles
* with "font style elements" like <b>, <i>, etc, and <blockquote> to indent,
* align attribute to align contents, returns false.
*/
bool IsCSSEnabled() const {
// TODO: removal of mCSSAware and use only the presence of mCSSEditUtils
return mCSSAware && mCSSEditUtils && mCSSEditUtils->IsCSSPrefChecked();
}
/**
* Enable/disable object resizers for <img> elements, <table> elements,
* absolute positioned elements (required absolute position editor enabled).
*/
void EnableObjectResizer(bool aEnable) {
if (mIsObjectResizingEnabled == aEnable) {
return;
}
AutoEditActionDataSetter editActionData(
*this, EditAction::eEnableOrDisableResizer);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return;
}
mIsObjectResizingEnabled = aEnable;
RefereshEditingUI();
}
bool IsObjectResizerEnabled() const { return mIsObjectResizingEnabled; }
/**
* Enable/disable inline table editor, e.g., adding new row or column,
* removing existing row or column.
*/
void EnableInlineTableEditor(bool aEnable) {
if (mIsInlineTableEditingEnabled == aEnable) {
return;
}
AutoEditActionDataSetter editActionData(
*this, EditAction::eEnableOrDisableInlineTableEditingUI);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return;
}
mIsInlineTableEditingEnabled = aEnable;
RefereshEditingUI();
}
bool IsInlineTableEditorEnabled() const {
return mIsInlineTableEditingEnabled;
}
/**
* Enable/disable absolute position editor, resizing absolute positioned
* elements (required object resizers enabled) or positioning them with
* dragging grabber.
*/
void EnableAbsolutePositionEditor(bool aEnable) {
if (mIsAbsolutelyPositioningEnabled == aEnable) {
return;
}
AutoEditActionDataSetter editActionData(
*this, EditAction::eEnableOrDisableAbsolutePositionEditor);
if (NS_WARN_IF(!editActionData.CanHandle())) {
return;
}
mIsAbsolutelyPositioningEnabled = aEnable;
RefereshEditingUI();
}
bool IsAbsolutePositionEditorEnabled() const {
return mIsAbsolutelyPositioningEnabled;
}
// non-virtual methods of interface methods
/**
* returns the deepest absolutely positioned container of the selection
* if it exists or null.
*/
already_AddRefed<Element> GetAbsolutelyPositionedSelectionContainer();
Element* GetPositionedElement() const { return mAbsolutelyPositionedObject; }
/**
* extracts the selection from the normal flow of the document and
* positions it.
* @param aEnabled [IN] true to absolutely position the selection,
* false to put it back in the normal flow
*/
nsresult SetSelectionToAbsoluteOrStatic(bool aEnabled);
/**
* returns the absolute z-index of a positioned element. Never returns 'auto'
* @return the z-index of the element
* @param aElement [IN] the element.
*/
int32_t GetZIndex(Element& aElement);
/**
* adds aChange to the z-index of the currently positioned element.
* @param aChange [IN] relative change to apply to current z-index
*/
nsresult AddZIndex(int32_t aChange);
/**
* SetInlinePropertyAsAction() sets a property which changes inline style of
* text. E.g., bold, italic, super and sub.
* This automatically removes exclusive style, however, treats all changes
* as a transaction.
*/
nsresult SetInlinePropertyAsAction(nsAtom& aProperty, nsAtom* aAttribute,
const nsAString& aValue);
nsresult GetInlineProperty(nsAtom* aProperty, nsAtom* aAttribute,
const nsAString& aValue, bool* aFirst, bool* aAny,
bool* aAll);
nsresult GetInlinePropertyWithAttrValue(nsAtom* aProperty, nsAtom* aAttr,
const nsAString& aValue, bool* aFirst,
bool* aAny, bool* aAll,
nsAString& outValue);
/**
* RemoveInlinePropertyAsAction() removes a property which changes inline
* style of text. E.g., bold, italic, super and sub.
*
* @param aProperty Tag name whcih represents the inline style you want to
* remove. E.g., nsGkAtoms::strong, nsGkAtoms::b, etc.
* If nsGkAtoms::href, <a> element which has href
* attribute will be removed.
* If nsGkAtoms::name, <a> element which has non-empty
* name attribute will be removed.
* @param aAttribute If aProperty is nsGkAtoms::font, aAttribute should be
* nsGkAtoms::fase, nsGkAtoms::size, nsGkAtoms::color or
* nsGkAtoms::bgcolor. Otherwise, set nullptr.
* Must not use nsGkAtoms::_empty here.
*/
nsresult RemoveInlinePropertyAsAction(nsAtom& aProperty, nsAtom* aAttribute);
/**
* GetFontColorState() returns foreground color information in first
* range of Selection.
* If first range of Selection is collapsed and there is a cache of style for
* new text, aIsMixed is set to false and aColor is set to the cached color.
* If first range of Selection is collapsed and there is no cached color,
* this returns the color of the node, aIsMixed is set to false and aColor is
* set to the color.
* If first range of Selection is not collapsed, this collects colors of
* each node in the range. If there are two or more colors, aIsMixed is set
* to true and aColor is truncated. If only one color is set to all of the
* range, aIsMixed is set to false and aColor is set to the color.
* If there is no Selection ranges, aIsMixed is set to false and aColor is
* truncated.
*
* @param aIsMixed Must not be nullptr. This is set to true
* if there is two or more colors in first
* range of Selection.
* @param aColor Returns the color if only one color is set to
* all of first range in Selection. Otherwise,
* returns empty string.
* @return Returns error only when illegal cases, e.g.,
* Selection instance has gone, first range
* Selection is broken.
*/
nsresult GetFontColorState(bool* aIsMixed, nsAString& aColor);
/**
* SetComposerCommandsUpdater() sets or unsets mComposerCommandsUpdater.
* This will crash in debug build if the editor already has an instance
* but called with another instance.
*/
void SetComposerCommandsUpdater(
ComposerCommandsUpdater* aComposerCommandsUpdater) {
MOZ_ASSERT(!aComposerCommandsUpdater || !mComposerCommandsUpdater ||
aComposerCommandsUpdater == mComposerCommandsUpdater);
mComposerCommandsUpdater = aComposerCommandsUpdater;
}
ParagraphSeparator GetDefaultParagraphSeparator() const {
return mDefaultParagraphSeparator;
}
void SetDefaultParagraphSeparator(ParagraphSeparator aSep) {
mDefaultParagraphSeparator = aSep;
}
/**
* Modifies the table containing the selection according to the
* activation of an inline table editing UI element
* @param aUIAnonymousElement [IN] the inline table editing UI element
*/
nsresult DoInlineTableEditingAction(const Element& aUIAnonymousElement);
/**
* GetElementOrParentByTagName() looks for an element node whose name matches
* aTagName from aNode or anchor node of Selection to <body> element.
*
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.
* If nsGkAtoms::anchor, the result may be <a> which
* has "name" attribute with non-empty value.
* @param aNode If non-nullptr, this starts to look for the result
* from it. Otherwise, i.e., nullptr, starts from
* anchor node of Selection.
* @return If an element which matches aTagName, returns
* an Element. Otherwise, nullptr.
*/
Element* GetElementOrParentByTagName(const nsAtom& aTagName,
nsINode* aNode) const;
/**
* Get an active editor's editing host in DOM window. If this editor isn't
* active in the DOM window, this returns NULL.
*/
Element* GetActiveEditingHost() const;
/** Insert a string as quoted text
* (whose representation is dependant on the editor type),
* replacing the selected text (if any).
*
* @param aQuotedText The actual text to be quoted
* @parem aNodeInserted Return the node which was inserted.
*/
nsresult InsertAsQuotation(const nsAString& aQuotedText,
nsINode** aNodeInserted);
/**
* Inserts a plaintext string at the current location,
* with special processing for lines beginning with ">",
* which will be treated as mail quotes and inserted
* as plaintext quoted blocks.
* If the selection is not collapsed, the selection is deleted
* and the insertion takes place at the resulting collapsed selection.
*
* @param aString the string to be inserted
*/
nsresult InsertTextWithQuotations(const nsAString& aStringToInsert);
protected: // May be called by friends.
/****************************************************************************
* Some classes like TextEditRules, HTMLEditRules, WSRunObject which are
* part of handling edit actions are allowed to call the following protected
* methods. However, those methods won't prepare caches of some objects
* which are necessary for them. So, if you want some following methods
* to do that for you, you need to create a wrapper method in public scope
* and call it.
****************************************************************************/
/**
* DeleteSelectionWithTransaction() removes selected content or content
* around caret with transactions.
*
* @param aDirection How much range should be removed.
* @param aStripWrappers Whether the parent blocks should be removed
* when they become empty.
*/
virtual nsresult DeleteSelectionWithTransaction(
EDirection aAction, EStripWrappers aStripWrappers) override;
/**
* DeleteNodeWithTransaction() removes aNode from the DOM tree if it's
* modifiable. Note that this is not an override of same method of
* EditorBase.
*
* @param aNode The node to be removed from the DOM tree.
*/
nsresult DeleteNodeWithTransaction(nsINode& aNode);
/**
* DeleteTextWithTransaction() removes text in the range from aCharData if
* it's modifiable. Note that this not an override of same method of
* EditorBase.
*
* @param aCharData The data node which should be modified.
* @param aOffset Start offset of removing text in aCharData.
* @param aLength Length of removing text.
*/
nsresult DeleteTextWithTransaction(dom::CharacterData& aTextNode,
uint32_t aOffset, uint32_t aLength);
/**
* InsertTextWithTransaction() inserts aStringToInsert at aPointToInsert.
*/
virtual nsresult InsertTextWithTransaction(
nsIDocument& aDocument, const nsAString& aStringToInsert,
const EditorRawDOMPoint& aPointToInsert,
EditorRawDOMPoint* aPointAfterInsertedString = nullptr) override;
/**
* CopyLastEditableChildStyles() clones inline container elements into
* aPreviousBlock to aNewBlock to keep using same style in it.
*
* @param aPreviousBlock The previous block element. All inline
* elements which are last sibling of each level
* are cloned to aNewBlock.
* @param aNewBlock New block container element.
* @param aNewBrElement If this method creates a new <br> element for
* placeholder, this is set to the new <br>
* element.
*/
nsresult CopyLastEditableChildStylesWithTransaction(
Element& aPreviousBlock, Element& aNewBlock,
RefPtr<Element>* aNewBrElement);
/**
* RemoveBlockContainerWithTransaction() removes aElement from the DOM tree
* but moves its all children to its parent node and if its parent needs <br>
* element to have at least one line-height, this inserts <br> element
* automatically.
*
* @param aElement Block element to be removed.
*/
nsresult RemoveBlockContainerWithTransaction(Element& aElement);
virtual Element* GetEditorRoot() const override;
using EditorBase::IsEditable;
virtual nsresult RemoveAttributeOrEquivalent(
Element* aElement, nsAtom* aAttribute,
bool aSuppressTransaction) override;
virtual nsresult SetAttributeOrEquivalent(Element* aElement,
nsAtom* aAttribute,
const nsAString& aValue,
bool aSuppressTransaction) override;
using EditorBase::RemoveAttributeOrEquivalent;
using EditorBase::SetAttributeOrEquivalent;
/**
* GetBlockNodeParent() returns parent or nearest ancestor of aNode if
* there is a block parent. If aAncestorLimiter is not nullptr,
* this stops looking for the result.
*/
static Element* GetBlockNodeParent(nsINode* aNode,
nsINode* aAncestorLimiter = nullptr);
/**
* GetBlock() returns aNode itself, or parent or nearest ancestor of aNode
* if there is a block parent. If aAncestorLimiter is not nullptr,
* this stops looking for the result.
*/
static Element* GetBlock(nsINode& aNode, nsINode* aAncestorLimiter = nullptr);
/**
* Returns container element of ranges in Selection. If Selection is
* collapsed, returns focus container node (or its parent element).
* If Selection selects only one element node, returns the element node.
* If Selection is only one range, returns common ancestor of the range.
* XXX If there are two or more Selection ranges, this returns parent node
* of start container of a range which starts with different node from
* start container of the first range.
*/
Element* GetSelectionContainerElement() const;
/**
* GetFirstSelectedTableCellElement() returns a <td> or <th> element if
* first range of Selection (i.e., result of Selection::GetRangeAt(0))
* selects a <td> element or <th> element. Even if Selection is in
* a cell element, this returns nullptr. And even if 2nd or later
* range of Selection selects a cell element, also returns nullptr.
* Note that when this looks for a cell element, this resets the internal
* index of ranges of Selection. When you call
* GetNextSelectedTableCellElement() after a call of this, it'll return 2nd
* selected cell if there is.
*
* @param aRv Returns error if there is no selection or
* first range of Selection is unexpected.
* @return A <td> or <th> element is selected by first
* range of Selection. Note that the range must
* be: startContaienr and endContainer are same
* <tr> element, startOffset + 1 equals endOffset.
*/
already_AddRefed<Element> GetFirstSelectedTableCellElement(
ErrorResult& aRv) const;
/**
* GetNextSelectedTableCellElement() is a stateful method to retrieve
* selected table cell elements which are selected by 2nd or later ranges
* of Selection. When you call GetFirstSelectedTableCellElement(), it
* resets internal counter of this method. Then, following calls of
* GetNextSelectedTableCellElement() scans the remaining ranges of Selection.
* If a range selects a <td> or <th> element, returns the cell element.
* If a range selects an element but neither <td> nor <th> element, this
* ignores the range. If a range is in a text node, returns null without
* throwing exception, but stops scanning the remaining ranges even you
* call this again.
* Note that this may cross <table> boundaries since this method just
* scans all ranges of Selection. Therefore, returning cells which
* belong to different <table> elements.
*
* @param aRv Returns error if Selection doesn't have
* range properly.
* @return A <td> or <th> element if one of remaining
* ranges selects a <td> or <th> element unless
* this does not meet a range in a text node.
*/
already_AddRefed<Element> GetNextSelectedTableCellElement(
ErrorResult& aRv) const;
/**
* DeleteTableCellContentsWithTransaction() removes any contents in cell
* elements. If two or more cell elements are selected, this removes
* all selected cells' contents. Otherwise, this removes contents of
* a cell which contains first selection range. This does not return
* error even if selection is not in cell element, just does nothing.
*/
nsresult DeleteTableCellContentsWithTransaction();
void IsNextCharInNodeWhitespace(nsIContent* aContent, int32_t aOffset,
bool* outIsSpace, bool* outIsNBSP,
nsIContent** outNode = nullptr,
int32_t* outOffset = 0);
void IsPrevCharInNodeWhitespace(nsIContent* aContent, int32_t aOffset,
bool* outIsSpace, bool* outIsNBSP,
nsIContent** outNode = nullptr,
int32_t* outOffset = 0);
/**
* @param aElement Must not be null.
*/
static bool NodeIsBlockStatic(const nsINode* aElement);
/**
* extracts an element from the normal flow of the document and
* positions it, and puts it back in the normal flow.
* @param aElement [IN] the element
* @param aEnabled [IN] true to absolutely position the element,
* false to put it back in the normal flow
*/
nsresult SetPositionToAbsoluteOrStatic(Element& aElement, bool aEnabled);
/**
* adds aChange to the z-index of an arbitrary element.
* @param aElement [IN] the element
* @param aChange [IN] relative change to apply to current z-index of
* the element
* @param aReturn [OUT] the new z-index of the element
*/
nsresult RelativeChangeElementZIndex(Element& aElement, int32_t aChange,
int32_t* aReturn);
virtual bool IsBlockNode(nsINode* aNode) override;
using EditorBase::IsBlockNode;
/**
* returns true if aParentTag can contain a child of type aChildTag.
*/
virtual bool TagCanContainTag(nsAtom& aParentTag,
nsAtom& aChildTag) const override;
/**
* Returns true if aNode is a container.
*/
virtual bool IsContainer(nsINode* aNode) override;
/**
* Join together any adjacent editable text nodes in the range.
*/
nsresult CollapseAdjacentTextNodes(nsRange* aRange);
/**
* IsInVisibleTextFrames() returns true if all text in aText is in visible
* text frames. Callers have to guarantee that there is no pending reflow.
*/
bool IsInVisibleTextFrames(dom::Text& aText);
/**
* IsVisibleTextNode() returns true if aText has visible text. If it has
* only whitespaces and they are collapsed, returns false.
*/
bool IsVisibleTextNode(Text& aText);
/**
* aNode must be a non-null text node.
* outIsEmptyNode must be non-null.
*/
nsresult IsEmptyNode(nsINode* aNode, bool* outIsEmptyBlock,
bool aMozBRDoesntCount = false,
bool aListOrCellNotEmpty = false,
bool aSafeToAskFrames = false);
nsresult IsEmptyNodeImpl(nsINode* aNode, bool* outIsEmptyBlock,
bool aMozBRDoesntCount, bool aListOrCellNotEmpty,
bool aSafeToAskFrames, bool* aSeenBR);
static bool HasAttributes(Element* aElement) {
MOZ_ASSERT(aElement);
uint32_t attrCount = aElement->GetAttrCount();
return attrCount > 1 ||
(1 == attrCount &&
!aElement->GetAttrNameAt(0)->Equals(nsGkAtoms::mozdirty));
}
/**
* Content-based query returns true if <aProperty aAttribute=aValue> effects
* aNode. If <aProperty aAttribute=aValue> contains aNode, but
* <aProperty aAttribute=SomeOtherValue> also contains aNode and the second is
* more deeply nested than the first, then the first does not effect aNode.
*
* @param aNode The target of the query
* @param aProperty The property that we are querying for
* @param aAttribute The attribute of aProperty, example: color in
* <FONT color="blue"> May be null.
* @param aValue The value of aAttribute, example: blue in
* <FONT color="blue"> May be null. Ignored if aAttribute
* is null.
* @param outValue [OUT] the value of the attribute, if aIsSet is true
* @return true if <aProperty aAttribute=aValue> effects
* aNode.
*
* The nsIContent variant returns aIsSet instead of using an out parameter.
*/
bool IsTextPropertySetByContent(nsINode* aNode, nsAtom* aProperty,
nsAtom* aAttribute, const nsAString* aValue,
nsAString* outValue = nullptr);
static dom::Element* GetLinkElement(nsINode* aNode);
/**
* Small utility routine to test if a break node is visible to user.
*/
bool IsVisibleBRElement(nsINode* aNode);
/**
* Helper routines for font size changing.
*/
enum class FontSize { incr, decr };
nsresult RelativeFontChangeOnTextNode(FontSize aDir, Text& aTextNode,
int32_t aStartOffset,
int32_t aEndOffset);
nsresult SetInlinePropertyOnNode(nsIContent& aNode, nsAtom& aProperty,
nsAtom* aAttribute, const nsAString& aValue);
nsresult SplitStyleAbovePoint(nsCOMPtr<nsINode>* aNode, int32_t* aOffset,
nsAtom* aProperty, nsAtom* aAttribute,
nsIContent** aOutLeftNode = nullptr,
nsIContent** aOutRightNode = nullptr);
nsIContent* GetPriorHTMLSibling(nsINode* aNode);
nsIContent* GetNextHTMLSibling(nsINode* aNode);
/**
* GetPreviousHTMLElementOrText*() methods are similar to
* EditorBase::GetPreviousElementOrText*() but this won't return nodes
* outside active editing host.
*/
nsIContent* GetPreviousHTMLElementOrText(nsINode& aNode) {
return GetPreviousHTMLElementOrTextInternal(aNode, false);
}
nsIContent* GetPreviousHTMLElementOrTextInBlock(nsINode& aNode) {
return GetPreviousHTMLElementOrTextInternal(aNode, true);
}
template <typename PT, typename CT>
nsIContent* GetPreviousHTMLElementOrText(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetPreviousHTMLElementOrTextInternal(aPoint, false);
}
template <typename PT, typename CT>
nsIContent* GetPreviousHTMLElementOrTextInBlock(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetPreviousHTMLElementOrTextInternal(aPoint, true);
}
/**
* GetPreviousHTMLElementOrTextInternal() methods are common implementation
* of above methods. Please don't use this method directly.
*/
nsIContent* GetPreviousHTMLElementOrTextInternal(nsINode& aNode,
bool aNoBlockCrossing);
template <typename PT, typename CT>
nsIContent* GetPreviousHTMLElementOrTextInternal(
const EditorDOMPointBase<PT, CT>& aPoint, bool aNoBlockCrossing);
/**
* GetPreviousEditableHTMLNode*() methods are similar to
* EditorBase::GetPreviousEditableNode() but this won't return nodes outside
* active editing host.
*/
nsIContent* GetPreviousEditableHTMLNode(nsINode& aNode) {
return GetPreviousEditableHTMLNodeInternal(aNode, false);
}
nsIContent* GetPreviousEditableHTMLNodeInBlock(nsINode& aNode) {
return GetPreviousEditableHTMLNodeInternal(aNode, true);
}
template <typename PT, typename CT>
nsIContent* GetPreviousEditableHTMLNode(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetPreviousEditableHTMLNodeInternal(aPoint, false);
}
template <typename PT, typename CT>
nsIContent* GetPreviousEditableHTMLNodeInBlock(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetPreviousEditableHTMLNodeInternal(aPoint, true);
}
/**
* GetPreviousEditableHTMLNodeInternal() methods are common implementation
* of above methods. Please don't use this method directly.
*/
nsIContent* GetPreviousEditableHTMLNodeInternal(nsINode& aNode,
bool aNoBlockCrossing);
template <typename PT, typename CT>
nsIContent* GetPreviousEditableHTMLNodeInternal(
const EditorDOMPointBase<PT, CT>& aPoint, bool aNoBlockCrossing);
/**
* GetNextHTMLElementOrText*() methods are similar to
* EditorBase::GetNextElementOrText*() but this won't return nodes outside
* active editing host.
*
* Note that same as EditorBase::GetTextEditableNode(), methods which take
* |const EditorRawDOMPoint&| start to search from the node pointed by it.
* On the other hand, methods which take |nsINode&| start to search from
* next node of aNode.
*/
nsIContent* GetNextHTMLElementOrText(nsINode& aNode) {
return GetNextHTMLElementOrTextInternal(aNode, false);
}
nsIContent* GetNextHTMLElementOrTextInBlock(nsINode& aNode) {
return GetNextHTMLElementOrTextInternal(aNode, true);
}
template <typename PT, typename CT>
nsIContent* GetNextHTMLElementOrText(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetNextHTMLElementOrTextInternal(aPoint, false);
}
template <typename PT, typename CT>
nsIContent* GetNextHTMLElementOrTextInBlock(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetNextHTMLElementOrTextInternal(aPoint, true);
}
/**
* GetNextHTMLNodeInternal() methods are common implementation
* of above methods. Please don't use this method directly.
*/
nsIContent* GetNextHTMLElementOrTextInternal(nsINode& aNode,
bool aNoBlockCrossing);
template <typename PT, typename CT>
nsIContent* GetNextHTMLElementOrTextInternal(
const EditorDOMPointBase<PT, CT>& aPoint, bool aNoBlockCrossing);
/**
* GetNextEditableHTMLNode*() methods are similar to
* EditorBase::GetNextEditableNode() but this won't return nodes outside
* active editing host.
*
* Note that same as EditorBase::GetTextEditableNode(), methods which take
* |const EditorRawDOMPoint&| start to search from the node pointed by it.
* On the other hand, methods which take |nsINode&| start to search from
* next node of aNode.
*/
nsIContent* GetNextEditableHTMLNode(nsINode& aNode) {
return GetNextEditableHTMLNodeInternal(aNode, false);
}
nsIContent* GetNextEditableHTMLNodeInBlock(nsINode& aNode) {
return GetNextEditableHTMLNodeInternal(aNode, true);
}
template <typename PT, typename CT>
nsIContent* GetNextEditableHTMLNode(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetNextEditableHTMLNodeInternal(aPoint, false);
}
template <typename PT, typename CT>
nsIContent* GetNextEditableHTMLNodeInBlock(
const EditorDOMPointBase<PT, CT>& aPoint) {
return GetNextEditableHTMLNodeInternal(aPoint, true);
}
/**
* GetNextEditableHTMLNodeInternal() methods are common implementation
* of above methods. Please don't use this method directly.
*/
nsIContent* GetNextEditableHTMLNodeInternal(nsINode& aNode,
bool aNoBlockCrossing);
template <typename PT, typename CT>
nsIContent* GetNextEditableHTMLNodeInternal(
const EditorDOMPointBase<PT, CT>& aPoint, bool aNoBlockCrossing);
bool IsFirstEditableChild(nsINode* aNode);
bool IsLastEditableChild(nsINode* aNode);
nsIContent* GetFirstEditableChild(nsINode& aNode);
nsIContent* GetLastEditableChild(nsINode& aNode);
nsIContent* GetFirstEditableLeaf(nsINode& aNode);
nsIContent* GetLastEditableLeaf(nsINode& aNode);
nsresult GetInlinePropertyBase(nsAtom& aProperty, nsAtom* aAttribute,
const nsAString* aValue, bool* aFirst,
bool* aAny, bool* aAll, nsAString* outValue);
nsresult ClearStyle(nsCOMPtr<nsINode>* aNode, int32_t* aOffset,
nsAtom* aProperty, nsAtom* aAttribute);
nsresult SetPositionToAbsolute(Element& aElement);
nsresult SetPositionToStatic(Element& aElement);
/**
* OnModifyDocument() is called when the editor is changed. This should
* be called only by HTMLEditRules::DocumentModifiedWorker() to call
* HTMLEditRules::OnModifyDocument() with AutoEditActionDataSetter
* instance.
*/
MOZ_CAN_RUN_SCRIPT void OnModifyDocument();
protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;
virtual void OnEndHandlingTopLevelEditSubAction() override;
protected: // Shouldn't be used by friend classes
virtual ~HTMLEditor();
/**
* InsertParagraphSeparatorAsSubAction() inserts a line break if it's
* HTMLEditor and it's possible.
*/
nsresult InsertParagraphSeparatorAsSubAction();
virtual nsresult SelectAllInternal() override;
/**
* SelectContentInternal() sets Selection to aContentToSelect to
* aContentToSelect + 1 in parent of aContentToSelect.
*
* @param aContentToSelect The content which should be selected.
*/
nsresult SelectContentInternal(nsIContent& aContentToSelect);
/**
* CollapseSelectionAfter() collapses Selection after aElement.
* If aElement is an orphan node or not in editing host, returns error.
*/
nsresult CollapseSelectionAfter(Element& aElement);
/**
* GetElementOrParentByTagNameAtSelection() looks for an element node whose
* name matches aTagName from anchor node of Selection to <body> element.
*
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.
* If nsGkAtoms::anchor, the result may be <a> which
* has "name" attribute with non-empty value.
* @return If an element which matches aTagName, returns
* an Element. Otherwise, nullptr.
*/
Element* GetElementOrParentByTagNameAtSelection(const nsAtom& aTagName) const;
/**
* GetElementOrParentByTagNameInternal() looks for an element node whose
* name matches aTagName from aNode to <body> element.
*
* @param aTagName The tag name which you want to look for.
* Must not be nsGkAtoms::_empty.
* If nsGkAtoms::list, the result may be <ul>, <ol> or
* <dl> element.
* If nsGkAtoms::td, the result may be <td> or <th>.
* If nsGkAtoms::href, the result may be <a> element
* which has "href" attribute with non-empty value.