Bug 1540029 - part 9: Move TextEditRules::Init() and HTMLEditRules::Init() to TextEditor and HTMLEditor r=m_kato

Differential Revision: https://phabricator.services.mozilla.com/D45791

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-09-18 08:21:37 +00:00
parent 8b7e7ba3d1
commit d2ae4d5d88
8 changed files with 51 additions and 61 deletions

View File

@ -232,50 +232,33 @@ HTMLEditRules::HTMLEditRules() : mHTMLEditor(nullptr), mInitialized(false) {
mIsHTMLEditRules = true;
}
nsresult HTMLEditRules::Init(TextEditor* aTextEditor) {
if (NS_WARN_IF(!aTextEditor) || NS_WARN_IF(!aTextEditor->AsHTMLEditor())) {
return NS_ERROR_INVALID_ARG;
}
nsresult HTMLEditor::InitEditorContentAndSelection() {
MOZ_ASSERT(IsEditActionDataAvailable());
mHTMLEditor = aTextEditor->AsHTMLEditor();
if (NS_WARN_IF(!mHTMLEditor)) {
return NS_ERROR_FAILURE;
}
AutoSafeEditorData setData(*this, *mHTMLEditor);
nsresult rv = TextEditRules::Init(aTextEditor);
nsresult rv = TextEditor::InitEditorContentAndSelection();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (NS_WARN_IF(!mHTMLEditor)) {
Element* bodyOrDocumentElement = GetRoot();
if (NS_WARN_IF(!bodyOrDocumentElement && !GetDocument())) {
return NS_ERROR_FAILURE;
}
Element* bodyOrDocumentElement = HTMLEditorRef().GetRoot();
if (NS_WARN_IF(!bodyOrDocumentElement && !HTMLEditorRef().GetDocument())) {
return NS_ERROR_FAILURE;
if (!bodyOrDocumentElement) {
return NS_OK;
}
// make a utility range for use by the listenter
if (bodyOrDocumentElement) {
nsresult rv =
MOZ_KnownLive(HTMLEditorRef())
.InsertBRElementToEmptyListItemsAndTableCellsInRange(
RawRangeBoundary(bodyOrDocumentElement, 0),
RawRangeBoundary(bodyOrDocumentElement,
bodyOrDocumentElement->GetChildCount()));
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rv),
"Failed to insert <br> elements to empty list items and table cells");
rv = InsertBRElementToEmptyListItemsAndTableCellsInRange(
RawRangeBoundary(bodyOrDocumentElement, 0),
RawRangeBoundary(bodyOrDocumentElement,
bodyOrDocumentElement->GetChildCount()));
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
mInitialized = true; // Start to handle edit sub-actions.
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"InsertBRElementToEmptyListItemsAndTableCellsInRange() "
"failed, but ignored");
return NS_OK;
}

View File

@ -55,8 +55,6 @@ class HTMLEditRules : public TextEditRules {
HTMLEditRules();
// TextEditRules methods
MOZ_CAN_RUN_SCRIPT
virtual nsresult Init(TextEditor* aTextEditor) override;
virtual nsresult DetachEditor() override;
protected:

View File

@ -459,8 +459,10 @@ nsresult HTMLEditor::InitRules() {
// instantiate the rules for the html editor
mRules = new HTMLEditRules();
}
RefPtr<TextEditRules> rules(mRules);
return rules->Init(this);
nsresult rv = InitEditorContentAndSelection();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"InitEditorContentAndSelection() failed");
return rv;
}
NS_IMETHODIMP

View File

@ -2751,6 +2751,17 @@ class HTMLEditor final : public TextEditor,
protected: // Shouldn't be used by friend classes
virtual ~HTMLEditor();
/**
* 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).
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE virtual nsresult
InitEditorContentAndSelection() override;
MOZ_CAN_RUN_SCRIPT
virtual nsresult SelectAllInternal() override;

View File

@ -76,40 +76,28 @@ const HTMLEditRules* TextEditRules::AsHTMLEditRules() const {
return mIsHTMLEditRules ? static_cast<const HTMLEditRules*>(this) : nullptr;
}
nsresult TextEditRules::Init(TextEditor* aTextEditor) {
if (NS_WARN_IF(!aTextEditor)) {
return NS_ERROR_INVALID_ARG;
}
nsresult TextEditor::InitEditorContentAndSelection() {
MOZ_ASSERT(IsEditActionDataAvailable());
Selection* selection = aTextEditor->GetSelection();
if (NS_WARN_IF(!selection)) {
return NS_ERROR_FAILURE;
}
InitFields();
// We hold a non-refcounted reference back to our editor.
mTextEditor = aTextEditor;
AutoSafeEditorData setData(*this, *mTextEditor);
nsresult rv = MOZ_KnownLive(TextEditorRef())
.MaybeCreatePaddingBRElementForEmptyEditor();
nsresult rv = MaybeCreatePaddingBRElementForEmptyEditor();
if (NS_WARN_IF(NS_FAILED(rv))) {
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 (!SelectionRefPtr()->RangeCount()) {
rv = TextEditorRef().CollapseSelectionToEnd();
nsresult rv = CollapseSelectionToEnd();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (IsPlaintextEditor() && !IsSingleLineEditor()) {
nsresult rv = MOZ_KnownLive(TextEditorRef())
.EnsurePaddingBRElementInMultilineEditor();
nsresult rv = EnsurePaddingBRElementInMultilineEditor();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

View File

@ -73,8 +73,6 @@ class TextEditRules {
HTMLEditRules* AsHTMLEditRules();
const HTMLEditRules* AsHTMLEditRules() const;
MOZ_CAN_RUN_SCRIPT
virtual nsresult Init(TextEditor* aTextEditor);
virtual nsresult DetachEditor();
protected:

View File

@ -321,8 +321,10 @@ nsresult TextEditor::InitRules() {
// instantiate the rules for this text editor
mRules = new TextEditRules();
}
RefPtr<TextEditRules> textEditRules(mRules);
return textEditRules->Init(this);
nsresult rv = InitEditorContentAndSelection();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"InitEditorContentAndSelection() failed");
return rv;
}
nsresult TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent) {

View File

@ -713,6 +713,14 @@ class TextEditor : public EditorBase,
protected: // Shouldn't be used by friend classes
virtual ~TextEditor();
/**
* InitEditorContentAndSelection() may insert a padding `<br>` element for
* if it's required in the anonymous `<div>` element and collapse selection
* at the end if there is no selection ranges.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE virtual nsresult
InitEditorContentAndSelection();
int32_t WrapWidth() const { return mWrapColumn; }
/**