Bug 1574852 - part 47: Move `HTMLEditRules::ReturnInParagraph()` to `HTMLEditor` r=m_kato

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-09-02 08:49:18 +00:00
Родитель 187e20b6ae
Коммит e356eee3f9
3 изменённых файлов: 37 добавлений и 41 удалений

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

@ -1968,7 +1968,9 @@ EditActionResult HTMLEditRules::WillInsertParagraphSeparator() {
blockParent->IsAnyOfHTMLElements(nsGkAtoms::p, nsGkAtoms::div))) { blockParent->IsAnyOfHTMLElements(nsGkAtoms::p, nsGkAtoms::div))) {
AutoEditorDOMPointChildInvalidator lockOffset(atStartOfSelection); AutoEditorDOMPointChildInvalidator lockOffset(atStartOfSelection);
// Paragraphs: special rules to look for <br>s // Paragraphs: special rules to look for <br>s
EditActionResult result = ReturnInParagraph(*blockParent); EditActionResult result =
MOZ_KnownLive(HTMLEditorRef())
.HandleInsertParagraphInParagraph(*blockParent);
if (NS_WARN_IF(result.Failed())) { if (NS_WARN_IF(result.Failed())) {
return result; return result;
} }
@ -1979,9 +1981,9 @@ EditActionResult HTMLEditRules::WillInsertParagraphSeparator() {
lockOffset.Cancel(); lockOffset.Cancel();
return result; return result;
} }
// Fall through, if ReturnInParagraph() didn't handle it. // Fall through, if HandleInsertParagraphInParagraph() didn't handle it.
MOZ_ASSERT(!result.Canceled(), MOZ_ASSERT(!result.Canceled(),
"ReturnInParagraph canceled this edit action, " "HandleInsertParagraphInParagraph canceled this edit action, "
"WillInsertBreak() needs to handle such case"); "WillInsertBreak() needs to handle such case");
} }
@ -8100,8 +8102,9 @@ nsresult HTMLEditRules::ReturnInHeader(Element& aHeader, nsINode& aNode,
return NS_OK; return NS_OK;
} }
EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) { EditActionResult HTMLEditor::HandleInsertParagraphInParagraph(
MOZ_ASSERT(IsEditorDataAvailable()); Element& aParentDivOrP) {
MOZ_ASSERT(IsEditActionDataAvailable());
nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0); nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0);
if (NS_WARN_IF(!firstRange)) { if (NS_WARN_IF(!firstRange)) {
@ -8180,9 +8183,7 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
} }
} }
bool doesCRCreateNewP = bool doesCRCreateNewP = GetReturnInParagraphCreatesNewParagraph();
HTMLEditorRef().GetReturnInParagraphCreatesNewParagraph();
bool splitAfterNewBR = false; bool splitAfterNewBR = false;
nsCOMPtr<nsIContent> brContent; nsCOMPtr<nsIContent> brContent;
@ -8196,9 +8197,8 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
// at beginning of text node? // at beginning of text node?
if (atStartOfSelection.IsStartOfContainer()) { if (atStartOfSelection.IsStartOfContainer()) {
// is there a BR prior to it? // is there a BR prior to it?
brContent = HTMLEditorRef().GetPriorHTMLSibling( brContent = GetPriorHTMLSibling(atStartOfSelection.GetContainer());
atStartOfSelection.GetContainer()); if (!brContent || !IsVisibleBRElement(brContent) ||
if (!brContent || !HTMLEditorRef().IsVisibleBRElement(brContent) ||
EditorBase::IsPaddingBRElementForEmptyLastLine(*brContent)) { EditorBase::IsPaddingBRElementForEmptyLastLine(*brContent)) {
pointToInsertBR.Set(atStartOfSelection.GetContainer()); pointToInsertBR.Set(atStartOfSelection.GetContainer());
brContent = nullptr; brContent = nullptr;
@ -8206,9 +8206,8 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
} else if (atStartOfSelection.IsEndOfContainer()) { } else if (atStartOfSelection.IsEndOfContainer()) {
// we're at the end of text node... // we're at the end of text node...
// is there a BR after to it? // is there a BR after to it?
brContent = brContent = GetNextHTMLSibling(atStartOfSelection.GetContainer());
HTMLEditorRef().GetNextHTMLSibling(atStartOfSelection.GetContainer()); if (!brContent || !IsVisibleBRElement(brContent) ||
if (!brContent || !HTMLEditorRef().IsVisibleBRElement(brContent) ||
EditorBase::IsPaddingBRElementForEmptyLastLine(*brContent)) { EditorBase::IsPaddingBRElementForEmptyLastLine(*brContent)) {
pointToInsertBR.Set(atStartOfSelection.GetContainer()); pointToInsertBR.Set(atStartOfSelection.GetContainer());
DebugOnly<bool> advanced = pointToInsertBR.AdvanceOffset(); DebugOnly<bool> advanced = pointToInsertBR.AdvanceOffset();
@ -8221,9 +8220,8 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
if (doesCRCreateNewP) { if (doesCRCreateNewP) {
ErrorResult error; ErrorResult error;
nsCOMPtr<nsIContent> newLeftDivOrP = nsCOMPtr<nsIContent> newLeftDivOrP =
MOZ_KnownLive(HTMLEditorRef()) SplitNodeWithTransaction(pointToSplitParentDivOrP, error);
.SplitNodeWithTransaction(pointToSplitParentDivOrP, error); if (NS_WARN_IF(Destroyed())) {
if (NS_WARN_IF(!CanHandleEditAction())) {
error.SuppressException(); error.SuppressException();
return EditActionResult(NS_ERROR_EDITOR_DESTROYED); return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
} }
@ -8244,13 +8242,13 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
} else { } else {
// not in a text node. // not in a text node.
// is there a BR prior to it? // is there a BR prior to it?
nsCOMPtr<nsIContent> nearNode; nsCOMPtr<nsIContent> nearNode =
nearNode = HTMLEditorRef().GetPreviousEditableHTMLNode(atStartOfSelection); GetPreviousEditableHTMLNode(atStartOfSelection);
if (!nearNode || !HTMLEditorRef().IsVisibleBRElement(nearNode) || if (!nearNode || !IsVisibleBRElement(nearNode) ||
EditorBase::IsPaddingBRElementForEmptyLastLine(*nearNode)) { EditorBase::IsPaddingBRElementForEmptyLastLine(*nearNode)) {
// is there a BR after it? // is there a BR after it?
nearNode = HTMLEditorRef().GetNextEditableHTMLNode(atStartOfSelection); nearNode = GetNextEditableHTMLNode(atStartOfSelection);
if (!nearNode || !HTMLEditorRef().IsVisibleBRElement(nearNode) || if (!nearNode || !IsVisibleBRElement(nearNode) ||
EditorBase::IsPaddingBRElementForEmptyLastLine(*nearNode)) { EditorBase::IsPaddingBRElementForEmptyLastLine(*nearNode)) {
pointToInsertBR = atStartOfSelection; pointToInsertBR = atStartOfSelection;
splitAfterNewBR = true; splitAfterNewBR = true;
@ -8266,9 +8264,8 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
return EditActionResult(NS_OK); return EditActionResult(NS_OK);
} }
brContent = MOZ_KnownLive(HTMLEditorRef()) brContent = InsertBRElementWithTransaction(pointToInsertBR);
.InsertBRElementWithTransaction(pointToInsertBR); if (NS_WARN_IF(Destroyed())) {
if (NS_WARN_IF(!CanHandleEditAction())) {
return EditActionResult(NS_ERROR_EDITOR_DESTROYED); return EditActionResult(NS_ERROR_EDITOR_DESTROYED);
} }
NS_WARNING_ASSERTION(brContent, "Failed to create a <br> element"); NS_WARNING_ASSERTION(brContent, "Failed to create a <br> element");
@ -8281,8 +8278,7 @@ EditActionResult HTMLEditRules::ReturnInParagraph(Element& aParentDivOrP) {
} }
} }
EditActionResult result( EditActionResult result(
MOZ_KnownLive(HTMLEditorRef()) SplitParagraph(aParentDivOrP, pointToSplitParentDivOrP, brContent));
.SplitParagraph(aParentDivOrP, pointToSplitParentDivOrP, brContent));
result.MarkAsHandled(); result.MarkAsHandled();
if (NS_WARN_IF(result.Failed())) { if (NS_WARN_IF(result.Failed())) {
return result; return result;

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

@ -473,20 +473,6 @@ class HTMLEditRules : public TextEditRules {
MOZ_MUST_USE nsresult ReturnInHeader(Element& aHeader, nsINode& aNode, MOZ_MUST_USE nsresult ReturnInHeader(Element& aHeader, nsINode& aNode,
int32_t aOffset); int32_t aOffset);
/**
* ReturnInParagraph() does the right thing for Enter key press or
* 'insertParagraph' command in aParentDivOrP. aParentDivOrP will be
* split at start of first selection range.
*
* @param aParentDivOrP The parent block. This must be <p> or <div>
* element.
* @return Returns with NS_OK if this doesn't meat any
* unexpected situation. If this method tries to
* split the paragraph, marked as handled.
*/
MOZ_CAN_RUN_SCRIPT
MOZ_MUST_USE EditActionResult ReturnInParagraph(Element& aParentDivOrP);
/** /**
* ReturnInListItem() handles insertParagraph command (i.e., handling * ReturnInListItem() handles insertParagraph command (i.e., handling
* Enter key press) in a list item element. * Enter key press) in a list item element.

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

@ -1798,6 +1798,20 @@ class HTMLEditor final : public TextEditor,
Element& aParentDivOrP, Element& aParentDivOrP,
const EditorDOMPointBase<PT, CT>& aStartOfRightNode, nsIContent* aBRNode); const EditorDOMPointBase<PT, CT>& aStartOfRightNode, nsIContent* aBRNode);
/**
* HandleInsertParagraphInParagraph() does the right thing for Enter key
* press or 'insertParagraph' command in aParentDivOrP. aParentDivOrP will
* be split at start of first selection range.
*
* @param aParentDivOrP The parent block. This must be <p> or <div>
* element.
* @return Returns with NS_OK if this doesn't meat any
* unexpected situation. If this method tries to
* split the paragraph, marked as handled.
*/
MOZ_CAN_RUN_SCRIPT MOZ_MUST_USE EditActionResult
HandleInsertParagraphInParagraph(Element& aParentDivOrP);
protected: // Called by helper classes. protected: // Called by helper classes.
virtual void OnStartToHandleTopLevelEditSubAction( virtual void OnStartToHandleTopLevelEditSubAction(
EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override; EditSubAction aEditSubAction, nsIEditor::EDirection aDirection) override;