From 2346a4667eaff2ea84da22dc79a2b71128080510 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Wed, 1 Feb 2012 11:54:22 +0100 Subject: [PATCH] Bug 721483 - Clean up RemoveElementIfNoStyleOrIdOrClass / HasStyleOrIdOrClass; r=ehsan --- editor/libeditor/base/nsEditor.cpp | 7 +++ editor/libeditor/base/nsEditor.h | 1 + editor/libeditor/html/nsHTMLAbsPosition.cpp | 8 +-- editor/libeditor/html/nsHTMLCSSUtils.cpp | 20 ------- editor/libeditor/html/nsHTMLCSSUtils.h | 7 --- editor/libeditor/html/nsHTMLEditor.h | 4 +- editor/libeditor/html/nsHTMLEditorStyle.cpp | 60 ++++++++++----------- 7 files changed, 43 insertions(+), 64 deletions(-) diff --git a/editor/libeditor/base/nsEditor.cpp b/editor/libeditor/base/nsEditor.cpp index ae368b0dee9b..0d6be3c058a4 100644 --- a/editor/libeditor/base/nsEditor.cpp +++ b/editor/libeditor/base/nsEditor.cpp @@ -1617,6 +1617,13 @@ nsEditor::ReplaceContainer(nsIDOMNode *inNode, // RemoveContainer: remove inNode, reparenting its children into their // the parent of inNode // +nsresult +nsEditor::RemoveContainer(nsINode* aNode) +{ + nsCOMPtr node = do_QueryInterface(aNode); + return RemoveContainer(node); +} + nsresult nsEditor::RemoveContainer(nsIDOMNode *inNode) { diff --git a/editor/libeditor/base/nsEditor.h b/editor/libeditor/base/nsEditor.h index d384d4c97ad2..f1c560b99b74 100644 --- a/editor/libeditor/base/nsEditor.h +++ b/editor/libeditor/base/nsEditor.h @@ -191,6 +191,7 @@ public: const nsAString *aValue = nsnull, bool aCloneAttributes = false); + nsresult RemoveContainer(nsINode* aNode); nsresult RemoveContainer(nsIDOMNode *inNode); nsresult InsertContainerAbove(nsIDOMNode *inNode, nsCOMPtr *outNode, diff --git a/editor/libeditor/html/nsHTMLAbsPosition.cpp b/editor/libeditor/html/nsHTMLAbsPosition.cpp index 85c3923ff811..e1b9c15c91d1 100644 --- a/editor/libeditor/html/nsHTMLAbsPosition.cpp +++ b/editor/libeditor/html/nsHTMLAbsPosition.cpp @@ -62,6 +62,7 @@ #include "nsIDOMRGBColor.h" #include "mozilla/Preferences.h" +#include "mozilla/dom/Element.h" using namespace mozilla; @@ -561,6 +562,7 @@ nsHTMLEditor::AbsolutelyPositionElement(nsIDOMElement * aElement, } } else { + res = NS_OK; mHTMLCSSUtils->RemoveCSSProperty(aElement, nsEditProperty::cssPosition, EmptyString(), false); @@ -583,10 +585,8 @@ nsHTMLEditor::AbsolutelyPositionElement(nsIDOMElement * aElement, EmptyString(), false); } - bool hasStyleOrIdOrClass; - res = HasStyleOrIdOrClass(aElement, &hasStyleOrIdOrClass); - NS_ENSURE_SUCCESS(res, res); - if (!hasStyleOrIdOrClass && nsHTMLEditUtils::IsDiv(aElement)) { + nsCOMPtr element = do_QueryInterface(aElement); + if (element && element->IsHTML(nsGkAtoms::div) && !HasStyleOrIdOrClass(element)) { nsHTMLEditRules* htmlRules = static_cast(mRules.get()); NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE); res = htmlRules->MakeSureElemStartsOrEndsOnCR(aElement); diff --git a/editor/libeditor/html/nsHTMLCSSUtils.cpp b/editor/libeditor/html/nsHTMLCSSUtils.cpp index 4cbe2330987a..5698f573a007 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.cpp +++ b/editor/libeditor/html/nsHTMLCSSUtils.cpp @@ -1001,26 +1001,6 @@ nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, return NS_OK; } -// aReturn is true if the element aElement carries an ID or a class. -nsresult -nsHTMLCSSUtils::HasClassOrID(nsIDOMElement * aElement, bool & aReturn) -{ - nsAutoString classVal, idVal; - bool isClassSet, isIdSet; - aReturn = false; - - nsresult res = mHTMLEditor->GetAttributeValue(aElement, NS_LITERAL_STRING("class"), classVal, &isClassSet); - NS_ENSURE_SUCCESS(res, res); - res = mHTMLEditor->GetAttributeValue(aElement, NS_LITERAL_STRING("id"), idVal, &isIdSet); - NS_ENSURE_SUCCESS(res, res); - - // we need to make sure that if the element has an id or a class attribute, - // the attribute is not the empty string - aReturn = ((isClassSet && !classVal.IsEmpty()) || - (isIdSet && !idVal.IsEmpty())); - return NS_OK; -} - // returns in aValueString the list of values for the CSS equivalences to // the HTML style aHTMLProperty/aAttribute/aValueString for the node aNode; // the value of aStyleType controls the styles we retrieve : specified or diff --git a/editor/libeditor/html/nsHTMLCSSUtils.h b/editor/libeditor/html/nsHTMLCSSUtils.h index 48b4a16dd8d5..a32cc99c2676 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.h +++ b/editor/libeditor/html/nsHTMLCSSUtils.h @@ -183,13 +183,6 @@ public: */ void GetDefaultLengthUnit(nsAString & aLengthUnit); - /** asnwers true if the element aElement carries an ID or a class - * - * @param aElement [IN] a DOM element - * @param aReturn [OUT] the boolean answer - */ - nsresult HasClassOrID(nsIDOMElement * aElement, bool & aReturn); - /** returns the list of values for the CSS equivalences to * the passed HTML style for the passed node * diff --git a/editor/libeditor/html/nsHTMLEditor.h b/editor/libeditor/html/nsHTMLEditor.h index bd3078e587b1..cd140948a3aa 100644 --- a/editor/libeditor/html/nsHTMLEditor.h +++ b/editor/libeditor/html/nsHTMLEditor.h @@ -722,8 +722,8 @@ protected: bool *aAll, nsAString *outValue, bool aCheckDefaults = true); - nsresult HasStyleOrIdOrClass(nsIDOMElement * aElement, bool *aHasStyleOrIdOrClass); - nsresult RemoveElementIfNoStyleOrIdOrClass(nsIDOMElement * aElement, nsIAtom * aTag); + bool HasStyleOrIdOrClass(mozilla::dom::Element* aElement); + nsresult RemoveElementIfNoStyleOrIdOrClass(nsIDOMNode* aElement); // Whether the outer window of the DOM event target has focus or not. bool OurWindowHasFocus(); diff --git a/editor/libeditor/html/nsHTMLEditorStyle.cpp b/editor/libeditor/html/nsHTMLEditorStyle.cpp index eb20b8b22465..b329843b7270 100644 --- a/editor/libeditor/html/nsHTMLEditorStyle.cpp +++ b/editor/libeditor/html/nsHTMLEditorStyle.cpp @@ -57,6 +57,9 @@ #include "nsIContentIterator.h" #include "nsAttrName.h" +#include "mozilla/dom/Element.h" + +using namespace mozilla; NS_IMETHODIMP nsHTMLEditor::AddDefaultProperty(nsIAtom *aProperty, const nsAString & aAttribute, @@ -635,9 +638,9 @@ nsresult nsHTMLEditor::ApplyDefaultProperties() } nsresult nsHTMLEditor::RemoveStyleInside(nsIDOMNode *aNode, - nsIAtom *aProperty, // null here means remove all properties - const nsAString *aAttribute, - bool aChildrenOnly) + nsIAtom *aProperty, // null here means remove all properties + const nsAString *aAttribute, + bool aChildrenOnly) { NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER); if (IsTextNode(aNode)) return NS_OK; @@ -695,8 +698,7 @@ nsresult nsHTMLEditor::RemoveStyleInside(nsIDOMNode *aNode, &propertyValue, false); // remove the span if it's useless - nsCOMPtr element = do_QueryInterface(spanNode); - res = RemoveElementIfNoStyleOrIdOrClass(element, nsEditProperty::span); + RemoveElementIfNoStyleOrIdOrClass(spanNode); } } res = RemoveContainer(aNode); @@ -741,8 +743,8 @@ nsresult nsHTMLEditor::RemoveStyleInside(nsIDOMNode *aNode, false); // remove the node if it is a span, if its style attribute is empty or absent, // and if it does not have a class nor an id - nsCOMPtr element = do_QueryInterface(aNode); - res = RemoveElementIfNoStyleOrIdOrClass(element, nsEditProperty::span); + RemoveElementIfNoStyleOrIdOrClass(aNode); + res = NS_OK; } } } @@ -1890,41 +1892,37 @@ nsHTMLEditor::GetIsCSSEnabled(bool *aIsCSSEnabled) return NS_OK; } -nsresult -nsHTMLEditor::HasStyleOrIdOrClass(nsIDOMElement * aElement, bool *aHasStyleOrIdOrClass) +static bool +HasNonEmptyAttribute(dom::Element* aElement, nsIAtom* aName) { - NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER); - nsCOMPtr node = do_QueryInterface(aElement); + MOZ_ASSERT(aElement); + nsAutoString value; + return aElement->GetAttr(kNameSpaceID_None, aName, value) && !value.IsEmpty(); +} + +bool +nsHTMLEditor::HasStyleOrIdOrClass(dom::Element* aElement) +{ + MOZ_ASSERT(aElement); // remove the node if its style attribute is empty or absent, // and if it does not have a class nor an id - nsAutoString styleVal; - bool isStyleSet; - *aHasStyleOrIdOrClass = true; - nsresult res = GetAttributeValue(aElement, NS_LITERAL_STRING("style"), styleVal, &isStyleSet); - NS_ENSURE_SUCCESS(res, res); - if (!isStyleSet || styleVal.IsEmpty()) { - res = mHTMLCSSUtils->HasClassOrID(aElement, *aHasStyleOrIdOrClass); - NS_ENSURE_SUCCESS(res, res); - } - return res; + return HasNonEmptyAttribute(aElement, nsGkAtoms::style) || + HasNonEmptyAttribute(aElement, nsGkAtoms::_class) || + HasNonEmptyAttribute(aElement, nsGkAtoms::id); } nsresult -nsHTMLEditor::RemoveElementIfNoStyleOrIdOrClass(nsIDOMElement * aElement, nsIAtom * aTag) +nsHTMLEditor::RemoveElementIfNoStyleOrIdOrClass(nsIDOMNode* aElement) { - NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER); - nsCOMPtr node = do_QueryInterface(aElement); + nsCOMPtr element = do_QueryInterface(aElement); + NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER); // early way out if node is not the right kind of element - if (!NodeIsType(node, aTag)) { + if (!element->IsHTML(nsGkAtoms::span) || HasStyleOrIdOrClass(element)) { return NS_OK; } - bool hasStyleOrIdOrClass; - nsresult res = HasStyleOrIdOrClass(aElement, &hasStyleOrIdOrClass); - if (!hasStyleOrIdOrClass) { - res = RemoveContainer(node); - } - return res; + + return RemoveContainer(element); }