Bug 1717156 - part 1: Make editor type specific flags clearer with MOZ_ASSERT r=m_kato

Some `nsIEditor::eEditor*Mask` flags are now only for `TextEditor` or
`HTMLEditor`.  For making it clearer, add `MOZ_ASSERT` to the `SetFlags` and
each flag accessor.

Differential Revision: https://phabricator.services.mozilla.com/D118261
This commit is contained in:
Masayuki Nakano 2021-06-22 00:18:05 +00:00
parent 5bce02c01c
commit 0e68fa3f7e
5 changed files with 74 additions and 23 deletions

View File

@ -675,13 +675,33 @@ NS_IMETHODIMP EditorBase::SetFlags(uint32_t aFlags) {
// If we're a `TextEditor` instance, the plaintext mode should always be set.
// If we're an `HTMLEditor` instance, either is fine.
MOZ_ASSERT_IF(IsTextEditor(), !!(aFlags & nsIEditor::eEditorPlaintextMask));
// If we're an `HTMLEditor` instance, we cannot treat it as a single line
// editor. So, eEditorSingleLineMask is available only when we're a
// `TextEditor` instance.
MOZ_ASSERT_IF(IsHTMLEditor(), !(aFlags & nsIEditor::eEditorSingleLineMask));
// If we're an `HTMLEditor` instance, we cannot treat it as a password editor.
// So, eEditorPasswordMask is available only when we're a `TextEditor`
// instance.
MOZ_ASSERT_IF(IsHTMLEditor(), !(aFlags & nsIEditor::eEditorPasswordMask));
// If we're a password editor, we show the last typed character for
// a while by default. eEditorDontEchoPassword prevents it. So, this flag
// is available only when we're a `TextEditor`.
MOZ_ASSERT_IF(IsHTMLEditor(), !(aFlags & nsIEditor::eEditorDontEchoPassword));
// eEditorMailMask specifies the editing rules of `HTMLEditor`. So, it's
// available only with `HTMLEditor` instance.
MOZ_ASSERT_IF(IsTextEditor(), !(aFlags & nsIEditor::eEditorMailMask));
// eEditorWidgetMask must be specified only when we're a `TextEditor`
// instance. So, it's not available when we're an `HTMLEditor` instance.
MOZ_ASSERT(IsTextEditor() == !!(aFlags & nsIEditor::eEditorWidgetMask));
// eEditorNoCSSMask specifies the editing rules of `HTMLEditor`. So, it's
// available only with `HTMLEditor` instance.
MOZ_ASSERT_IF(IsTextEditor(), !(aFlags & nsIEditor::eEditorNoCSSMask));
DebugOnly<bool> changingPasswordEditorFlagDynamically =
mFlags != ~aFlags && ((mFlags ^ aFlags) & nsIEditor::eEditorPasswordMask);
MOZ_ASSERT(
!changingPasswordEditorFlagDynamically,
"TextEditor does not support dynamic eEditorPasswordMask flag change");
bool spellcheckerWasEnabled = CanEnableSpellCheck();
const bool isCalledByPostCreate = (mFlags == ~aFlags);
// We don't support dynamic password flag change.
MOZ_ASSERT_IF(!isCalledByPostCreate,
!((mFlags ^ aFlags) & nsIEditor::eEditorPasswordMask));
bool spellcheckerWasEnabled = !isCalledByPostCreate && CanEnableSpellCheck();
mFlags = aFlags;
if (!IsInitialized()) {

View File

@ -558,11 +558,17 @@ class EditorBase : public nsIEditor,
}
bool IsSingleLineEditor() const {
return (mFlags & nsIEditor::eEditorSingleLineMask) != 0;
const bool isSingleLineEditor =
(mFlags & nsIEditor::eEditorSingleLineMask) != 0;
MOZ_ASSERT_IF(isSingleLineEditor, IsTextEditor());
return isSingleLineEditor;
}
bool IsPasswordEditor() const {
return (mFlags & nsIEditor::eEditorPasswordMask) != 0;
const bool isPasswordEditor =
(mFlags & nsIEditor::eEditorPasswordMask) != 0;
MOZ_ASSERT_IF(isPasswordEditor, IsTextEditor());
return isPasswordEditor;
}
// FYI: Both IsRightToLeft() and IsLeftToRight() may return false if
@ -583,7 +589,9 @@ class EditorBase : public nsIEditor,
}
bool IsMailEditor() const {
return (mFlags & nsIEditor::eEditorMailMask) != 0;
const bool isMailEditor = (mFlags & nsIEditor::eEditorMailMask) != 0;
MOZ_ASSERT_IF(isMailEditor, IsHTMLEditor());
return isMailEditor;
}
bool IsWrapHackEnabled() const {
@ -591,10 +599,16 @@ class EditorBase : public nsIEditor,
}
bool IsFormWidget() const {
return (mFlags & nsIEditor::eEditorWidgetMask) != 0;
const bool isFormWidget = (mFlags & nsIEditor::eEditorWidgetMask) != 0;
MOZ_ASSERT(isFormWidget == IsTextEditor());
return isFormWidget;
}
bool NoCSS() const { return (mFlags & nsIEditor::eEditorNoCSSMask) != 0; }
bool NoCSS() const {
const bool isNoCSS = (mFlags & nsIEditor::eEditorNoCSSMask) != 0;
MOZ_ASSERT_IF(!isNoCSS, IsHTMLEditor());
return isNoCSS;
}
bool IsInteractionAllowed() const {
return (mFlags & nsIEditor::eEditorAllowInteraction) != 0;
@ -609,7 +623,10 @@ class EditorBase : public nsIEditor,
IsInteractionAllowed();
}
bool HasIndependentSelection() const { return !!mSelectionController; }
bool HasIndependentSelection() const {
MOZ_ASSERT_IF(mSelectionController, IsTextEditor());
return !!mSelectionController;
}
bool IsModifiable() const { return !IsReadonly(); }

