Back to index

lightning-sunbird  0.9+nobinonly
nsCSSDeclaration.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Daniel Glazman <glazman@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 #ifndef nsCSSDeclaration_h___
00039 #define nsCSSDeclaration_h___
00040 
00041 #include "nsISupports.h"
00042 #include "nsColor.h"
00043 #include <stdio.h>
00044 #include "nsString.h"
00045 #include "nsCoord.h"
00046 #include "nsCSSValue.h"
00047 #include "nsCSSProps.h"
00048 #include "nsVoidArray.h"
00049 #include "nsValueArray.h"
00050 #include "nsCSSDataBlock.h"
00051 #include "nsCSSStruct.h"
00052 
00053 class nsCSSDeclaration {
00054 public:
00060   nsCSSDeclaration();
00061 
00062   nsCSSDeclaration(const nsCSSDeclaration& aCopy);
00063 
00069   nsresult ValueAppended(nsCSSProperty aProperty);
00070 
00071   nsresult AppendComment(const nsAString& aComment);
00072   nsresult RemoveProperty(nsCSSProperty aProperty);
00073 
00074   nsresult GetValue(nsCSSProperty aProperty, nsAString& aValue) const;
00075   nsresult GetValue(const nsAString& aProperty, nsAString& aValue) const;
00076 
00077   PRBool HasImportantData() const { return mImportantData != nsnull; }
00078   PRBool GetValueIsImportant(nsCSSProperty aProperty) const;
00079   PRBool GetValueIsImportant(const nsAString& aProperty) const;
00080 
00081   PRUint32 Count() const;
00082   nsresult GetNthProperty(PRUint32 aIndex, nsAString& aReturn) const;
00083 
00084   nsresult ToString(nsAString& aString) const;
00085 
00086   nsCSSDeclaration* Clone() const;
00087 
00088   nsresult MapRuleInfoInto(nsRuleData *aRuleData) const {
00089     return mData->MapRuleInfoInto(aRuleData);
00090   }
00091 
00092   nsresult MapImportantRuleInfoInto(nsRuleData *aRuleData) const {
00093     return mImportantData->MapRuleInfoInto(aRuleData);
00094   }
00095 
00100   PRBool InitializeEmpty();
00101 
00106   void CompressFrom(nsCSSExpandedDataBlock *aExpandedData) {
00107     NS_ASSERTION(!mData, "oops");
00108     NS_ASSERTION(!mImportantData, "oops");
00109     aExpandedData->Compress(&mData, &mImportantData);
00110   }
00111 
00119   void ExpandTo(nsCSSExpandedDataBlock *aExpandedData) {
00120     aExpandedData->AssertInitialState();
00121 
00122     NS_ASSERTION(mData, "oops");
00123     aExpandedData->Expand(&mData, &mImportantData);
00124     NS_ASSERTION(!mData && !mImportantData,
00125                  "Expand didn't null things out");
00126   }
00127 
00132   void ClearData() {
00133     mData->Destroy();
00134     mData = nsnull;
00135     if (mImportantData) {
00136       mImportantData->Destroy();
00137       mImportantData = nsnull;
00138     }
00139     mOrder.Clear();
00140   }
00141 
00142 #ifdef DEBUG
00143   void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
00144 #endif
00145   
00146 private:
00147   // Not implemented, and not supported.
00148   nsCSSDeclaration& operator=(const nsCSSDeclaration& aCopy);
00149   PRBool operator==(const nsCSSDeclaration& aCopy) const;
00150 
00151   void     AppendImportanceToString(PRBool aIsImportant, nsAString& aString) const;
00152   // return whether there was a value in |aValue| (i.e., it had a non-null unit)
00153   PRBool   AppendValueToString(nsCSSProperty aProperty, nsAString& aResult) const;
00154   // return whether there was a value in |aValue| (i.e., it had a non-null unit)
00155   PRBool   AppendCSSValueToString(nsCSSProperty aProperty, const nsCSSValue& aValue, nsAString& aResult) const;
00156 
00157   // May be called only for properties whose type is eCSSType_Value.
00158   nsresult GetValueOrImportantValue(nsCSSProperty aProperty, nsCSSValue& aValue) const;
00159 
00160   void   PropertyIsSet(PRInt32 & aPropertyIndex, PRInt32 aIndex, PRUint32 & aSet, PRUint32 aValue) const;
00161   PRBool TryBorderShorthand(nsAString & aString, PRUint32 aPropertiesSet,
00162                             PRInt32 aBorderTopWidth,
00163                             PRInt32 aBorderTopStyle,
00164                             PRInt32 aBorderTopColor,
00165                             PRInt32 aBorderBottomWidth,
00166                             PRInt32 aBorderBottomStyle,
00167                             PRInt32 aBorderBottomColor,
00168                             PRInt32 aBorderLeftWidth,
00169                             PRInt32 aBorderLeftStyle,
00170                             PRInt32 aBorderLeftColor,
00171                             PRInt32 aBorderRightWidth,
00172                             PRInt32 aBorderRightStyle,
00173                             PRInt32 aBorderRightColor) const;
00174   PRBool  TryBorderSideShorthand(nsAString & aString,
00175                                  nsCSSProperty  aShorthand,
00176                                  PRInt32 aBorderWidth,
00177                                  PRInt32 aBorderStyle,
00178                                  PRInt32 aBorderColor) const;
00179   PRBool  TryFourSidesShorthand(nsAString & aString,
00180                                 nsCSSProperty aShorthand,
00181                                 PRInt32 & aTop,
00182                                 PRInt32 & aBottom,
00183                                 PRInt32 & aLeft,
00184                                 PRInt32 & aRight,
00185                                 PRBool aClearIndexes) const;
00186   void  TryBackgroundShorthand(nsAString & aString,
00187                                PRInt32 & aBgColor, PRInt32 & aBgImage,
00188                                PRInt32 & aBgRepeat, PRInt32 & aBgAttachment,
00189                                PRInt32 & aBgPositionX,
00190                                PRInt32 & aBgPositionY) const;
00191   void  UseBackgroundPosition(nsAString & aString,
00192                               PRInt32 & aBgPositionX,
00193                               PRInt32 & aBgPositionY) const;
00194   void  TryOverflowShorthand(nsAString & aString,
00195                              PRInt32 & aOverflowX, PRInt32 & aOverflowY) const;
00196 #ifdef MOZ_SVG
00197   void  TryMarkerShorthand(nsAString & aString,
00198                            PRInt32 & aMarkerEnd,
00199                            PRInt32 & aMarkerMid,
00200                            PRInt32 & aMarkerStart) const;
00201 #endif
00202 
00203   PRBool   AllPropertiesSameImportance(PRInt32 aFirst, PRInt32 aSecond,
00204                                        PRInt32 aThird, PRInt32 aFourth,
00205                                        PRInt32 aFifth, PRInt32 aSixth,
00206                                        PRBool & aImportance) const;
00207   PRBool   AllPropertiesSameValue(PRInt32 aFirst, PRInt32 aSecond,
00208                                   PRInt32 aThird, PRInt32 aFourth) const;
00209   void     AppendPropertyAndValueToString(nsCSSProperty aProperty,
00210                                           nsAString& aResult) const
00211   {
00212     AppendPropertyAndValueToString(aProperty, aProperty, aResult);
00213   }
00214   void     AppendPropertyAndValueToString(nsCSSProperty aProperty,
00215                                           nsCSSProperty aPropertyName,
00216                                           nsAString& aResult) const;
00217 
00218 private:
00219     //
00220     // Specialized ref counting.
00221     // We do not want everyone to ref count us, only the rules which hold
00222     //  onto us (our well defined lifetime is when the last rule releases
00223     //  us).
00224     // It's worth a comment here that the main nsCSSDeclaration is refcounted,
00225     //  but it's |mImportant| is not refcounted, but just owned by the
00226     //  non-important declaration.
00227     //
00228     friend class CSSStyleRuleImpl;
00229     void AddRef(void) {
00230       ++mRefCnt;
00231     }
00232     void Release(void) {
00233       NS_ASSERTION(0 < mRefCnt, "bad Release");
00234       if (0 == --mRefCnt) {
00235         delete this;
00236       }
00237     }
00238 public:
00239     void RuleAbort(void) {
00240       NS_ASSERTION(0 == mRefCnt, "bad RuleAbort");
00241       delete this;
00242     }
00243 private:
00244   // Block everyone, except us or a derivative, from deleting us.
00245   ~nsCSSDeclaration(void);
00246     
00247   nsCSSProperty OrderValueAt(nsValueArrayIndex aValue) const {
00248     return nsCSSProperty(mOrder.ValueAt(aValue));
00249   }
00250 
00251 private:
00252     nsValueArray mOrder;
00253     nsAutoRefCnt mRefCnt;
00254     nsCSSCompressedDataBlock *mData; // never null, except while expanded
00255     nsCSSCompressedDataBlock *mImportantData; // may be null
00256 };
00257 
00258 #endif /* nsCSSDeclaration_h___ */