Bug 761308 - Part b: Cleanup CreateElementTxn::DoTransaction(); r=ehsan

This commit is contained in:
Ms2ger 2012-06-06 09:41:51 +02:00
parent 02dd71b0e0
commit c839dab25b

View File

@ -78,14 +78,11 @@ NS_IMETHODIMP CreateElementTxn::DoTransaction(void)
//new call to use instead to get proper HTML element, bug# 39919
nsresult result = mEditor->CreateHTMLContent(mTag, getter_AddRefs(newContent));
NS_ENSURE_SUCCESS(result, result);
nsCOMPtr<nsIDOMElement>newElement = do_QueryInterface(newContent);
NS_ENSURE_TRUE(newElement, NS_ERROR_NULL_POINTER);
mNewNode = do_QueryInterface(newElement);
NS_ENSURE_STATE(newContent);
mNewNode = newContent->AsDOMNode();
// Try to insert formatting whitespace for the new node:
mEditor->MarkNodeDirty(mNewNode);
NS_ASSERTION(((NS_SUCCEEDED(result)) && (mNewNode)), "could not create element.");
NS_ENSURE_TRUE(mNewNode, NS_ERROR_NULL_POINTER);
#ifdef NS_DEBUG
if (gNoisy)
@ -95,50 +92,43 @@ NS_IMETHODIMP CreateElementTxn::DoTransaction(void)
#endif
// insert the new node
if (CreateElementTxn::eAppend == PRInt32(mOffsetInParent)) {
nsCOMPtr<nsIDOMNode> resultNode;
return mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
}
nsCOMPtr<nsINode> parent = do_QueryInterface(mParent);
NS_ENSURE_STATE(parent);
mOffsetInParent = NS_MIN(mOffsetInParent, parent->GetChildCount());
// note, it's ok for mRefNode to be null. that means append
nsIContent* refNode = parent->GetChildAt(mOffsetInParent);
mRefNode = refNode ? refNode->AsDOMNode() : nsnull;
nsCOMPtr<nsIDOMNode> resultNode;
if (CreateElementTxn::eAppend==(PRInt32)mOffsetInParent)
{
result = mParent->AppendChild(mNewNode, getter_AddRefs(resultNode));
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
NS_ENSURE_SUCCESS(result, result);
// only set selection to insertion point if editor gives permission
bool bAdjustSelection;
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
if (!bAdjustSelection) {
// do nothing - dom range gravity will adjust selection
return NS_OK;
}
else
{
nsCOMPtr<nsIDOMNodeList> childNodes;
result = mParent->GetChildNodes(getter_AddRefs(childNodes));
if ((NS_SUCCEEDED(result)) && (childNodes))
{
PRUint32 count;
childNodes->GetLength(&count);
if (mOffsetInParent>count)
mOffsetInParent = count;
result = childNodes->Item(mOffsetInParent, getter_AddRefs(mRefNode));
NS_ENSURE_SUCCESS(result, result); // note, it's ok for mRefNode to be null. that means append
result = mParent->InsertBefore(mNewNode, mRefNode, getter_AddRefs(resultNode));
NS_ENSURE_SUCCESS(result, result);
nsCOMPtr<nsISelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(result, result);
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
// only set selection to insertion point if editor gives permission
bool bAdjustSelection;
mEditor->ShouldTxnSetSelection(&bAdjustSelection);
if (bAdjustSelection)
{
nsCOMPtr<nsISelection> selection;
result = mEditor->GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(result, result);
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
nsCOMPtr<nsIContent> parentContent = do_QueryInterface(mParent);
NS_ENSURE_STATE(parentContent);
PRInt32 offset=0;
result = nsEditor::GetChildOffset(mNewNode, mParent, offset);
NS_ENSURE_SUCCESS(result, result);
result = selection->Collapse(mParent, offset+1);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
}
else
{
// do nothing - dom range gravity will adjust selection
}
}
}
result = selection->CollapseNative(parentContent,
parentContent->IndexOf(newContent) + 1);
NS_ASSERTION((NS_SUCCEEDED(result)), "selection could not be collapsed after insert.");
return result;
}