View File

@ -1040,8 +1040,6 @@ NS_IMETHODIMP HTMLEditor::UpdateBaseURL() {
}
NS_IMETHODIMP HTMLEditor::InsertLineBreak() {
MOZ_ASSERT(!IsSingleLineEditor());
// XPCOM method's InsertLineBreak() should insert paragraph separator in
// HTMLEditor.
AutoEditActionDataSetter editActionData(

View File

@ -50,30 +50,41 @@ interface nsIEditor : nsISupports
const short eStrip = 0;
const short eNoStrip = 1;
// only plain text entry is allowed via events
// If you want an HTML editor to behave as a plaintext editor, specify this
// flag. Note that this is always set if the instance is a text editor.
const long eEditorPlaintextMask = 0x0001;
// enter key and CR-LF handled specially
// We don't support single line editor mode with HTML editors. Therefore,
// don't specify this for HTML editor.
const long eEditorSingleLineMask = 0x0002;
// text is not entered into content, only a representative character
// We don't support password editor mode with HTML editors. Therefore,
// don't specify this for HTML editor.
const long eEditorPasswordMask = 0x0004;
// editing events are disabled. Editor may still accept focus.
// When the editor should be in readonly mode (currently, same as "disabled"),
// you can specify this flag with any editor instances. Note that setting
// this flag does not change the style of editor. This just changes the
// internal editor's readonly state.
const long eEditorReadonlyMask = 0x0008;
// text input is limited to certain character types, use mFilter
const long eEditorFilterInputMask = 0x0010;
// use mail-compose editing rules
// If you want an HTML editor to work as an email composer, specify this flag.
// So, this is not available with text editor instances.
const long eEditorMailMask = 0x0020;
// allow the editor to set font: monospace on the root node
const long eEditorEnableWrapHackMask = 0x0040;
// bit for widgets (form elements)
const long eEditorWidgetMask = 0x0080;
// this HTML editor should not create css styles
// If you want an HTML editor to set style of text with legacy HTML inline
// elements, specify this flag. So, this is not available with text editors.
const long eEditorNoCSSMask = 0x0100;
// whether HTML document specific actions are executed or not.
// e.g., if this flag is set, the editor doesn't handle Tab key.
// besides, anchors of HTML are not clickable.
const long eEditorAllowInteraction = 0x0200;
// when this is set, the characters in password editor are always masked.
// see bug 530367 for the detail.
// If you want typed character to be immediately masked in password editor,
// specify this flag. If you want to keep last input character(s) visible
// for a while, unset this. If you want to manage the masked range, you
// can use `unmask()` below.
// So, this flag is available only when the instance is a text editor.
const long eEditorDontEchoPassword = 0x0400;
// when this flag is set, the internal direction of the editor is RTL.
// if neither of the direction flags are set, the direction is determined

View File

@ -1201,6 +1201,11 @@ function runEditorFlagChangeTests() {
is(gUtils.IMEStatus, gUtils.IME_STATUS_ENABLED,
description + "IME isn't enabled on HTML editor");
const kIMEStateChangeFlags = Ci.nsIEditor.eEditorReadonlyMask;
const kFlagsNotAllowedWithHTMLEditor =
Ci.nsIEditor.eEditorPasswordMask |
Ci.nsIEditor.eEditorSingleLineMask |
Ci.nsIEditor.eEditorWidgetMask |
Ci.nsIEditor.eEditorDontEchoPassword;
var editor = window.docShell.editor;
var flags = editor.flags;
@ -1223,7 +1228,7 @@ function runEditorFlagChangeTests() {
description + "#1 IME isn't enabled on HTML editor");
editor.flags |=
~(kIMEStateChangeFlags | Ci.nsIEditor.eEditorPasswordMask);
~(kIMEStateChangeFlags | kFlagsNotAllowedWithHTMLEditor);
ok(editor.composing,
description + "#2 IME composition was committed unexpectedly");
is(gUtils.IMEStatus, gUtils.IME_STATUS_ENABLED,