Bug 1460509 - part 80: Make HTMLEditRules::DeleteNodeIfCollapsedText() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r=m_kato

MozReview-Commit-ID: C8ZNGlKJXlq

--HG--
extra : rebase_source : 17bd0b929b6339c2736514c28a354b21c45143b6
This commit is contained in:
Masayuki Nakano 2018-05-18 00:31:20 +09:00
Родитель 7e3a011a75
Коммит c7127dda7f
2 изменённых файлов: 39 добавлений и 18 удалений

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

@ -2647,7 +2647,11 @@ HTMLEditRules::WillDeleteSelection(nsIEditor::EDirection aAction,
// non-empty text node. For now, we should keep our traditional
// behavior same as Chromium for backward compatibility.
DeleteNodeIfCollapsedText(nodeAsText);
rv = DeleteNodeIfCollapsedText(nodeAsText);
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to remove collapsed text");
rv = InsertBRIfNeeded();
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -3198,8 +3202,18 @@ HTMLEditRules::WillDeleteSelection(nsIEditor::EDirection aAction,
AutoTrackDOMPoint endTracker(HTMLEditorRef().mRangeUpdater,
address_of(endNode), &endOffset);
DeleteNodeIfCollapsedText(*startNode);
DeleteNodeIfCollapsedText(*endNode);
nsresult rv = DeleteNodeIfCollapsedText(*startNode);
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to delete start node even though it's collapsed text");
rv = DeleteNodeIfCollapsedText(*endNode);
if (NS_WARN_IF(rv == NS_ERROR_EDITOR_DESTROYED)) {
return NS_ERROR_EDITOR_DESTROYED;
}
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to delete end node even though it's collapsed text");
}
// If we're joining blocks: if deleting forward the selection should be
@ -3224,31 +3238,30 @@ HTMLEditRules::WillDeleteSelection(nsIEditor::EDirection aAction,
return NS_OK;
}
/**
* If aNode is a text node that contains only collapsed whitespace, delete it.
* It doesn't serve any useful purpose, and we don't want it to confuse code
* that doesn't correctly skip over it.
*
* If deleting the node fails (like if it's not editable), the caller should
* proceed as usual, so don't return any errors.
*/
void
nsresult
HTMLEditRules::DeleteNodeIfCollapsedText(nsINode& aNode)
{
MOZ_ASSERT(IsEditorDataAvailable());
Text* text = aNode.GetAsText();
if (!text) {
return;
return NS_OK;
}
if (!HTMLEditorRef().IsVisibleTextNode(*text)) {
DebugOnly<nsresult> rv = HTMLEditorRef().DeleteNodeWithTransaction(aNode);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "Failed to remove aNode");
if (HTMLEditorRef().IsVisibleTextNode(*text)) {
return NS_OK;
}
nsresult rv = HTMLEditorRef().DeleteNodeWithTransaction(aNode);
if (NS_WARN_IF(!CanHandleEditAction())) {
return NS_ERROR_EDITOR_DESTROYED;
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
/**
* InsertBRIfNeeded() determines if a br is needed for current selection to not
* be spastic. If so, it inserts one. Callers responsibility to only call

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

@ -195,7 +195,15 @@ protected:
*/
nsresult WillInsertBreak(bool* aCancel, bool* aHandled);
void DeleteNodeIfCollapsedText(nsINode& aNode);
/**
* If aNode is a text node that contains only collapsed whitespace, delete
* it. It doesn't serve any useful purpose, and we don't want it to confuse
* code that doesn't correctly skip over it.
*
* If deleting the node fails (like if it's not editable), the caller should
* proceed as usual, so don't return any errors.
*/
MOZ_MUST_USE nsresult DeleteNodeIfCollapsedText(nsINode& aNode);
/**
* InsertBRElement() inserts a <br> element into aInsertToBreak.