From 3410225022e2e75c7de96345c92529dba0410e94 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Mon, 28 Apr 2014 14:54:47 +0300 Subject: [PATCH] Bug 1002429 part 8 - Clean up nsHTMLEditor::CreateElementWithDefaults; r=ehsan --- editor/libeditor/html/nsHTMLEditor.cpp | 83 +++++++++++++------------- editor/libeditor/html/nsHTMLEditor.h | 4 +- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/editor/libeditor/html/nsHTMLEditor.cpp b/editor/libeditor/html/nsHTMLEditor.cpp index 574c9cea1864..759ae21e16fe 100644 --- a/editor/libeditor/html/nsHTMLEditor.cpp +++ b/editor/libeditor/html/nsHTMLEditor.cpp @@ -2542,66 +2542,67 @@ nsHTMLEditor::GetSelectedElement(const nsAString& aTagName, nsIDOMElement** aRet return res; } -NS_IMETHODIMP -nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName, nsIDOMElement** aReturn) +already_AddRefed +nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName) { - nsresult res=NS_ERROR_NOT_INITIALIZED; - if (aReturn) - *aReturn = nullptr; + MOZ_ASSERT(!aTagName.IsEmpty()); -// NS_ENSURE_TRUE(aTagName && aReturn, NS_ERROR_NULL_POINTER); - NS_ENSURE_TRUE(!aTagName.IsEmpty() && aReturn, NS_ERROR_NULL_POINTER); - - nsAutoString TagName(aTagName); - ToLowerCase(TagName); + nsAutoString tagName(aTagName); + ToLowerCase(tagName); nsAutoString realTagName; - if (IsLinkTag(TagName) || IsNamedAnchorTag(TagName)) - { + if (IsLinkTag(tagName) || IsNamedAnchorTag(tagName)) { realTagName.AssignLiteral("a"); } else { - realTagName = TagName; + realTagName = tagName; } - //We don't use editor's CreateElement because we don't want to - // go through the transaction system - - nsCOMPtrnewElement; - nsCOMPtr newContent; - nsCOMPtr doc = do_QueryReferent(mDocWeak); - NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED); + // We don't use editor's CreateElement because we don't want to go through + // the transaction system + // New call to use instead to get proper HTML element, bug 39919 ErrorResult rv; - newContent = CreateHTMLContent(realTagName, rv); - newElement = do_QueryInterface(newContent); + nsCOMPtr newElement = CreateHTMLContent(realTagName, rv); if (rv.Failed() || !newElement) { - return NS_ERROR_FAILURE; + return nullptr; } // Mark the new element dirty, so it will be formatted - newElement->SetAttribute(NS_LITERAL_STRING("_moz_dirty"), EmptyString()); + newElement->SetAttribute(NS_LITERAL_STRING("_moz_dirty"), EmptyString(), rv); // Set default values for new elements - if (TagName.EqualsLiteral("table")) { - res = newElement->SetAttribute(NS_LITERAL_STRING("cellpadding"),NS_LITERAL_STRING("2")); - NS_ENSURE_SUCCESS(res, res); - res = newElement->SetAttribute(NS_LITERAL_STRING("cellspacing"),NS_LITERAL_STRING("2")); - NS_ENSURE_SUCCESS(res, res); - res = newElement->SetAttribute(NS_LITERAL_STRING("border"),NS_LITERAL_STRING("1")); - } else if (TagName.EqualsLiteral("td")) - { - res = SetAttributeOrEquivalent(newElement, NS_LITERAL_STRING("valign"), - NS_LITERAL_STRING("top"), true); + if (tagName.EqualsLiteral("table")) { + newElement->SetAttribute(NS_LITERAL_STRING("cellpadding"), + NS_LITERAL_STRING("2"), rv); + NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr); + newElement->SetAttribute(NS_LITERAL_STRING("cellspacing"), + NS_LITERAL_STRING("2"), rv); + NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr); + newElement->SetAttribute(NS_LITERAL_STRING("border"), + NS_LITERAL_STRING("1"), rv); + NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr); + } else if (tagName.EqualsLiteral("td")) { + nsresult res = SetAttributeOrEquivalent( + static_cast(newElement->AsDOMNode()), + NS_LITERAL_STRING("valign"), NS_LITERAL_STRING("top"), true); + NS_ENSURE_SUCCESS(res, nullptr); } // ADD OTHER TAGS HERE - if (NS_SUCCEEDED(res)) - { - *aReturn = newElement; - // Getters must addref - NS_ADDREF(*aReturn); - } + return newElement.forget(); +} - return res; +NS_IMETHODIMP +nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName, nsIDOMElement** aReturn) +{ + NS_ENSURE_TRUE(!aTagName.IsEmpty() && aReturn, NS_ERROR_NULL_POINTER); + *aReturn = nullptr; + + nsCOMPtr newElement = CreateElementWithDefaults(aTagName); + nsCOMPtr ret = do_QueryInterface(newElement); + NS_ENSURE_TRUE(ret, NS_ERROR_FAILURE); + + ret.forget(aReturn); + return NS_OK; } NS_IMETHODIMP diff --git a/editor/libeditor/html/nsHTMLEditor.h b/editor/libeditor/html/nsHTMLEditor.h index 4705a8524779..b3e43017cc14 100644 --- a/editor/libeditor/html/nsHTMLEditor.h +++ b/editor/libeditor/html/nsHTMLEditor.h @@ -963,7 +963,9 @@ private: nsIContent* aChild, int32_t aIndexInContainer, InsertedOrAppended aInsertedOrAppended); already_AddRefed GetElementOrParentByTagName( - const nsAString& aTagName, nsINode* aNode); + const nsAString& aTagName, nsINode* aNode); + already_AddRefed CreateElementWithDefaults( + const nsAString& aTagName); }; #endif //nsHTMLEditor_h__