From 8146930fd8c1d3af0bf1da7734af04b5e48cc235 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Sun, 2 Nov 2014 14:04:13 +0200 Subject: [PATCH] Bug 1086349 part 4 - Clean up nsEditor::DeleteSelectionAndCreateElement; r=ehsan --- editor/libeditor/nsEditor.cpp | 26 ++++------ editor/libeditor/nsEditor.h | 5 +- editor/libeditor/nsHTMLDataTransfer.cpp | 68 ++++++++++--------------- 3 files changed, 42 insertions(+), 57 deletions(-) diff --git a/editor/libeditor/nsEditor.cpp b/editor/libeditor/nsEditor.cpp index 6e87d773ab90..4a6c66cae85b 100644 --- a/editor/libeditor/nsEditor.cpp +++ b/editor/libeditor/nsEditor.cpp @@ -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 +nsEditor::DeleteSelectionAndCreateElement(nsIAtom& aTag) { - nsCOMPtr tag = do_GetAtom(aTag); - - nsresult result = DeleteSelectionAndPrepareToCreateNode(); - NS_ENSURE_SUCCESS(result, result); + nsresult res = DeleteSelectionAndPrepareToCreateNode(); + NS_ENSURE_SUCCESS(res, nullptr); nsRefPtr selection = GetSelection(); - NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER); + NS_ENSURE_TRUE(selection, nullptr); nsCOMPtr node = selection->GetAnchorNode(); uint32_t offset = selection->AnchorOffset(); - nsCOMPtr 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 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(); } diff --git a/editor/libeditor/nsEditor.h b/editor/libeditor/nsEditor.h index ca1e79b75f68..79fb937d9301 100644 --- a/editor/libeditor/nsEditor.h +++ b/editor/libeditor/nsEditor.h @@ -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 + DeleteSelectionAndCreateElement(nsIAtom& aTag); /* helper routines for node/parent manipulations */ nsresult DeleteNode(nsINode* aNode); diff --git a/editor/libeditor/nsHTMLDataTransfer.cpp b/editor/libeditor/nsHTMLDataTransfer.cpp index 240a4f463ee4..1c23c6c99d4e 100644 --- a/editor/libeditor/nsHTMLDataTransfer.cpp +++ b/editor/libeditor/nsHTMLDataTransfer.cpp @@ -1592,16 +1592,13 @@ NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsAString & aCitation, return NS_OK; // rules canceled the operation } - nsCOMPtr newNode; - rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("blockquote"), getter_AddRefs(newNode)); - NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr newNode = + DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote); NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); // Try to set type=cite. Ignore it if this fails. - nsCOMPtr 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 newNode; // get selection nsRefPtr 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 so it won't be wrapped: - rv = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("span"), getter_AddRefs(newNode)); + nsCOMPtr 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 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 parent = GetNodeLocation(newNode, &offset); + nsCOMPtr 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 newNode; - // get selection nsRefPtr 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 newNode = + DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote); NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER); // Try to set type=cite. Ignore it if this fails. - nsCOMPtr 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 parent = GetNodeLocation(newNode, &offset); + nsCOMPtr parent = newNode->GetParentNode(); + int32_t offset = parent ? parent->IndexOf(newNode) : -1; if (parent) { selection->Collapse(parent, offset + 1); }