diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 026f48a2ba79..3e780941aba0 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -339,9 +339,33 @@ nsresult EditorBase::Init(Document& aDocument, Element* aRoot, return NS_OK; } +nsresult EditorBase::EnsureEmptyTextFirstChild() { + MOZ_ASSERT(IsTextEditor()); + RefPtr root = GetRoot(); + nsIContent* firstChild = root->GetFirstChild(); + + if (!firstChild || !firstChild->IsText()) { + RefPtr newTextNode = CreateTextNode(u""_ns); + if (!newTextNode) { + NS_WARNING("EditorBase::CreateTextNode() failed"); + return NS_ERROR_UNEXPECTED; + } + IgnoredErrorResult ignoredError; + root->InsertChildBefore(newTextNode, root->GetFirstChild(), true, + ignoredError); + MOZ_ASSERT(!ignoredError.Failed()); + } + + return NS_OK; +} + nsresult EditorBase::InitEditorContentAndSelection() { MOZ_ASSERT(IsEditActionDataAvailable()); + if (IsTextEditor()) { + MOZ_TRY(EnsureEmptyTextFirstChild()); + } + nsresult rv = MaybeCreatePaddingBRElementForEmptyEditor(); if (NS_FAILED(rv)) { NS_WARNING( diff --git a/editor/libeditor/EditorBase.h b/editor/libeditor/EditorBase.h index b0aaa197b041..0be5e4dcc539 100644 --- a/editor/libeditor/EditorBase.h +++ b/editor/libeditor/EditorBase.h @@ -2240,6 +2240,8 @@ class EditorBase : public nsIEditor, return mIsHTMLEditorClass ? EditorType::HTML : EditorType::Text; } + [[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 diff --git a/editor/libeditor/TextEditor.h b/editor/libeditor/TextEditor.h index c0bb713f91a1..44f9bd27104a 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -82,7 +82,7 @@ class TextEditor final : public EditorBase, MOZ_CAN_RUN_SCRIPT nsresult Init(Document& aDoc, Element* aRoot, nsISelectionController* aSelCon, uint32_t aFlags, - const nsAString& aValue) final; + const nsAString& aInitialValue) final; bool IsEmpty() const final;