Bug 1460509 - part 4: Make TextEditRules::CreateBogusNodeIfNeeded() return NS_ERROR_EDITOR_DESTROYED if it causes destroying the editor r?m_kato
Note that HTMLEditRules::DocumentModifiedWorker() is a runnable method.
So, it cannot return nsresult. Therefore, it just checks the result
only with NS_WARNING_ASSERTION().
MozReview-Commit-ID: KBSpv5H5KGU
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -10307,12 +10307,14 @@ HTMLEditRules::DocumentModifiedWorker()
DebugOnly<nsresult> rv =
HTMLEditorRef().DeleteNodeWithTransaction(*mBogusNode);
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
"Failed to remove the bogus node");
mBogusNode = nullptr;
}
// Try to recreate the bogus node if needed.
- CreateBogusNodeIfNeeded();
+ DebugOnly<nsresult> rv = CreateBogusNodeIfNeeded();
+ NS_WARNING_ASSERTION(rv.value != NS_ERROR_EDITOR_DESTROYED,
+ "The editor has been destroyed during creating a bogus node");
}
} // namespace mozilla
--- a/editor/libeditor/TextEditRules.cpp
+++ b/editor/libeditor/TextEditRules.cpp
@@ -1454,38 +1454,47 @@ TextEditRules::CreateBogusNodeIfNeeded()
// Skip adding the bogus node if body is read-only.
if (!TextEditorRef().IsModifiableNode(rootElement)) {
return NS_OK;
}
// Create a br.
RefPtr<Element> newBrElement =
TextEditorRef().CreateHTMLContent(nsGkAtoms::br);
+ if (NS_WARN_IF(!CanHandleEditAction())) {
+ return NS_ERROR_EDITOR_DESTROYED;
+ }
if (NS_WARN_IF(!newBrElement)) {
return NS_ERROR_FAILURE;
}
// set mBogusNode to be the newly created <br>
mBogusNode = newBrElement;
// Give it a special attribute.
newBrElement->SetAttr(kNameSpaceID_None, kMOZEditorBogusNodeAttrAtom,
kMOZEditorBogusNodeValue, false);
// Put the node in the document.
nsresult rv =
TextEditorRef().InsertNodeWithTransaction(
*mBogusNode, EditorRawDOMPoint(rootElement, 0));
+ if (NS_WARN_IF(!CanHandleEditAction())) {
+ return NS_ERROR_EDITOR_DESTROYED;
+ }
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// Set selection.
IgnoredErrorResult error;
SelectionRef().Collapse(EditorRawDOMPoint(rootElement, 0), error);
+ if (NS_WARN_IF(!CanHandleEditAction())) {
+ return NS_ERROR_EDITOR_DESTROYED;
+ }
NS_WARNING_ASSERTION(!error.Failed(),
"Failed to collapse selection at start of the root element");
return NS_OK;
}
nsresult
TextEditRules::TruncateInsertionIfNeeded(const nsAString* aInString,
--- a/editor/libeditor/TextEditRules.h
+++ b/editor/libeditor/TextEditRules.h
@@ -182,19 +182,19 @@ protected:
nsresult RemoveRedundantTrailingBR();
/**
* Creates a trailing break in the text doc if there is not one already.
*/
nsresult CreateTrailingBRIfNeeded();
/**
- * Creates a bogus text node if the document has no editable content.
+ * Creates a bogus <br> node if the root element has no editable content.
*/
- nsresult CreateBogusNodeIfNeeded();
+ MOZ_MUST_USE nsresult CreateBogusNodeIfNeeded();
/**
* Returns a truncated insertion string if insertion would place us over
* aMaxLength
*/
nsresult TruncateInsertionIfNeeded(const nsAString* aInString,
nsAString* aOutString,
int32_t aMaxLength,