Bug 1356496 - Don't use nsIDOM* in ConfirmSelectionInBody. r?masayuki
ConfirmSelectionInBody uses a lot of QI and TextEditUtils::IsBody call to check body element. So we shouldn't use nsIDOM* to avoid QI and EditorBase::GetTag().
Also, if did not found body element, it calls collapse to set caret. If collapse is successful, we might not have to check end selection... How do you think?
MozReview-Commit-ID: F7FhPrlEpCc
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -8072,71 +8072,72 @@ HTMLEditRules::RemoveListStructure(Eleme
return NS_OK;
}
nsresult
HTMLEditRules::ConfirmSelectionInBody()
{
// get the body
NS_ENSURE_STATE(mHTMLEditor);
- nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(mHTMLEditor->GetRoot());
- NS_ENSURE_TRUE(rootElement, NS_ERROR_UNEXPECTED);
+ RefPtr<Element> rootElement = mHTMLEditor->GetRoot();
+ if (NS_WARN_IF(!rootElement)) {
+ return NS_ERROR_UNEXPECTED;
+ }
// get the selection
NS_ENSURE_STATE(mHTMLEditor);
RefPtr<Selection> selection = mHTMLEditor->GetSelection();
- NS_ENSURE_STATE(selection);
+ if (NS_WARN_IF(!selection)) {
+ return NS_ERROR_UNEXPECTED;
+ }
// get the selection start location
- nsCOMPtr<nsIDOMNode> selNode, temp, parent;
+ nsCOMPtr<nsINode> selNode;
int32_t selOffset;
nsresult rv =
EditorBase::GetStartNodeAndOffset(selection,
getter_AddRefs(selNode), &selOffset);
if (NS_FAILED(rv)) {
return rv;
}
- temp = selNode;
+ nsINode* temp = selNode;
// check that selNode is inside body
- while (temp && !TextEditUtils::IsBody(temp)) {
- temp->GetParentNode(getter_AddRefs(parent));
- temp = parent;
+ while (temp && !temp->IsHTMLElement(nsGkAtoms::body)) {
+ temp = temp->GetParentNode();
}
// if we aren't in the body, force the issue
if (!temp) {
// uncomment this to see when we get bad selections
// NS_NOTREACHED("selection not in body");
selection->Collapse(rootElement, 0);
+ // XXX selection is collapsed, so should we check selection end?
}
// get the selection end location
rv = EditorBase::GetEndNodeAndOffset(selection,
getter_AddRefs(selNode), &selOffset);
NS_ENSURE_SUCCESS(rv, rv);
temp = selNode;
// check that selNode is inside body
- while (temp && !TextEditUtils::IsBody(temp)) {
- rv = temp->GetParentNode(getter_AddRefs(parent));
- temp = parent;
+ while (temp && !temp->IsHTMLElement(nsGkAtoms::body)) {
+ temp = temp->GetParentNode();
}
// if we aren't in the body, force the issue
if (!temp) {
// uncomment this to see when we get bad selections
// NS_NOTREACHED("selection not in body");
selection->Collapse(rootElement, 0);
}
- // XXX This is the result of the last call of GetParentNode(), it doesn't
- // make sense...
- return rv;
+ return NS_OK;
}
nsresult
HTMLEditRules::UpdateDocChangeRange(nsRange* aRange)
{
// first make sure aRange is in the document. It might not be if
// portions of our editting action involved manipulating nodes
// prior to placing them in the document (e.g., populating a list item