Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / editor / nsIEditor.idl
blob9193dc1b8b6e0ce510349dd192e48b9b2a8ea560
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsISupports.idl"
8 #include "domstubs.idl"
10 %{C++
11 #include "mozilla/Debug.h"
14 interface nsISelectionController;
15 interface nsIDocumentStateListener;
16 interface nsIEditActionListener;
17 interface nsIInlineSpellChecker;
18 interface nsITransferable;
20 webidl Document;
21 webidl Element;
22 webidl Node;
23 webidl Selection;
25 %{C++
26 namespace mozilla {
27 class EditorBase;
28 class HTMLEditor;
29 class TextEditor;
30 } // namespace mozilla
33 [scriptable, builtinclass, uuid(094be624-f0bf-400f-89e2-6a84baab9474)]
34 interface nsIEditor : nsISupports
36 %{C++
37 typedef short EDirection;
38 typedef short EStripWrappers;
40 const short eNone = 0;
41 const short eNext = 1;
42 const short ePrevious = 2;
43 const short eNextWord = 3;
44 const short ePreviousWord = 4;
45 const short eToBeginningOfLine = 5;
46 const short eToEndOfLine = 6;
48 %{C++
49 static bool EDirectionIsValid(EDirection aDirectionAndAmount) {
50 return aDirectionAndAmount == nsIEditor::eNone ||
51 aDirectionAndAmount == nsIEditor::eNext ||
52 aDirectionAndAmount == nsIEditor::ePrevious ||
53 aDirectionAndAmount == nsIEditor::eNextWord ||
54 aDirectionAndAmount == nsIEditor::ePreviousWord ||
55 aDirectionAndAmount == nsIEditor::eToBeginningOfLine ||
56 aDirectionAndAmount == nsIEditor::eToEndOfLine;
58 static bool EDirectionIsValidExceptNone(EDirection aDirectionAndAmount) {
59 return aDirectionAndAmount != nsIEditor::eNone &&
60 EDirectionIsValid(aDirectionAndAmount);
63 /**
64 * Return true if nsIEditor::EDirection value means the direction of pressing
65 * `Backspace` key.
67 [[nodiscard]] static bool DirectionIsBackspace(
68 EDirection aDirectionAndAmount) {
69 MOZ_ASSERT(EDirectionIsValid(aDirectionAndAmount));
70 return aDirectionAndAmount == nsIEditor::ePrevious ||
71 aDirectionAndAmount == nsIEditor::ePreviousWord ||
72 aDirectionAndAmount == nsIEditor::eToBeginningOfLine;
75 /**
76 * Return true if nsIEditor::EDirection value means the direction of pressing
77 * `Delete` key (forwardDelete).
79 [[nodiscard]] static bool DirectionIsDelete(
80 EDirection aDirectionAndAmount) {
81 MOZ_ASSERT(EDirectionIsValid(aDirectionAndAmount));
82 return aDirectionAndAmount == nsIEditor::eNext ||
83 aDirectionAndAmount == nsIEditor::eNextWord ||
84 aDirectionAndAmount == nsIEditor::eToEndOfLine;
88 const short eStrip = 0;
89 const short eNoStrip = 1;
91 // If you want an HTML editor to behave as a plaintext editor, specify this
92 // flag. This is currently used only with plaintext email composer.
93 const long eEditorPlaintextMask = 0x0001;
94 // We don't support single line editor mode with HTML editors. Therefore,
95 // don't specify this for HTML editor.
96 const long eEditorSingleLineMask = 0x0002;
97 // We don't support password editor mode with HTML editors. Therefore,
98 // don't specify this for HTML editor.
99 const long eEditorPasswordMask = 0x0004;
100 // When the editor should be in readonly mode (currently, same as "disabled"),
101 // you can specify this flag with any editor instances.
102 // NOTE: Setting this flag does not change the style of editor. This just
103 // changes the internal editor's readonly state.
104 // NOTE: The readonly mode does NOT block XPCOM APIs which modify the editor
105 // content. This just blocks edit operations from user input and editing
106 // commands (both HTML Document.execCommand and the XUL commands).
107 // FIXME: XPCOM methods of TextEditor may be blocked by this flag. If you
108 // find it, file a bug.
109 const long eEditorReadonlyMask = 0x0008;
110 // If you want an HTML editor to work as an email composer, specify this flag.
111 // And you can specify this to text editor too for making spellchecker for
112 // the text editor should work exactly same as email composer's.
113 const long eEditorMailMask = 0x0020;
114 // allow the editor to set font: monospace on the root node
115 const long eEditorEnableWrapHackMask = 0x0040;
116 // If you want to move focus from an HTML editor with tab navigation,
117 // specify this flag. This is not available with text editors becase
118 // it's always tabbable.
119 // Note that if this is not specified, link navigation is also enabled in
120 // the editable content.
121 const long eEditorAllowInteraction = 0x0200;
122 // when this flag is set, the internal direction of the editor is RTL.
123 // if neither of the direction flags are set, the direction is determined
124 // from the text control's content node.
125 const long eEditorRightToLeft = 0x0800;
126 // when this flag is set, the internal direction of the editor is LTR.
127 const long eEditorLeftToRight = 0x1000;
128 // when this flag is set, the editor's text content is not spell checked.
129 const long eEditorSkipSpellCheck = 0x2000;
132 * The valid values for newlines handling.
133 * Can't change the values unless we remove
134 * use of the pref.
136 const long eNewlinesPasteIntact = 0;
137 const long eNewlinesPasteToFirst = 1;
138 const long eNewlinesReplaceWithSpaces = 2;
139 const long eNewlinesStrip = 3;
140 const long eNewlinesReplaceWithCommas = 4;
141 const long eNewlinesStripSurroundingWhitespace = 5;
143 readonly attribute Selection selection;
145 [can_run_script]
146 void setAttributeOrEquivalent(in Element element,
147 in AString sourceAttrName,
148 in AString sourceAttrValue,
149 in boolean aSuppressTransaction);
150 [can_run_script]
151 void removeAttributeOrEquivalent(in Element element,
152 in AString sourceAttrName,
153 in boolean aSuppressTransaction);
155 /** edit flags for this editor. May be set at any time. */
156 [setter_can_run_script] attribute unsigned long flags;
159 * the MimeType of the document
161 attribute AString contentsMIMEType;
163 /** Returns true if we have a document that is not marked read-only */
164 readonly attribute boolean isDocumentEditable;
166 /** Returns true if the current selection anchor is editable */
167 readonly attribute boolean isSelectionEditable;
170 * the DOM Document this editor is associated with, refcounted.
172 readonly attribute Document document;
174 /** the body element, i.e. the root of the editable document.
176 readonly attribute Element rootElement;
179 * the selection controller for the current presentation, refcounted.
181 readonly attribute nsISelectionController selectionController;
184 /* ------------ Selected content removal -------------- */
187 * DeleteSelection removes all nodes in the current selection.
188 * @param aDir if eNext, delete to the right (for example, the DEL key)
189 * if ePrevious, delete to the left (for example, the BACKSPACE key)
190 * @param stripWrappers If eStrip, strip any empty inline elements left
191 * behind after the deletion; if eNoStrip, don't. If in
192 * doubt, pass eStrip -- eNoStrip is only for if you're
193 * about to insert text or similar right after.
195 [can_run_script]
196 void deleteSelection(in short action, in short stripWrappers);
199 /* ------------ Document info and file methods -------------- */
201 /** Returns true if the document has no *meaningful* content */
202 readonly attribute boolean documentIsEmpty;
204 /** Returns true if the document is modifed and needs saving */
205 readonly attribute boolean documentModified;
208 * Sets document's character set. This is available only when the editor
209 * instance is an HTMLEditor since it's odd to change character set of
210 * parent document of `<input>` and `<textarea>`.
212 [setter_can_run_script]
213 attribute ACString documentCharacterSet;
215 /** to be used ONLY when we need to override the doc's modification
216 * state (such as when it's saved).
218 [can_run_script]
219 void resetModificationCount();
221 /** Gets the modification count of the document we are editing.
222 * @return the modification count of the document being edited.
223 * Zero means unchanged.
225 long getModificationCount();
227 /** called each time we modify the document.
228 * Increments the modification count of the document.
229 * @param aModCount the number of modifications by which
230 * to increase or decrease the count
232 [can_run_script]
233 void incrementModificationCount(in long aModCount);
235 /* ------------ Transaction methods -------------- */
237 /** turn the undo system on or off
238 * @param aEnable if PR_TRUE, the undo system is turned on if available
239 * if PR_FALSE the undo system is turned off if it
240 * was previously on
241 * @return if aEnable is PR_TRUE, returns NS_OK if
242 * the undo system could be initialized properly
243 * if aEnable is PR_FALSE, returns NS_OK.
245 void enableUndo(in boolean enable);
248 * Returns true when undo/redo is enabled (by default).
250 [infallible] readonly attribute boolean undoRedoEnabled;
253 * Retruns true when undo/redo is enabled and there is one or more transaction
254 * in the undo stack.
256 [infallible] readonly attribute boolean canUndo;
259 * Returns true when undo/redo is enabled and there is one or more transaction
260 * in the redo stack.
262 [infallible] readonly attribute boolean canRedo;
265 * Clears the transactions both for undo and redo.
266 * This may fail if you call this while editor is handling something, i.e.,
267 * don't call this from a legacy mutation event listeners, then, you won't
268 * see any exceptions.
270 [binaryname(ClearUndoRedoXPCOM)]
271 void clearUndoRedo();
274 * Undo the topmost transaction in the undo stack.
275 * This may throw exception when this is called while editor is handling
276 * transactions.
278 [can_run_script]
279 void undo();
282 * Undo all transactions in the undo stack.
283 * This may throw exception when this is called while editor is handling
284 * transactions.
286 [can_run_script]
287 void undoAll();
290 * Redo the topmost transaction in the redo stack.
291 * This may throw exception when this is called while editor is handling
292 * transactions.
294 [can_run_script]
295 void redo();
297 /** beginTransaction is a signal from the caller to the editor that
298 * the caller will execute multiple updates to the content tree
299 * that should be treated as a single logical operation,
300 * in the most efficient way possible.<br>
301 * All transactions executed between a call to beginTransaction and
302 * endTransaction will be undoable as an atomic action.<br>
303 * endTransaction must be called after beginTransaction.<br>
304 * Calls to beginTransaction can be nested, as long as endTransaction
305 * is called once per beginUpdate.
307 [can_run_script]
308 void beginTransaction();
310 /** endTransaction is a signal to the editor that the caller is
311 * finished updating the content model.<br>
312 * beginUpdate must be called before endTransaction is called.<br>
313 * Calls to beginTransaction can be nested, as long as endTransaction
314 * is called once per beginTransaction.
316 [can_run_script]
317 void endTransaction();
319 /* ------------ Inline Spell Checking methods -------------- */
321 /** Returns the inline spell checker associated with this object. The spell
322 * checker is lazily created, so this function may create the object for
323 * you during this call.
324 * @param autoCreate If true, this will create a spell checker object
325 * if one does not exist yet for this editor. If false
326 * and the object has not been created, this function
327 * WILL RETURN NULL.
329 nsIInlineSpellChecker getInlineSpellChecker(in boolean autoCreate);
331 /** Called when the user manually overrides the spellchecking state for this
332 * editor.
333 * @param enable The new state of spellchecking in this editor, as
334 * requested by the user.
336 void setSpellcheckUserOverride(in boolean enable);
338 /* ------------ Clipboard methods -------------- */
340 /** cut the currently selected text, putting it into the OS clipboard
341 * What if no text is selected?
342 * What about mixed selections?
343 * What are the clipboard formats?
345 [can_run_script]
346 void cut();
349 * canCut() returns true if selected content is allowed to be copied to the
350 * clipboard and to be removed.
351 * Note that this always returns true if the editor is in a non-chrome
352 * HTML/XHTML document.
353 * FYI: Current user in script is only BlueGriffon.
355 [can_run_script]
356 boolean canCut();
358 /** copy the currently selected text, putting it into the OS clipboard
359 * What if no text is selected?
360 * What about mixed selections?
361 * What are the clipboard formats?
363 [can_run_script]
364 void copy();
367 * canCopy() returns true if selected content is allowed to be copied to
368 * the clipboard.
369 * Note that this always returns true if the editor is in a non-chrome
370 * HTML/XHTML document.
371 * FYI: Current user in script is only BlueGriffon.
373 [can_run_script]
374 boolean canCopy();
376 /** paste the text in the OS clipboard at the cursor position, replacing
377 * the selected text (if any)
379 [can_run_script]
380 void paste(in long aClipboardType);
382 /** Paste the text in |aTransferable| at the cursor position, replacing the
383 * selected text (if any).
385 [can_run_script]
386 void pasteTransferable(in nsITransferable aTransferable);
388 /** Can we paste? True if the doc is modifiable, and we have
389 * pasteable data in the clipboard.
391 boolean canPaste(in long aClipboardType);
393 /* ------------ Selection methods -------------- */
395 /** sets the document selection to the entire contents of the document */
396 [can_run_script]
397 void selectAll();
400 * Collapses selection at start of the document. If it's an HTML editor,
401 * collapses selection at start of current editing host (<body> element if
402 * it's in designMode) instead. If there is a non-editable node before any
403 * editable text nodes or inline elements which can have text nodes as their
404 * children, collapses selection at start of the editing host. If there is
405 * an editable text node which is not collapsed, collapses selection at
406 * start of the text node. If there is an editable inline element which
407 * cannot have text nodes as its child, collapses selection at before the
408 * element node. Otherwise, collapses selection at start of the editing
409 * host.
411 [can_run_script]
412 void beginningOfDocument();
415 * Sets the selection to the end of the last leaf child/descendant or the root
416 * element.
418 [can_run_script]
419 void endOfDocument();
421 /* ------------ Node manipulation methods -------------- */
424 * setAttribute() sets the attribute of aElement.
425 * No checking is done to see if aAttribute is a legal attribute of the node,
426 * or if aValue is a legal value of aAttribute.
428 * @param aElement the content element to operate on
429 * @param aAttribute the string representation of the attribute to set
430 * @param aValue the value to set aAttribute to
432 [can_run_script]
433 void setAttribute(in Element aElement, in AString attributestr,
434 in AString attvalue);
437 * removeAttribute() deletes aAttribute from the attribute list of aElement.
438 * If aAttribute is not an attribute of aElement, nothing is done.
440 * @param aElement the content element to operate on
441 * @param aAttribute the string representation of the attribute to get
443 [can_run_script]
444 void removeAttribute(in Element aElement,
445 in AString aAttribute);
448 * cloneAttributes() is similar to Node::cloneNode(),
449 * it assures the attribute nodes of the destination are identical
450 * with the source node by copying all existing attributes from the
451 * source and deleting those not in the source.
452 * This is used when the destination element already exists
454 * @param aDestNode the destination element to operate on
455 * @param aSourceNode the source element to copy attributes from
457 [can_run_script]
458 void cloneAttributes(in Element aDestElement, in Element aSourceElement);
461 * insertNode inserts aNode into aParent at aPosition and this operation is
462 * undoable.
463 * No checking is done to verify the legality of the insertion.
464 * That is the responsibility of the caller.
465 * TODO: Move this method to nsIHTMLEditor, TextEditor does not allow chrome
466 * script to customize its anonymous subtree.
468 * @param aNode The DOM Node to insert.
469 * @param aParent The node to insert the new object into
470 * @param aPosition The place in aParent to insert the new node
471 * 0=first child, 1=second child, etc.
472 * If larger than number of children of aParent,
473 * this will append aNode into aParent.
474 * @param aPreseveSelection The default value is false. If set to true,
475 * the insert node handler does not update
476 * Selection.
477 * FYI: If somebody handles `beforeinput` event or
478 * `input` event caused by this and it does
479 * something undoable, selection may be changed by
480 * that.
482 [optional_argc, can_run_script]
483 void insertNode(in Node node,
484 in Node parent,
485 in unsigned long aPosition,
486 [optional] in boolean aPreserveSelection);
490 * deleteNode removes aChild from aParent and this operation is undobable.
491 * TODO: Move this method to nsIHTMLEditor, TextEditor does not allow chrome
492 * script to customize its anonymous subtree.
494 * @param aChild The node to delete
495 * @param aPreseveSelection The default value is false. If set to true,
496 * the insert node handler does not update
497 * Selection.
498 * FYI: If somebody handles `beforeinput` event or
499 * `input` event caused by this and it does
500 * something undoable, selection may be changed by
501 * that.
503 [optional_argc, can_run_script]
504 void deleteNode(in Node child, [optional] in boolean aPreserveSelection);
506 /* ------------ Output methods -------------- */
509 * Output methods:
510 * aFormatType is a mime type, like text/plain.
512 AString outputToString(in AString formatType,
513 in unsigned long flags);
515 /* ------------ Various listeners methods --------------
516 * nsIEditor holds strong references to the editor observers, action listeners
517 * and document state listeners.
520 /** add an EditActionListener to the editors list of listeners. */
521 void addEditActionListener(in nsIEditActionListener listener);
523 /** Remove an EditActionListener from the editor's list of listeners. */
524 void removeEditActionListener(in nsIEditActionListener listener);
526 /** Add a DocumentStateListener to the editors list of doc state listeners. */
527 void addDocumentStateListener(in nsIDocumentStateListener listener);
529 /** Remove a DocumentStateListener to the editors list of doc state listeners. */
530 void removeDocumentStateListener(in nsIDocumentStateListener listener);
533 * forceCompositionEnd() force the composition end
535 void forceCompositionEnd();
538 * whether this editor has active IME transaction
540 readonly attribute boolean composing;
543 * unmask() is available only when the editor is a passwrod field. This
544 * unmasks characters in specified by aStart and aEnd. If there have
545 * already unmasked characters, they are masked when this is called.
546 * Note that if you calls this without non-zero `aTimeout`, you bear
547 * responsibility for masking password with calling `mask()`. I.e.,
548 * user inputting password won't be masked automacitally. If user types
549 * a new character and echo is enabled, unmasked range is expanded to
550 * including it.
552 * @param aStart Optional, first index to show the character. If you
553 * specify middle of a surrogate pair, this expands the
554 * range to include the prceding high surrogate
555 * automatically.
556 * If omitted, it means that all characters of the
557 * password becomes unmasked.
558 * @param aEnd Optional, next index of last unmasked character. If
559 * you specify middle of a surrogate pair, the expands
560 * the range to include the following low surrogate.
561 * If omitted or negative value, it means unmasking all
562 * characters after aStart. Specifying same index
563 * throws an exception.
564 * @param aTimeout Optional, specify milliseconds to hide the unmasked
565 * characters if you want to show them temporarily.
566 * If omitted or 0, it means this won't mask the characters
567 * automatically.
569 [can_run_script, optional_argc] void unmask(
570 [optional] in unsigned long aStart,
571 [optional] in long long aEnd,
572 [optional] in unsigned long aTimeout);
575 * mask() is available only when the editor is a password field. This masks
576 * all unmasked characters immediately.
578 [can_run_script] void mask();
581 * These attributes are available only when the editor is a password field.
582 * unmaskedStart is first unmasked character index, or 0 if there is no
583 * unmasked characters.
584 * unmaskedEnd is next index of the last unmasked character. 0 means there
585 * is no unmasked characters.
587 readonly attribute unsigned long unmaskedStart;
588 readonly attribute unsigned long unmaskedEnd;
591 * autoMaskingEnabled is true if unmasked range and newly inputted characters
592 * are masked automatically. That's the default state. If false, until
593 * `mask()` is called, unmasked range and newly inputted characters are
594 * unmasked.
596 readonly attribute boolean autoMaskingEnabled;
599 * passwordMask attribute is a mask character which is used to mask password.
601 readonly attribute AString passwordMask;
604 * The length of the contents in characters.
606 readonly attribute unsigned long textLength;
608 /** Get and set newline handling.
610 * Values are the constants defined above.
612 attribute long newlineHandling;
615 * Inserts a string at the current location,
616 * given by the selection.
617 * If the selection is not collapsed, the selection is deleted
618 * and the insertion takes place at the resulting collapsed selection.
620 * @param aString the string to be inserted
622 [can_run_script]
623 void insertText(in AString aStringToInsert);
626 * Insert a line break into the content model.
627 * The interpretation of a break is up to the implementation:
628 * it may enter a character, split a node in the tree, etc.
629 * This may be more efficient than calling InsertText with a newline.
631 [can_run_script]
632 void insertLineBreak();
634 %{C++
635 inline bool IsHTMLEditor() const;
636 inline bool IsTextEditor() const;
639 * AsEditorBase() returns a pointer to EditorBase class.
641 * In order to avoid circular dependency issues, this method is defined
642 * in mozilla/EditorBase.h. Consumers need to #include that header.
644 inline mozilla::EditorBase* AsEditorBase();
645 inline const mozilla::EditorBase* AsEditorBase() const;
648 * AsTextEditor() and GetTextEditor() return a pointer to TextEditor class.
649 * AsTextEditor() does not check the concrete class type. So, it never
650 * returns nullptr. GetAsTextEditor() does check the concrete class type.
651 * So, it may return nullptr.
653 * In order to avoid circular dependency issues, this method is defined
654 * in mozilla/TextEditor.h. Consumers need to #include that header.
656 inline mozilla::TextEditor* AsTextEditor();
657 inline const mozilla::TextEditor* AsTextEditor() const;
658 inline mozilla::TextEditor* GetAsTextEditor();
659 inline const mozilla::TextEditor* GetAsTextEditor() const;
662 * AsHTMLEditor() and GetHTMLEditor() return a pointer to HTMLEditor class.
663 * AsHTMLEditor() does not check the concrete class type. So, it never
664 * returns nullptr. GetAsHTMLEditor() does check the concrete class type.
665 * So, it may return nullptr.
667 * In order to avoid circular dependency issues, this method is defined
668 * in mozilla/HTMLEditor.h. Consumers need to #include that header.
670 inline mozilla::HTMLEditor* AsHTMLEditor();
671 inline const mozilla::HTMLEditor* AsHTMLEditor() const;
672 inline mozilla::HTMLEditor* GetAsHTMLEditor();
673 inline const mozilla::HTMLEditor* GetAsHTMLEditor() const;