Bug 1789967 - part 1: Make `TextEditor` and `HTMLEditor` implement `EditorBase::InitEditorContentAndSelection` by themselves r=m_kato CLOSED TREE

The method is enough simple, and uses bad cast from point of view of OOP.
Therefore, this patch make the sub classes implement the method only for each.

Differential Revision: https://phabricator.services.mozilla.com/D157406
This commit is contained in:
Masayuki Nakano 2022-09-21 00:20:26 +00:00
Родитель 52da19beca
Коммит f3ce7840c7
6 изменённых файлов: 60 добавлений и 53 удалений

Просмотреть файл

@ -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());

Просмотреть файл

@ -2245,13 +2245,6 @@ class EditorBase : public nsIEditor,
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult EnsureEmptyTextFirstChild();
/**
* InitEditorContentAndSelection() may insert a padding `<br>` element for
* if it's required in the anonymous `<div>` element or `<body>` 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; }
/**

Просмотреть файл

@ -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;

Просмотреть файл

@ -2682,9 +2682,6 @@ class HTMLEditor final : public EditorBase,
* InitEditorContentAndSelection() may insert `<br>` elements and padding
* `<br>` elements if they are required for `<body>` 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 `<body>`
* or `<html>` has `contenteditable` attribute).
*/
[[nodiscard]] MOZ_CAN_RUN_SCRIPT nsresult InitEditorContentAndSelection();

Просмотреть файл

@ -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())) {

Просмотреть файл

@ -533,6 +533,13 @@ class TextEditor final : public EditorBase,
*/
bool CanEchoPasswordNow() const;
/**
* InitEditorContentAndSelection() may insert a padding `<br>` element for
* if it's required in the anonymous `<div>` element or `<body>` 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.
*/