diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index dd427ae53257..2c6ac03c7e35 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -4273,6 +4273,31 @@ nsresult EditorBase::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) { return NS_OK; } +nsresult EditorBase::OnInputText(const nsAString& aStringToInsert) { + AutoEditActionDataSetter editActionData(*this, EditAction::eInsertText); + MOZ_ASSERT(!aStringToInsert.IsVoid()); + editActionData.SetData(aStringToInsert); + // FYI: For conforming to current UI Events spec, we should dispatch + // "beforeinput" event before "keypress" event, but here is in a + // "keypress" event listener. However, the other browsers dispatch + // "beforeinput" event after "keypress" event. Therefore, it makes + // sense to follow the other browsers. Spec issue: + // https://github.com/w3c/uievents/issues/220 + nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent(); + if (NS_FAILED(rv)) { + NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED, + "CanHandleAndMaybeDispatchBeforeInputEvent() failed"); + return EditorBase::ToGenericNSResult(rv); + } + + AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName, + ScrollSelectionIntoView::Yes); + rv = InsertTextAsSubAction(aStringToInsert); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), + "EditorBase::InsertTextAsSubAction() failed"); + return EditorBase::ToGenericNSResult(rv); +} + nsresult EditorBase::HandleInlineSpellCheck( const EditorDOMPoint& aPreviouslySelectedStart, const AbstractRange* aRange) { diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index 7b1430799898..5878f3b47705 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -1524,6 +1524,14 @@ class EditorBase : public nsIEditor, */ bool IsSelectionRangeContainerNotContent() const; + /** + * OnInputText() is called when user inputs text with keyboard or something. + * + * @param aStringToInsert The string to insert. + */ + [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult + OnInputText(const nsAString& aStringToInsert); + /** * InsertTextAsSubAction() inserts aStringToInsert at selection. This * should be used for handling it as an edit sub-action. diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index f87c4184b658..3e1fdf8c3265 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -344,7 +344,7 @@ nsresult TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) { aKeyboardEvent->PreventDefault(); nsresult rv = OnInputText(u"\t"_ns); NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), - "TextEditor::OnInputText(\\t) failed"); + "EditorBase::OnInputText(\\t) failed"); return rv; } case NS_VK_RETURN: { @@ -380,35 +380,10 @@ nsresult TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) { aKeyboardEvent->PreventDefault(); nsAutoString str(charCode); nsresult rv = OnInputText(str); - NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "TextEditor::OnInputText() failed"); + NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "EditorBase::OnInputText() failed"); return rv; } -nsresult TextEditor::OnInputText(const nsAString& aStringToInsert) { - AutoEditActionDataSetter editActionData(*this, EditAction::eInsertText); - MOZ_ASSERT(!aStringToInsert.IsVoid()); - editActionData.SetData(aStringToInsert); - // FYI: For conforming to current UI Events spec, we should dispatch - // "beforeinput" event before "keypress" event, but here is in a - // "keypress" event listener. However, the other browsers dispatch - // "beforeinput" event after "keypress" event. Therefore, it makes - // sense to follow the other browsers. Spec issue: - // https://github.com/w3c/uievents/issues/220 - nsresult rv = editActionData.CanHandleAndMaybeDispatchBeforeInputEvent(); - if (NS_FAILED(rv)) { - NS_WARNING_ASSERTION(rv == NS_ERROR_EDITOR_ACTION_CANCELED, - "CanHandleAndMaybeDispatchBeforeInputEvent() failed"); - return EditorBase::ToGenericNSResult(rv); - } - - AutoPlaceholderBatch treatAsOneTransaction(*this, *nsGkAtoms::TypingTxnName, - ScrollSelectionIntoView::Yes); - rv = InsertTextAsSubAction(aStringToInsert); - NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), - "EditorBase::InsertTextAsSubAction() failed"); - return EditorBase::ToGenericNSResult(rv); -} - nsresult TextEditor::InsertLineBreakAsAction(nsIPrincipal* aPrincipal) { AutoEditActionDataSetter editActionData(*this, EditAction::eInsertLineBreak, aPrincipal); diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h index c1a7b3df054d..a5f15034c7fc 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -632,14 +632,6 @@ class TextEditor : public EditorBase, public nsITimerCallback, public nsINamed { */ MOZ_CAN_RUN_SCRIPT virtual nsresult SelectEntireDocument() override; - /** - * OnInputText() is called when user inputs text with keyboard or something. - * - * @param aStringToInsert The string to insert. - */ - [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult - OnInputText(const nsAString& aStringToInsert); - /** * PrepareInsertContent() is a helper method of InsertTextAt(), * HTMLEditor::DoInsertHTMLWithContext(). They insert content coming from