From 09622112c8e8349cf6d4552bda87e848c47a19c0 Mon Sep 17 00:00:00 2001 From: "brade%comcast.net" Date: Wed, 3 Sep 2003 13:54:36 +0000 Subject: [PATCH] add helper methods for dealing with pixels; reduce string usage; code cleanup (r=glazman, sr=tor, a=asa, bug=216045) --- editor/libeditor/html/nsHTMLCSSUtils.cpp | 86 ++++++++++++++---------- editor/libeditor/html/nsHTMLCSSUtils.h | 5 ++ 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/editor/libeditor/html/nsHTMLCSSUtils.cpp b/editor/libeditor/html/nsHTMLCSSUtils.cpp index 7a2c9e803d0..19039bbf5d1 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.cpp +++ b/editor/libeditor/html/nsHTMLCSSUtils.cpp @@ -63,13 +63,8 @@ void ProcessBValue(const nsAString * aInputString, nsAString & aOutputString, const char * aDefaultValueString, const char * aPrependString, const char* aAppendString) { - if (aInputString) { - if (aInputString->Equals(NS_LITERAL_STRING("-moz-editor-invert-value"))) { + if (aInputString && aInputString->Equals(NS_LITERAL_STRING("-moz-editor-invert-value"))) { aOutputString.Assign(NS_LITERAL_STRING("normal")); - } - else { - aOutputString.Assign(NS_LITERAL_STRING("bold")); - } } else { aOutputString.Assign(NS_LITERAL_STRING("bold")); @@ -81,10 +76,11 @@ void ProcessDefaultValue(const nsAString * aInputString, nsAString & aOutputStri const char * aDefaultValueString, const char * aPrependString, const char* aAppendString) { - aOutputString.Truncate(); if (aDefaultValueString) { aOutputString.Assign(NS_ConvertASCIItoUCS2(aDefaultValueString)); } + else + aOutputString.Truncate(); } static @@ -92,10 +88,11 @@ void ProcessSameValue(const nsAString * aInputString, nsAString & aOutputString, const char * aDefaultValueString, const char * aPrependString, const char* aAppendString) { - aOutputString.Truncate(); if (aInputString) { aOutputString.Assign(*aInputString); } + else + aOutputString.Truncate(); } static @@ -348,6 +345,8 @@ nsHTMLCSSUtils::IsCSSEditableProperty(nsIDOMNode * aNode, nsCOMPtr tagName; content->GetTag(getter_AddRefs(tagName)); + // brade: should the above use nsEditor::GetTag(aNode)? + // brade: shouldn't some of the above go below the next block? // html inline styles B I TT U STRIKE and COLOR/FACE on FONT if (nsEditProperty::b == aProperty @@ -375,6 +374,8 @@ nsHTMLCSSUtils::IsCSSEditableProperty(nsIDOMNode * aNode, || nsEditProperty::th == tagName || nsEditProperty::table == tagName || nsEditProperty::hr == tagName + // brade: for the above, why not use nsHTMLEditUtils::SupportsAlignAttr + // brade: but it also checks for tbody, tfoot, thead // Let's add the following elements here even if ALIGN has not // the same meaning for them || nsEditProperty::legend == tagName @@ -484,6 +485,18 @@ nsHTMLCSSUtils::SetCSSProperty(nsIDOMElement *aElement, nsIAtom * aProperty, con return result; } +nsresult +nsHTMLCSSUtils::SetCSSPropertyPixels(nsIDOMElement *aElement, + nsIAtom *aProperty, + PRInt32 aIntValue, + PRBool aSuppressTransaction) +{ + nsAutoString s; + s.AppendInt(aIntValue); + return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px"), + aSuppressTransaction); +} + // the lowest level above the transaction; removes the value aValue from the list of values // specified for the CSS property aProperty, or totally remove the declaration if this // property accepts only one value @@ -504,7 +517,6 @@ nsHTMLCSSUtils::RemoveCSSProperty(nsIDOMElement *aElement, nsIAtom * aProperty, // The transaction system (if any) has taken ownwership of txn NS_IF_RELEASE(txn); return result; - return NS_OK; } nsresult @@ -515,7 +527,7 @@ nsHTMLCSSUtils::CreateCSSPropertyTxn(nsIDOMElement *aElement, PRBool aRemoveProperty) { nsresult result = NS_ERROR_NULL_POINTER; - if (nsnull != aElement) + if (aElement) { result = TransactionFactory::GetNewTransaction(ChangeCSSInlineStyleTxn::GetCID(), (EditTxn **)aTxn); if (NS_SUCCEEDED(result)) { @@ -574,7 +586,7 @@ nsHTMLCSSUtils::GetCSSInlinePropertyBase(nsIDOMNode *aNode, nsIAtom *aProperty, case SPECIFIED_STYLE_TYPE: if (element) { nsCOMPtr cssDecl; - PRUint32 length = 0; + PRUint32 length; res = GetInlineStyles(element, getter_AddRefs(cssDecl), &length); if (NS_FAILED(res)) return res; nsAutoString value, propString; @@ -603,10 +615,9 @@ nsHTMLCSSUtils::GetDefaultViewCSS(nsIDOMNode *aNode, nsIDOMViewCSS **aViewCSS) res = node->GetOwnerDocument(getter_AddRefs(doc)); if (NS_FAILED(res)) return res; if (doc) { - nsCOMPtr documentView; + nsCOMPtr documentView = do_QueryInterface(doc); nsCOMPtr abstractView; // from the document, get the abtractView - documentView = do_QueryInterface(doc); res = documentView->GetDefaultView(getter_AddRefs(abstractView)); if (NS_FAILED(res)) return res; // from the abstractView, get the CSS view @@ -626,9 +637,8 @@ NS_NewHTMLCSSUtils(nsHTMLCSSUtils** aInstancePtrResult) *aInstancePtrResult = rules; return NS_OK; } - else { - *aInstancePtrResult = nsnull; - } + + *aInstancePtrResult = nsnull; return NS_ERROR_OUT_OF_MEMORY; } @@ -909,6 +919,7 @@ nsHTMLCSSUtils::GenerateCSSDeclarationsFromHTMLStyle(nsIDOMNode * aNode, } if (!node) return; + // brade: use nsEditor::GetTag instead?? nsCOMPtr content = do_QueryInterface(node); nsCOMPtr tagName; content->GetTag(getter_AddRefs(tagName)); @@ -1051,7 +1062,6 @@ nsHTMLCSSUtils::RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, for (index = 0; index < count; index++) { nsAutoString valueString; cssValueArray.StringAt(index, valueString); - nsCOMPtr theElement = do_QueryInterface(aNode); res = RemoveCSSProperty(theElement, (nsIAtom *)cssPropertyArray.ElementAt(index), valueString, aSuppressTransaction); if (NS_FAILED(res)) return res; @@ -1091,32 +1101,31 @@ nsHTMLCSSUtils::GetCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, nsAString & aValueString, PRUint8 aStyleType) { + aValueString.Truncate(); nsCOMPtr theElement; nsresult res = GetElementContainerOrSelf(aNode, getter_AddRefs(theElement)); if (NS_FAILED(res)) return res; - nsCOMPtr theNode = do_QueryInterface(theElement); - aValueString.Truncate(); - if (theElement && IsCSSEditableProperty(theNode, aHTMLProperty, aAttribute)) { + if (theElement && IsCSSEditableProperty(theElement, aHTMLProperty, aAttribute)) { // Yes, the requested HTML style has a CSS equivalence in this implementation // Retrieve the default ViewCSS if we are asked for computed styles nsCOMPtr viewCSS = nsnull; if (COMPUTED_STYLE_TYPE == aStyleType) { - res = GetDefaultViewCSS(theNode, getter_AddRefs(viewCSS)); + res = GetDefaultViewCSS(theElement, getter_AddRefs(viewCSS)); if (NS_FAILED(res)) return res; } nsVoidArray cssPropertyArray; nsStringArray cssValueArray; // get the CSS equivalence with last param PR_TRUE indicating we want only the // "gettable" properties - GenerateCSSDeclarationsFromHTMLStyle(theNode, aHTMLProperty, aAttribute, nsnull, + GenerateCSSDeclarationsFromHTMLStyle(theElement, aHTMLProperty, aAttribute, nsnull, cssPropertyArray, cssValueArray, PR_TRUE); PRInt32 count = cssPropertyArray.Count(); PRInt32 index; for (index = 0; index < count; index++) { nsAutoString valueString; // retrieve the specified/computed value of the property - res = GetCSSInlinePropertyBase(theNode, (nsIAtom *)cssPropertyArray.ElementAt(index), + res = GetCSSInlinePropertyBase(theElement, (nsIAtom *)cssPropertyArray.ElementAt(index), valueString, viewCSS, aStyleType); if (NS_FAILED(res)) return res; // append the value to aValueString (possibly with a leading whitespace) @@ -1145,6 +1154,7 @@ nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, nsAutoString htmlValueString(valueString); aIsSet = PR_FALSE; nsCOMPtr node = aNode; + NS_NAMED_LITERAL_STRING(boldStr, "bold"); do { valueString.Assign(htmlValueString); // get the value of the CSS equivalent styles @@ -1156,7 +1166,7 @@ nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, if (valueString.IsEmpty()) return NS_OK; if (nsEditProperty::b == aHTMLProperty) { - if (valueString.Equals(NS_LITERAL_STRING("bold"))) { + if (valueString.Equals(boldStr)) { aIsSet = PR_TRUE; } else if (valueString.Equals(NS_LITERAL_STRING("normal"))) { @@ -1164,7 +1174,7 @@ nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, } else if (valueString.Equals(NS_LITERAL_STRING("bolder"))) { aIsSet = PR_TRUE; - valueString.Assign(NS_LITERAL_STRING("bold")); + valueString.Assign(boldStr); } else { PRInt32 weight = 0; @@ -1173,7 +1183,7 @@ nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, weight = value.ToInteger(&errorCode, 10); if (400 < weight) { aIsSet = PR_TRUE; - valueString.Assign(NS_LITERAL_STRING("bold")); + valueString.Assign(boldStr); } else { aIsSet = PR_FALSE; @@ -1286,12 +1296,7 @@ nsHTMLCSSUtils::IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, res = node->GetParentNode(getter_AddRefs(tmp)); if (NS_FAILED(res)) return res; nsCOMPtr element = do_QueryInterface(tmp); - if (element) { - node = tmp; - } - else { - node = nsnull; - } + node = element; // set to null if it's not a dom element } } while ((nsEditProperty::u == aHTMLProperty || nsEditProperty::strike == aHTMLProperty) && !aIsSet && node); @@ -1413,7 +1418,7 @@ nsHTMLCSSUtils::GetElementContainerOrSelf(nsIDOMNode * aNode, nsIDOMElement ** a { NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER); - nsCOMPtr node=aNode, parentNode=aNode; + nsCOMPtr node=aNode, parentNode; PRUint16 type; nsresult res; res = node->GetNodeType(&type); @@ -1442,7 +1447,7 @@ nsHTMLCSSUtils::SetCSSProperty(nsIDOMElement * aElement, const nsAString & aValue) { nsCOMPtr cssDecl; - PRUint32 length = 0; + PRUint32 length; nsresult res = GetInlineStyles(aElement, getter_AddRefs(cssDecl), &length); if (NS_FAILED(res)) return res; @@ -1450,12 +1455,23 @@ nsHTMLCSSUtils::SetCSSProperty(nsIDOMElement * aElement, aValue, nsString()); } + +nsresult +nsHTMLCSSUtils::SetCSSPropertyPixels(nsIDOMElement * aElement, + const nsAString & aProperty, + PRInt32 aIntValue) +{ + nsAutoString s; + s.AppendInt(aIntValue); + return SetCSSProperty(aElement, aProperty, s + NS_LITERAL_STRING("px")); +} + nsresult nsHTMLCSSUtils::RemoveCSSProperty(nsIDOMElement * aElement, const nsAString & aProperty) { nsCOMPtr cssDecl; - PRUint32 length = 0; + PRUint32 length; nsresult res = GetInlineStyles(aElement, getter_AddRefs(cssDecl), &length); if (NS_FAILED(res)) return res; diff --git a/editor/libeditor/html/nsHTMLCSSUtils.h b/editor/libeditor/html/nsHTMLCSSUtils.h index 5691db2b3e0..8782d6f63b1 100644 --- a/editor/libeditor/html/nsHTMLCSSUtils.h +++ b/editor/libeditor/html/nsHTMLCSSUtils.h @@ -123,6 +123,8 @@ public: nsresult SetCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty, const nsAString & aValue, PRBool aSuppressTransaction); + nsresult SetCSSPropertyPixels(nsIDOMElement *aElement, nsIAtom *aProperty, + PRInt32 aIntValue, PRBool aSuppressTxn); nsresult RemoveCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty, const nsAString & aPropertyValue, PRBool aSuppressTransaction); @@ -136,6 +138,9 @@ public: nsresult SetCSSProperty(nsIDOMElement * aElement, const nsAString & aProperty, const nsAString & aValue); + nsresult SetCSSPropertyPixels(nsIDOMElement * aElement, + const nsAString & aProperty, + PRInt32 aIntValue); nsresult RemoveCSSProperty(nsIDOMElement * aElement, const nsAString & aProperty);