Bug 1086349 part 4 - Clean up nsEditor::DeleteSelectionAndCreateElement; r=ehsan

This commit is contained in:
Aryeh Gregor 2014-11-02 14:04:13 +02:00
parent 2420067448
commit 8146930fd8
3 changed files with 42 additions and 57 deletions

View File

@ -4065,29 +4065,25 @@ nsEditor::DeleteSelectionImpl(EDirection aAction,
return res;
}
// XXX: error handling in this routine needs to be cleaned up!
NS_IMETHODIMP
nsEditor::DeleteSelectionAndCreateNode(const nsAString& aTag,
nsIDOMNode ** aNewNode)
already_AddRefed<Element>
nsEditor::DeleteSelectionAndCreateElement(nsIAtom& aTag)
{
nsCOMPtr<nsIAtom> tag = do_GetAtom(aTag);
nsresult result = DeleteSelectionAndPrepareToCreateNode();
NS_ENSURE_SUCCESS(result, result);
nsresult res = DeleteSelectionAndPrepareToCreateNode();
NS_ENSURE_SUCCESS(res, nullptr);
nsRefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(selection, nullptr);
nsCOMPtr<nsINode> node = selection->GetAnchorNode();
uint32_t offset = selection->AnchorOffset();
nsCOMPtr<nsIDOMNode> newNode;
*aNewNode = GetAsDOMNode(CreateNode(tag, node, offset).take());
// XXX: ERROR_HANDLING check result, and make sure aNewNode is set correctly
// in success/failure cases
nsCOMPtr<Element> newElement = CreateNode(&aTag, node, offset);
// we want the selection to be just after the new node
return selection->Collapse(node, offset + 1);
// We want the selection to be just after the new node
res = selection->Collapse(node, offset + 1);
NS_ENSURE_SUCCESS(res, nullptr);
return newElement.forget();
}

View File

@ -212,8 +212,9 @@ public:
bool aSuppressIME = false);
NS_IMETHOD DeleteSelectionImpl(EDirection aAction,
EStripWrappers aStripWrappers);
NS_IMETHOD DeleteSelectionAndCreateNode(const nsAString& aTag,
nsIDOMNode ** aNewNode);
already_AddRefed<mozilla::dom::Element>
DeleteSelectionAndCreateElement(nsIAtom& aTag);
/* helper routines for node/parent manipulations */
nsresult DeleteNode(nsINode* aNode);

View File

@ -1592,16 +1592,13 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsAString & aCitation,
return NS_OK; // rules canceled the operation
}
nsCOMPtr<nsIDOMNode> newNode;
rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<Element> newNode =
DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote);
NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER);
// Try to set type=cite. Ignore it if this fails.
nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(newNode);
if (newElement) {
newElement->SetAttribute(NS_LITERAL_STRING("type"), NS_LITERAL_STRING("cite"));
}
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("cite"), true);
// Set the selection to the underneath the node we just inserted:
rv = selection->Collapse(newNode, 0);
@ -1771,7 +1768,6 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText,
if (mWrapToWindow)
return nsPlaintextEditor::InsertAsQuotation(aQuotedText, aNodeInserted);
nsCOMPtr<nsIDOMNode> newNode;
// get selection
nsRefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@ -1791,25 +1787,22 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText,
}
// Wrap the inserted quote in a <span> so it won't be wrapped:
rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("span"), getter_AddRefs(newNode));
nsCOMPtr<Element> newNode =
DeleteSelectionAndCreateElement(*nsGkAtoms::span);
// If this succeeded, then set selection inside the pre
// so the inserted text will end up there.
// If it failed, we don't care what the return value was,
// but we'll fall through and try to insert the text anyway.
if (NS_SUCCEEDED(rv) && newNode)
{
if (newNode) {
// Add an attribute on the pre node so we'll know it's a quotation.
// Do this after the insertion, so that
nsCOMPtr<nsIDOMElement> preElement = do_QueryInterface(newNode);
if (preElement)
{
preElement->SetAttribute(NS_LITERAL_STRING("_moz_quote"),
NS_LITERAL_STRING("true"));
// turn off wrapping on spans
preElement->SetAttribute(NS_LITERAL_STRING("style"),
NS_LITERAL_STRING("white-space: pre;"));
}
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::mozquote,
NS_LITERAL_STRING("true"), true);
// turn off wrapping on spans
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
NS_LITERAL_STRING("white-space: pre;"), true);
// and set the selection inside it:
selection->Collapse(newNode, 0);
}
@ -1824,15 +1817,15 @@ nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText,
if (aNodeInserted && NS_SUCCEEDED(rv))
{
*aNodeInserted = newNode;
*aNodeInserted = GetAsDOMNode(newNode);
NS_IF_ADDREF(*aNodeInserted);
}
// Set the selection to just after the inserted node:
if (NS_SUCCEEDED(rv) && newNode)
{
int32_t offset;
nsCOMPtr<nsIDOMNode> parent = GetNodeLocation(newNode, &offset);
nsCOMPtr<nsINode> parent = newNode->GetParentNode();
int32_t offset = parent ? parent->IndexOf(newNode) : -1;
if (parent) {
selection->Collapse(parent, offset + 1);
}
@ -1865,8 +1858,6 @@ nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText,
return InsertAsPlaintextQuotation(aQuotedText, true, aNodeInserted);
}
nsCOMPtr<nsIDOMNode> newNode;
// get selection
nsRefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@ -1885,24 +1876,21 @@ nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText,
return NS_OK; // rules canceled the operation
}
rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<Element> newNode =
DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote);
NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER);
// Try to set type=cite. Ignore it if this fails.
nsCOMPtr<nsIDOMElement> newElement = do_QueryInterface(newNode);
if (newElement)
{
NS_NAMED_LITERAL_STRING(citeStr, "cite");
newElement->SetAttribute(NS_LITERAL_STRING("type"), citeStr);
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
NS_LITERAL_STRING("cite"), true);
if (!aCitation.IsEmpty())
newElement->SetAttribute(citeStr, aCitation);
// Set the selection inside the blockquote so aQuotedText will go there:
selection->Collapse(newNode, 0);
if (!aCitation.IsEmpty()) {
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::cite, aCitation, true);
}
// Set the selection inside the blockquote so aQuotedText will go there:
selection->Collapse(newNode, 0);
if (aInsertHTML)
rv = LoadHTML(aQuotedText);
else
@ -1910,15 +1898,15 @@ nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText,
if (aNodeInserted && NS_SUCCEEDED(rv))
{
*aNodeInserted = newNode;
*aNodeInserted = GetAsDOMNode(newNode);
NS_IF_ADDREF(*aNodeInserted);
}
// Set the selection to just after the inserted node:
if (NS_SUCCEEDED(rv) && newNode)
{
int32_t offset;
nsCOMPtr<nsIDOMNode> parent = GetNodeLocation(newNode, &offset);
nsCOMPtr<nsINode> parent = newNode->GetParentNode();
int32_t offset = parent ? parent->IndexOf(newNode) : -1;
if (parent) {
selection->Collapse(parent, offset + 1);
}