diff --git a/editor/libeditor/EditorBase.cpp b/editor/libeditor/EditorBase.cpp index 026f48a2ba79..5cb575127e1a 100644 --- a/editor/libeditor/EditorBase.cpp +++ b/editor/libeditor/EditorBase.cpp @@ -339,9 +339,32 @@ 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, nullptr, 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 51e59bb73a3b..a75b94b6b10f 100644 --- a/editor/libeditor/TextEditor.h +++ b/editor/libeditor/TextEditor.h @@ -77,10 +77,9 @@ class TextEditor : public EditorBase, public nsITimerCallback, public nsINamed { using EditorBase::CanPaste; // Overrides of EditorBase - MOZ_CAN_RUN_SCRIPT virtual nsresult Init(Document& aDoc, Element* aRoot, - nsISelectionController* aSelCon, - uint32_t aFlags, - const nsAString& aValue) override; + MOZ_CAN_RUN_SCRIPT virtual nsresult Init( + Document& aDoc, Element* aRoot, nsISelectionController* aSelCon, + uint32_t aFlags, const nsAString& aInitialValue) override; bool IsEmpty() const override;