Bug 1503473 - part 3: Move inserting a line break code in TextEditor::InsertParagraphSeparatorAsSubAction() to TextEditRules::WillInsertLineBreak() r=m_kato

When TextEditRules::WillDoAction() and HTMLEditRules::WillDoAction() didn't
return error, didn't handle it, and didn't cancel it,
TextEditor::InsertParagraphSeparatorAsSubAction() inserts a line breaker.
However, this case is only when the instance is TextEditRules and
TextEditRules::WillInsertLineBreak() prepares to insert a line break without
any errors.  So, we can move the part into TextEditRules::WillInsertLineBreak()
simply.

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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2018-11-03 11:21:15 +00:00
Родитель 5787ea1b53
Коммит 7c5fc30479
2 изменённых файлов: 68 добавлений и 69 удалений

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

@ -476,7 +476,65 @@ TextEditRules::WillInsertLineBreak(int32_t aMaxLength)
return EditActionIgnored(rv);
}
return EditActionIgnored();
// get the (collapsed) selection location
nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0);
if (NS_WARN_IF(!firstRange)) {
return EditActionIgnored(NS_ERROR_FAILURE);
}
EditorRawDOMPoint pointToInsert(firstRange->StartRef());
if (NS_WARN_IF(!pointToInsert.IsSet())) {
return EditActionIgnored(NS_ERROR_FAILURE);
}
MOZ_ASSERT(pointToInsert.IsSetAndValid());
// Don't put text in places that can't have it.
if (!pointToInsert.IsInTextNode() &&
!TextEditorRef().CanContainTag(*pointToInsert.GetContainer(),
*nsGkAtoms::textTagName)) {
return EditActionIgnored(NS_ERROR_FAILURE);
}
nsCOMPtr<nsIDocument> doc = TextEditorRef().GetDocument();
if (NS_WARN_IF(!doc)) {
return EditActionIgnored(NS_ERROR_NOT_INITIALIZED);
}
// Don't change my selection in sub-transactions.
AutoTransactionsConserveSelection dontChangeMySelection(TextEditorRef());
// Insert a linefeed character.
EditorRawDOMPoint pointAfterInsertedLineBreak;
rv = TextEditorRef().InsertTextWithTransaction(
*doc, NS_LITERAL_STRING("\n"), pointToInsert,
&pointAfterInsertedLineBreak);
if (NS_WARN_IF(!pointAfterInsertedLineBreak.IsSet())) {
return EditActionIgnored(NS_ERROR_FAILURE);
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return EditActionIgnored(rv);
}
// set the selection to the correct location
MOZ_ASSERT(!pointAfterInsertedLineBreak.GetChild(),
"After inserting text into a text node, pointAfterInsertedLineBreak."
"GetChild() should be nullptr");
rv = SelectionRefPtr()->Collapse(pointAfterInsertedLineBreak);
if (NS_WARN_IF(NS_FAILED(rv))) {
return EditActionIgnored(rv);
}
// see if we're at the end of the editor range
EditorRawDOMPoint endPoint(EditorBase::GetEndPoint(*SelectionRefPtr()));
if (endPoint == pointAfterInsertedLineBreak) {
// SetInterlinePosition(true) means we want the caret to stick to the
// content on the "right". We want the caret to stick to whatever is
// past the break. This is because the break is on the same line we
// were on, but the next content will be on the following line.
SelectionRefPtr()->SetInterlinePosition(true, IgnoreErrors());
}
return EditActionHandled();
}
nsresult

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

@ -1104,77 +1104,18 @@ TextEditor::InsertParagraphSeparatorAsSubAction()
subActionInfo.maxLength = mMaxTextLength;
bool cancel, handled;
nsresult rv = rules->WillDoAction(subActionInfo, &cancel, &handled);
if (cancel) {
return rv; // We don't need to call DidDoAction() if canceled.
}
// XXX DidDoAction() does nothing for eInsertParagraphSeparator. However,
// we should call it until we keep using this style. Perhaps, each
// editor method should call necessary method of
// TextEditRules/HTMLEditRules directly.
rv = rules->DidDoAction(subActionInfo, rv);
if (NS_WARN_IF(NS_FAILED(rv))) {
// XXX DidDoAction() won't be called when WillDoAction() returns error.
// Perhaps, we should move the code between WillDoAction() and
// DidDoAction() to a new method and guarantee that DidDoAction() is
// always called after WillDoAction().
return rv;
}
if (!cancel && !handled) {
// get the (collapsed) selection location
nsRange* firstRange = SelectionRefPtr()->GetRangeAt(0);
if (NS_WARN_IF(!firstRange)) {
return NS_ERROR_FAILURE;
}
EditorRawDOMPoint pointToInsert(firstRange->StartRef());
if (NS_WARN_IF(!pointToInsert.IsSet())) {
return NS_ERROR_FAILURE;
}
MOZ_ASSERT(pointToInsert.IsSetAndValid());
// don't put text in places that can't have it
if (!pointToInsert.IsInTextNode() &&
!CanContainTag(*pointToInsert.GetContainer(),
*nsGkAtoms::textTagName)) {
return NS_ERROR_FAILURE;
}
// we need to get the doc
nsCOMPtr<nsIDocument> doc = GetDocument();
if (NS_WARN_IF(!doc)) {
return NS_ERROR_NOT_INITIALIZED;
}
// don't change my selection in subtransactions
AutoTransactionsConserveSelection dontChangeMySelection(*this);
// insert a linefeed character
EditorRawDOMPoint pointAfterInsertedLineBreak;
rv = InsertTextWithTransaction(*doc, NS_LITERAL_STRING("\n"), pointToInsert,
&pointAfterInsertedLineBreak);
if (NS_WARN_IF(!pointAfterInsertedLineBreak.IsSet())) {
rv = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called
}
if (NS_SUCCEEDED(rv)) {
// set the selection to the correct location
MOZ_ASSERT(!pointAfterInsertedLineBreak.GetChild(),
"After inserting text into a text node, pointAfterInsertedLineBreak."
"GetChild() should be nullptr");
rv = SelectionRefPtr()->Collapse(pointAfterInsertedLineBreak);
if (NS_SUCCEEDED(rv)) {
// see if we're at the end of the editor range
EditorRawDOMPoint endPoint(EditorBase::GetEndPoint(*SelectionRefPtr()));
if (endPoint == pointAfterInsertedLineBreak) {
// SetInterlinePosition(true) means we want the caret to stick to the
// content on the "right". We want the caret to stick to whatever is
// past the break. This is because the break is on the same line we
// were on, but the next content will be on the following line.
SelectionRefPtr()->SetInterlinePosition(true, IgnoreErrors());
}
}
}
}
if (!cancel) {
// post-process, always called if WillInsertBreak didn't return cancel==true
rv = rules->DidDoAction(subActionInfo, rv);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"TextEditRules::DidDoAction() failed");
}
return rv;
return NS_OK;
}
nsresult