diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index c0bf59b3a6c6..7072f3c13fe1 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -376,46 +376,6 @@ nsresult EditorBase::EnsureEmptyTextFirstChild() { return NS_OK; } -nsresult EditorBase::InitEditorContentAndSelection() { - MOZ_ASSERT(IsEditActionDataAvailable()); - - if (IsTextEditor()) { - MOZ_TRY(EnsureEmptyTextFirstChild()); - } else { - nsresult rv = MOZ_KnownLive(AsHTMLEditor()) - ->MaybeCreatePaddingBRElementForEmptyEditor(); - if (NS_FAILED(rv)) { - NS_WARNING( - "HTMLEditor::MaybeCreatePaddingBRElementForEmptyEditor() failed"); - return rv; - } - } - - // If the selection hasn't been set up yet, set it up collapsed to the end of - // our editable content. - // XXX I think that this shouldn't do it in `HTMLEditor` because it maybe - // removed by the web app and if they call `Selection::AddRange()`, - // it may cause multiple selection ranges. - if (!SelectionRef().RangeCount()) { - nsresult rv = CollapseSelectionToEndOfLastLeafNode(); - if (MOZ_UNLIKELY(NS_FAILED(rv))) { - NS_WARNING("EditorBase::CollapseSelectionToEndOfLastLeafNode() failed"); - return rv; - } - } - - if (IsInPlaintextMode() && !IsSingleLineEditor()) { - nsresult rv = EnsurePaddingBRElementInMultilineEditor(); - if (NS_FAILED(rv)) { - NS_WARNING( - "EditorBase::EnsurePaddingBRElementInMultilineEditor() failed"); - return rv; - } - } - - return NS_OK; -} - nsresult EditorBase::PostCreateInternal() { MOZ_ASSERT(IsEditActionDataAvailable()); diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index 9cd575086d87..d6957b22a2d7 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -2245,13 +2245,6 @@ class EditorBase : public nsIEditor, [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult EnsureEmptyTextFirstChild(); - /** - * InitEditorContentAndSelection() may insert a padding `
` element for - * if it's required in the anonymous `
` element or `` element and - * collapse selection at the end if there is no selection ranges. - */ - [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult InitEditorContentAndSelection(); - int32_t WrapWidth() const { return mWrapColumn; } /** diff --git a/editor/libeditor/HTMLEditSubActionHandler.cpp b/editor/libeditor/HTMLEditSubActionHandler.cpp index 5d4c7c841f6d..222777b0123e 100644 --- a/editor/libeditor/HTMLEditSubActionHandler.cpp +++ b/editor/libeditor/HTMLEditSubActionHandler.cpp @@ -131,12 +131,35 @@ HTMLEditor::CreateRangeIncludingAdjuscentWhiteSpaces( nsresult HTMLEditor::InitEditorContentAndSelection() { MOZ_ASSERT(IsEditActionDataAvailable()); - nsresult rv = EditorBase::InitEditorContentAndSelection(); + nsresult rv = MaybeCreatePaddingBRElementForEmptyEditor(); if (NS_FAILED(rv)) { - NS_WARNING("EditorBase::InitEditorContentAndSelection() failed"); + NS_WARNING( + "HTMLEditor::MaybeCreatePaddingBRElementForEmptyEditor() failed"); return rv; } + // If the selection hasn't been set up yet, set it up collapsed to the end of + // our editable content. + // XXX I think that this shouldn't do it in `HTMLEditor` because it maybe + // removed by the web app and if they call `Selection::AddRange()`, + // it may cause multiple selection ranges. + if (!SelectionRef().RangeCount()) { + nsresult rv = CollapseSelectionToEndOfLastLeafNode(); + if (NS_FAILED(rv)) { + NS_WARNING("EditorBase::CollapseSelectionToEndOfLastLeafNode() failed"); + return rv; + } + } + + if (IsInPlaintextMode()) { + nsresult rv = EnsurePaddingBRElementInMultilineEditor(); + if (NS_FAILED(rv)) { + NS_WARNING( + "EditorBase::EnsurePaddingBRElementInMultilineEditor() failed"); + return rv; + } + } + Element* bodyOrDocumentElement = GetRoot(); if (NS_WARN_IF(!bodyOrDocumentElement && !GetDocument())) { return NS_ERROR_FAILURE; diff --git a/editor/libeditor/HTMLEditor.h b/editor/libeditor/HTMLEditor.h index 41b04f330bf5..194fc0747970 100644 --- a/editor/libeditor/HTMLEditor.h +++ b/editor/libeditor/HTMLEditor.h @@ -2682,9 +2682,6 @@ class HTMLEditor final : public EditorBase, * InitEditorContentAndSelection() may insert `
` elements and padding * `
` elements if they are required for `` or document element. * And collapse selection at the end if there is no selection ranges. - * XXX I think that this should work with active editing host unless - * all over the document is ediable (i.e., in design mode or `` - * or `` has `contenteditable` attribute). */ [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult InitEditorContentAndSelection(); diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index ca0cbf7a346f..145713957148 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -138,7 +138,7 @@ nsresult TextEditor::Init(Document& aDocument, Element& aAnonymousDivElement, rv = InitEditorContentAndSelection(); if (NS_FAILED(rv)) { - NS_WARNING("EditorBase::InitEditorContentAndSelection() failed"); + NS_WARNING("TextEditor::InitEditorContentAndSelection() failed"); // XXX Shouldn't we expose `NS_ERROR_EDITOR_DESTROYED` even though this // is a public method? mInitSucceeded = false; @@ -152,6 +152,33 @@ nsresult TextEditor::Init(Document& aDocument, Element& aAnonymousDivElement, return NS_OK; } +nsresult TextEditor::InitEditorContentAndSelection() { + MOZ_ASSERT(IsEditActionDataAvailable()); + + MOZ_TRY(EnsureEmptyTextFirstChild()); + + // If the selection hasn't been set up yet, set it up collapsed to the end of + // our editable content. + if (!SelectionRef().RangeCount()) { + nsresult rv = CollapseSelectionToEndOfLastLeafNode(); + if (NS_FAILED(rv)) { + NS_WARNING("EditorBase::CollapseSelectionToEndOfLastLeafNode() failed"); + return rv; + } + } + + if (!IsSingleLineEditor()) { + nsresult rv = EnsurePaddingBRElementInMultilineEditor(); + if (NS_FAILED(rv)) { + NS_WARNING( + "EditorBase::EnsurePaddingBRElementInMultilineEditor() failed"); + return rv; + } + } + + return NS_OK; +} + nsresult TextEditor::PostCreate() { AutoEditActionDataSetter editActionData(*this, EditAction::eNotEditing); if (NS_WARN_IF(!editActionData.CanHandle())) { diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h index 8a0db195478d..e6518fd01979 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -533,6 +533,13 @@ class TextEditor final : public EditorBase, */ bool CanEchoPasswordNow() const; + /** + * InitEditorContentAndSelection() may insert a padding `
` element for + * if it's required in the anonymous `
` element or `` element and + * collapse selection at the end if there is no selection ranges. + */ + [[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult InitEditorContentAndSelection(); + /** * Make the given selection span the entire document. */