Back to index

lightning-sunbird  0.9+nobinonly
nsCSSDataBlock.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 nsCSSDataBlock.cpp.
00016  *
00017  * The Initial Developer of the Original Code is L. David Baron.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   L. David Baron <dbaron@dbaron.org> (original author)
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsCSSDataBlock.h"
00039 #include "nsCSSProps.h"
00040 #include "nsRuleData.h"
00041 
00042 /*
00043  * nsCSSCompressedDataBlock holds property-value pairs corresponding to
00044  * CSS declaration blocks.  The value is stored in one of the six CSS
00045  * data types.  These six types are nsCSSValue, nsCSSRect,
00046  * nsCSSValueList, nsCSSCounterData, nsCSSQuotes, and nsCSSShadow, and
00047  * each correspond to a value of the nsCSSType enumeration.
00048  *
00049  * The storage strategy uses the CDB*Storage structs below to help
00050  * ensure that all the types remain properly aligned.  nsCSSValue's
00051  * alignment requirements cannot be weaker than any others, since it
00052  * contains a pointer and an enumeration.
00053  *
00054  * The simple types, nsCSSValue and nsCSSRect have the nsCSSValue or
00055  * nsCSSRect objects stored in the block.  The list types have only a
00056  * pointer to the first element in the list stored in the block.
00057  */
00058 
00059 struct CDBValueStorage {
00060     nsCSSProperty property;
00061     nsCSSValue value;
00062 };
00063 
00064 struct CDBRectStorage {
00065     nsCSSProperty property;
00066     nsCSSRect value;
00067 
00068 };
00069 
00070 struct CDBValuePairStorage {
00071     nsCSSProperty property;
00072     nsCSSValuePair value;
00073 };
00074 
00075 struct CDBPointerStorage {
00076     nsCSSProperty property;
00077     void *value;
00078 };
00079 
00080 enum {
00081     CDBValueStorage_advance = sizeof(CDBValueStorage),
00082     CDBRectStorage_advance = sizeof(CDBRectStorage),
00083     CDBValuePairStorage_advance = sizeof(CDBValuePairStorage),
00084     // round up using the closest estimate we can get of the alignment
00085     // requirements of nsCSSValue:
00086     CDBPointerStorage_advance = PR_ROUNDUP(sizeof(CDBPointerStorage),
00087                                 sizeof(CDBValueStorage) - sizeof(nsCSSValue))
00088 };
00089 
00090 /*
00091  * Define a bunch of utility functions for getting the property or any
00092  * of the value types when the cursor is at the beginning of the storage
00093  * for the property-value pair.  The versions taking a non-const cursor
00094  * argument return a reference so that the caller can assign into the
00095  * result.
00096  */
00097 
00098 inline nsCSSProperty& PropertyAtCursor(char *aCursor) {
00099     return *NS_REINTERPRET_CAST(nsCSSProperty*, aCursor);
00100 }
00101 
00102 inline nsCSSProperty PropertyAtCursor(const char *aCursor) {
00103     return *NS_REINTERPRET_CAST(const nsCSSProperty*, aCursor);
00104 }
00105 
00106 inline nsCSSValue* ValueAtCursor(char *aCursor) {
00107     return & NS_REINTERPRET_CAST(CDBValueStorage*, aCursor)->value;
00108 }
00109 
00110 inline const nsCSSValue* ValueAtCursor(const char *aCursor) {
00111     return & NS_REINTERPRET_CAST(const CDBValueStorage*, aCursor)->value;
00112 }
00113 
00114 inline nsCSSRect* RectAtCursor(char *aCursor) {
00115     return & NS_REINTERPRET_CAST(CDBRectStorage*, aCursor)->value;
00116 }
00117 
00118 inline const nsCSSRect* RectAtCursor(const char *aCursor) {
00119     return & NS_REINTERPRET_CAST(const CDBRectStorage*, aCursor)->value;
00120 }
00121 
00122 inline nsCSSValuePair* ValuePairAtCursor(char *aCursor) {
00123   return & NS_REINTERPRET_CAST(CDBValuePairStorage*, aCursor)->value;
00124 }
00125 
00126 inline const nsCSSValuePair* ValuePairAtCursor(const char *aCursor) {
00127   return & NS_REINTERPRET_CAST(const CDBValuePairStorage*, aCursor)->value;
00128 }
00129 
00130 inline void*& PointerAtCursor(char *aCursor) {
00131     return NS_REINTERPRET_CAST(CDBPointerStorage*, aCursor)->value;
00132 }
00133 
00134 inline void* PointerAtCursor(const char *aCursor) {
00135     return NS_REINTERPRET_CAST(const CDBPointerStorage*, aCursor)->value;
00136 }
00137 
00138 inline nsCSSValueList*& ValueListAtCursor(char *aCursor) {
00139     return * NS_REINTERPRET_CAST(nsCSSValueList**,
00140                     & NS_REINTERPRET_CAST(CDBPointerStorage*, aCursor)->value);
00141 }
00142 
00143 inline nsCSSValueList* ValueListAtCursor(const char *aCursor) {
00144     return NS_STATIC_CAST(nsCSSValueList*,
00145                 NS_REINTERPRET_CAST(const CDBPointerStorage*, aCursor)->value);
00146 }
00147 
00148 inline nsCSSCounterData*& CounterDataAtCursor(char *aCursor) {
00149     return * NS_REINTERPRET_CAST(nsCSSCounterData**,
00150                     & NS_REINTERPRET_CAST(CDBPointerStorage*, aCursor)->value);
00151 }
00152 
00153 inline nsCSSCounterData* CounterDataAtCursor(const char *aCursor) {
00154     return NS_STATIC_CAST(nsCSSCounterData*,
00155                 NS_REINTERPRET_CAST(const CDBPointerStorage*, aCursor)->value);
00156 }
00157 
00158 inline nsCSSQuotes*& QuotesAtCursor(char *aCursor) {
00159     return * NS_REINTERPRET_CAST(nsCSSQuotes**,
00160                     & NS_REINTERPRET_CAST(CDBPointerStorage*, aCursor)->value);
00161 }
00162 
00163 inline nsCSSQuotes* QuotesAtCursor(const char *aCursor) {
00164     return NS_STATIC_CAST(nsCSSQuotes*,
00165                 NS_REINTERPRET_CAST(const CDBPointerStorage*, aCursor)->value);
00166 }
00167 
00168 inline nsCSSShadow*& ShadowAtCursor(char *aCursor) {
00169     return * NS_REINTERPRET_CAST(nsCSSShadow**,
00170                     & NS_REINTERPRET_CAST(CDBPointerStorage*, aCursor)->value);
00171 }
00172 
00173 inline nsCSSShadow* ShadowAtCursor(const char *aCursor) {
00174     return NS_STATIC_CAST(nsCSSShadow*,
00175                 NS_REINTERPRET_CAST(const CDBPointerStorage*, aCursor)->value);
00176 }
00177 
00178 nsresult
00179 nsCSSCompressedDataBlock::MapRuleInfoInto(nsRuleData *aRuleData) const
00180 {
00181     // If we have no data for this struct, then return immediately.
00182     // This optimization should make us return most of the time, so we
00183     // have to worry much less (although still some) about the speed of
00184     // the rest of the function.
00185     if (!(nsCachedStyleData::GetBitForSID(aRuleData->mSID) & mStyleBits))
00186         return NS_OK;
00187 
00188     const char* cursor = Block();
00189     const char* cursor_end = BlockEnd();
00190     while (cursor < cursor_end) {
00191         nsCSSProperty iProp = PropertyAtCursor(cursor);
00192         NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00193                      "out of range");
00194         if (nsCSSProps::kSIDTable[iProp] == aRuleData->mSID) {
00195             void *prop =
00196                 nsCSSExpandedDataBlock::RuleDataPropertyAt(aRuleData, iProp);
00197             switch (nsCSSProps::kTypeTable[iProp]) {
00198                 case eCSSType_Value: {
00199                     nsCSSValue* target = NS_STATIC_CAST(nsCSSValue*, prop);
00200                     if (target->GetUnit() == eCSSUnit_Null) {
00201                         const nsCSSValue *val = ValueAtCursor(cursor);
00202                         NS_ASSERTION(val->GetUnit() != eCSSUnit_Null, "oops");
00203                         if ((iProp == eCSSProperty_background_image ||
00204                              iProp == eCSSProperty_list_style_image) &&
00205                             val->GetUnit() == eCSSUnit_URL) {
00206                             val->StartImageLoad(aRuleData->mPresContext->GetDocument(),
00207                                                 iProp == eCSSProperty_background_image);
00208                         }
00209                         *target = *val;
00210                         if (iProp == eCSSProperty_font_family) {
00211                             // XXX Are there other things like this?
00212                             aRuleData->mFontData->mFamilyFromHTML = PR_FALSE;
00213                         }
00214                     }
00215                     cursor += CDBValueStorage_advance;
00216                 } break;
00217 
00218                 case eCSSType_Rect: {
00219                     const nsCSSRect* val = RectAtCursor(cursor);
00220                     NS_ASSERTION(val->HasValue(), "oops");
00221                     nsCSSRect* target = NS_STATIC_CAST(nsCSSRect*, prop);
00222                     if (target->mTop.GetUnit() == eCSSUnit_Null)
00223                         target->mTop = val->mTop;
00224                     if (target->mRight.GetUnit() == eCSSUnit_Null)
00225                         target->mRight = val->mRight;
00226                     if (target->mBottom.GetUnit() == eCSSUnit_Null)
00227                         target->mBottom = val->mBottom;
00228                     if (target->mLeft.GetUnit() == eCSSUnit_Null)
00229                         target->mLeft = val->mLeft;
00230                     cursor += CDBRectStorage_advance;
00231                 } break;
00232 
00233                 case eCSSType_ValuePair: {
00234                     const nsCSSValuePair* val = ValuePairAtCursor(cursor);
00235                     NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00236                                  val->mYValue.GetUnit() != eCSSUnit_Null, "oops");
00237                     nsCSSValuePair* target = NS_STATIC_CAST(nsCSSValuePair*, prop);
00238                     if (target->mXValue.GetUnit() == eCSSUnit_Null)
00239                         target->mXValue = val->mXValue;
00240                     if (target->mYValue.GetUnit() == eCSSUnit_Null)
00241                         target->mYValue = val->mYValue;
00242                     cursor += CDBValuePairStorage_advance;
00243                 } break;
00244 
00245                 case eCSSType_ValueList:
00246                     if (iProp == eCSSProperty_content) {
00247                         for (nsCSSValueList* l = ValueListAtCursor(cursor);
00248                              l; l = l->mNext)
00249                             if (l->mValue.GetUnit() == eCSSUnit_URL)
00250                                 l->mValue.StartImageLoad(
00251                                     aRuleData->mPresContext->GetDocument());
00252                     } else if (iProp == eCSSProperty_cursor) {
00253                         for (nsCSSValueList* l = ValueListAtCursor(cursor);
00254                              l; l = l->mNext)
00255                             if (l->mValue.GetUnit() == eCSSUnit_Array)
00256                                 l->mValue.GetArrayValue()->Item(0).
00257                                     StartImageLoad(
00258                                     aRuleData->mPresContext->GetDocument());
00259                     }
00260                 // fall through
00261                 case eCSSType_CounterData:
00262                 case eCSSType_Quotes:
00263                 case eCSSType_Shadow: {
00264                     void** target = NS_STATIC_CAST(void**, prop);
00265                     if (!*target) {
00266                         void* val = PointerAtCursor(cursor);
00267                         NS_ASSERTION(val, "oops");
00268                         *target = val;
00269                     }
00270                     cursor += CDBPointerStorage_advance;
00271                 } break;
00272             }
00273         } else {
00274             switch (nsCSSProps::kTypeTable[iProp]) {
00275                 case eCSSType_Value: {
00276                     cursor += CDBValueStorage_advance;
00277                 } break;
00278 
00279                 case eCSSType_Rect: {
00280                     cursor += CDBRectStorage_advance;
00281                 } break;
00282 
00283                 case eCSSType_ValuePair: {
00284                     cursor += CDBValuePairStorage_advance;
00285                 } break;
00286 
00287                 case eCSSType_ValueList:
00288                 case eCSSType_CounterData:
00289                 case eCSSType_Quotes:
00290                 case eCSSType_Shadow: {
00291                     cursor += CDBPointerStorage_advance;
00292                 } break;
00293             }
00294         }
00295     }
00296     NS_ASSERTION(cursor == cursor_end, "inconsistent data");
00297 
00298     return NS_OK;
00299 }
00300 
00301 const void*
00302 nsCSSCompressedDataBlock::StorageFor(nsCSSProperty aProperty) const
00303 {
00304     // If we have no data for this struct, then return immediately.
00305     // This optimization should make us return most of the time, so we
00306     // have to worry much less (although still some) about the speed of
00307     // the rest of the function.
00308     if (!(nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[aProperty]) &
00309           mStyleBits))
00310         return nsnull;
00311 
00312     const char* cursor = Block();
00313     const char* cursor_end = BlockEnd();
00314     while (cursor < cursor_end) {
00315         nsCSSProperty iProp = PropertyAtCursor(cursor);
00316         NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00317                      "out of range");
00318         if (iProp == aProperty) {
00319             switch (nsCSSProps::kTypeTable[iProp]) {
00320                 case eCSSType_Value: {
00321                     return ValueAtCursor(cursor);
00322                 }
00323                 case eCSSType_Rect: {
00324                     return RectAtCursor(cursor);
00325                 }
00326                 case eCSSType_ValuePair: {
00327                     return ValuePairAtCursor(cursor);
00328                 }
00329                 case eCSSType_ValueList:
00330                 case eCSSType_CounterData:
00331                 case eCSSType_Quotes:
00332                 case eCSSType_Shadow: {
00333                     return &PointerAtCursor(NS_CONST_CAST(char*, cursor));
00334                 }
00335             }
00336         }
00337         switch (nsCSSProps::kTypeTable[iProp]) {
00338             case eCSSType_Value: {
00339                 cursor += CDBValueStorage_advance;
00340             } break;
00341 
00342             case eCSSType_Rect: {
00343                 cursor += CDBRectStorage_advance;
00344             } break;
00345 
00346             case eCSSType_ValuePair: {
00347                 cursor += CDBValuePairStorage_advance;
00348             } break;
00349 
00350             case eCSSType_ValueList:
00351             case eCSSType_CounterData:
00352             case eCSSType_Quotes:
00353             case eCSSType_Shadow: {
00354                 cursor += CDBPointerStorage_advance;
00355             } break;
00356         }
00357     }
00358     NS_ASSERTION(cursor == cursor_end, "inconsistent data");
00359 
00360     return nsnull;
00361 }
00362 
00363 nsCSSCompressedDataBlock*
00364 nsCSSCompressedDataBlock::Clone() const
00365 {
00366     const char *cursor = Block(), *cursor_end = BlockEnd();
00367     char *result_cursor;
00368 
00369     nsCSSCompressedDataBlock *result =
00370         new(cursor_end - cursor) nsCSSCompressedDataBlock();
00371     if (!result)
00372         return nsnull;
00373     result_cursor = result->Block();
00374 
00375     while (cursor < cursor_end) {
00376         nsCSSProperty iProp = PropertyAtCursor(cursor);
00377         NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00378                      "out of range");
00379         PropertyAtCursor(result_cursor) = iProp;
00380 
00381         switch (nsCSSProps::kTypeTable[iProp]) {
00382             case eCSSType_Value: {
00383                 const nsCSSValue* val = ValueAtCursor(cursor);
00384                 NS_ASSERTION(val->GetUnit() != eCSSUnit_Null, "oops");
00385                 nsCSSValue *result_val = ValueAtCursor(result_cursor);
00386                 new (result_val) nsCSSValue(*val);
00387                 cursor += CDBValueStorage_advance;
00388                 result_cursor +=  CDBValueStorage_advance;
00389             } break;
00390 
00391             case eCSSType_Rect: {
00392                 const nsCSSRect* val = RectAtCursor(cursor);
00393                 NS_ASSERTION(val->HasValue(), "oops");
00394                 nsCSSRect* result_val = RectAtCursor(result_cursor);
00395                 new (result_val) nsCSSRect(*val);
00396                 cursor += CDBRectStorage_advance;
00397                 result_cursor += CDBRectStorage_advance;
00398             } break;
00399 
00400             case eCSSType_ValuePair: {
00401                 const nsCSSValuePair* val = ValuePairAtCursor(cursor);
00402                 NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00403                              val->mYValue.GetUnit() != eCSSUnit_Null, "oops");
00404                 nsCSSValuePair* result_val = ValuePairAtCursor(result_cursor);
00405                 new (result_val) nsCSSValuePair(*val);
00406                 cursor += CDBValuePairStorage_advance;
00407                 result_cursor += CDBValuePairStorage_advance;
00408             } break;
00409 
00410             case eCSSType_ValueList:
00411             case eCSSType_CounterData:
00412             case eCSSType_Quotes:
00413             case eCSSType_Shadow: {
00414                 void *copy;
00415                 NS_ASSERTION(PointerAtCursor(cursor), "oops");
00416                 switch (nsCSSProps::kTypeTable[iProp]) {
00417                     default:
00418                         NS_NOTREACHED("unreachable");
00419                         // fall through to keep gcc's uninitialized
00420                         // variable warning quiet
00421                     case eCSSType_ValueList:
00422                         copy = new nsCSSValueList(*ValueListAtCursor(cursor));
00423                         break;
00424                     case eCSSType_CounterData:
00425                         copy =
00426                             new nsCSSCounterData(*CounterDataAtCursor(cursor));
00427                         break;
00428                     case eCSSType_Quotes:
00429                         copy = new nsCSSQuotes(*QuotesAtCursor(cursor));
00430                         break;
00431                     case eCSSType_Shadow:
00432                         copy = new nsCSSShadow(*ShadowAtCursor(cursor));
00433                         break;
00434                 }
00435                 if (!copy) {
00436                     result->mBlockEnd = result_cursor;
00437                     result->Destroy();
00438                     return nsnull;
00439                 }
00440                 PointerAtCursor(result_cursor) = copy;
00441                 cursor += CDBPointerStorage_advance;
00442                 result_cursor += CDBPointerStorage_advance;
00443             } break;
00444         }
00445     }
00446     NS_ASSERTION(cursor == cursor_end, "inconsistent data");
00447 
00448     result->mBlockEnd = result_cursor;
00449     result->mStyleBits = mStyleBits;
00450     NS_ASSERTION(result->DataSize() == DataSize(), "wrong size");
00451     return result;
00452 }
00453 
00454 void
00455 nsCSSCompressedDataBlock::Destroy()
00456 {
00457     const char* cursor = Block();
00458     const char* cursor_end = BlockEnd();
00459     while (cursor < cursor_end) {
00460         nsCSSProperty iProp = PropertyAtCursor(cursor);
00461         NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00462                      "out of range");
00463 
00464         switch (nsCSSProps::kTypeTable[iProp]) {
00465             case eCSSType_Value: {
00466                 const nsCSSValue* val = ValueAtCursor(cursor);
00467                 NS_ASSERTION(val->GetUnit() != eCSSUnit_Null, "oops");
00468                 val->~nsCSSValue();
00469                 cursor += CDBValueStorage_advance;
00470             } break;
00471 
00472             case eCSSType_Rect: {
00473                 const nsCSSRect* val = RectAtCursor(cursor);
00474                 NS_ASSERTION(val->HasValue(), "oops");
00475                 val->~nsCSSRect();
00476                 cursor += CDBRectStorage_advance;
00477             } break;
00478 
00479             case eCSSType_ValuePair: {
00480                 const nsCSSValuePair* val = ValuePairAtCursor(cursor);
00481                 NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00482                              val->mYValue.GetUnit() != eCSSUnit_Null, "oops");
00483                 val->~nsCSSValuePair();
00484                 cursor += CDBValuePairStorage_advance;
00485             } break;
00486 
00487             case eCSSType_ValueList: {
00488                 nsCSSValueList* val = ValueListAtCursor(cursor);
00489                 NS_ASSERTION(val, "oops");
00490                 delete val;
00491                 cursor += CDBPointerStorage_advance;
00492             } break;
00493 
00494             case eCSSType_CounterData: {
00495                 nsCSSCounterData* val = CounterDataAtCursor(cursor);
00496                 NS_ASSERTION(val, "oops");
00497                 delete val;
00498                 cursor += CDBPointerStorage_advance;
00499             } break;
00500 
00501             case eCSSType_Quotes: {
00502                 nsCSSQuotes* val = QuotesAtCursor(cursor);
00503                 NS_ASSERTION(val, "oops");
00504                 delete val;
00505                 cursor += CDBPointerStorage_advance;
00506             } break;
00507 
00508             case eCSSType_Shadow: {
00509                 nsCSSShadow* val = ShadowAtCursor(cursor);
00510                 NS_ASSERTION(val, "oops");
00511                 delete val;
00512                 cursor += CDBPointerStorage_advance;
00513             } break;
00514         }
00515     }
00516     NS_ASSERTION(cursor == cursor_end, "inconsistent data");
00517     delete this;
00518 }
00519 
00520 /* static */ nsCSSCompressedDataBlock*
00521 nsCSSCompressedDataBlock::CreateEmptyBlock()
00522 {
00523     nsCSSCompressedDataBlock *result = new(0) nsCSSCompressedDataBlock();
00524     if (!result)
00525         return nsnull;
00526     result->mBlockEnd = result->Block();
00527     return result;
00528 }
00529 
00530 /*****************************************************************************/
00531 
00532 nsCSSExpandedDataBlock::nsCSSExpandedDataBlock()
00533 {
00534     ClearSets();
00535     AssertInitialState();
00536 }
00537 
00538 nsCSSExpandedDataBlock::~nsCSSExpandedDataBlock()
00539 {
00540     AssertInitialState();
00541 }
00542 
00543 const nsCSSExpandedDataBlock::PropertyOffsetInfo
00544 nsCSSExpandedDataBlock::kOffsetTable[eCSSProperty_COUNT_no_shorthands] = {
00545     #define CSS_PROP_BACKENDONLY(name_, id_, method_, datastruct_, member_, type_, kwtable_) \
00546         { offsetof(nsCSSExpandedDataBlock, m##datastruct_.member_),           \
00547           size_t(-1),                                                         \
00548           size_t(-1) },
00549     #define CSS_PROP(name_, id_, method_, datastruct_, member_, type_, kwtable_) \
00550         { offsetof(nsCSSExpandedDataBlock, m##datastruct_.member_),           \
00551           offsetof(nsRuleData, m##datastruct_##Data),                         \
00552           offsetof(nsRuleData##datastruct_, member_) },
00553     #include "nsCSSPropList.h"
00554     #undef CSS_PROP
00555     #undef CSS_PROP_BACKENDONLY
00556 };
00557 
00558 void
00559 nsCSSExpandedDataBlock::DoExpand(nsCSSCompressedDataBlock *aBlock,
00560                                  PRBool aImportant)
00561 {
00562     NS_PRECONDITION(aBlock, "unexpected null block");
00563 
00564     /*
00565      * Save needless copying and allocation by copying the memory
00566      * corresponding to the stored data in the compressed block, and
00567      * then, to avoid destructors, deleting the compressed block by
00568      * calling |delete| instead of using its |Destroy| method.
00569      */
00570     const char* cursor = aBlock->Block();
00571     const char* cursor_end = aBlock->BlockEnd();
00572     while (cursor < cursor_end) {
00573         nsCSSProperty iProp = PropertyAtCursor(cursor);
00574         NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00575                      "out of range");
00576         NS_ASSERTION(!HasPropertyBit(iProp),
00577                      "compressed block has property multiple times");
00578         SetPropertyBit(iProp);
00579         if (aImportant)
00580             SetImportantBit(iProp);
00581         void *prop = PropertyAt(iProp);
00582 
00583         switch (nsCSSProps::kTypeTable[iProp]) {
00584             case eCSSType_Value: {
00585                 const nsCSSValue* val = ValueAtCursor(cursor);
00586                 NS_ASSERTION(val->GetUnit() != eCSSUnit_Null, "oops");
00587                 memcpy(prop, val, sizeof(nsCSSValue));
00588                 cursor += CDBValueStorage_advance;
00589             } break;
00590 
00591             case eCSSType_Rect: {
00592                 const nsCSSRect* val = RectAtCursor(cursor);
00593                 NS_ASSERTION(val->HasValue(), "oops");
00594                 memcpy(prop, val, sizeof(nsCSSRect));
00595                 cursor += CDBRectStorage_advance;
00596             } break;
00597 
00598             case eCSSType_ValuePair: {
00599                 const nsCSSValuePair* val = ValuePairAtCursor(cursor);
00600                 NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00601                              val->mYValue.GetUnit() != eCSSUnit_Null, "oops");
00602                 memcpy(prop, val, sizeof(nsCSSValuePair));
00603                 cursor += CDBValuePairStorage_advance;
00604             } break;
00605 
00606             case eCSSType_ValueList:
00607             case eCSSType_CounterData:
00608             case eCSSType_Quotes:
00609             case eCSSType_Shadow: {
00610                 void* val = PointerAtCursor(cursor);
00611                 NS_ASSERTION(val, "oops");
00612                 *NS_STATIC_CAST(void**, prop) = val;
00613                 cursor += CDBPointerStorage_advance;
00614             } break;
00615         }
00616     }
00617     NS_ASSERTION(cursor == cursor_end, "inconsistent data");
00618 
00619     delete aBlock;
00620 }
00621 
00622 void
00623 nsCSSExpandedDataBlock::Expand(nsCSSCompressedDataBlock **aNormalBlock,
00624                                nsCSSCompressedDataBlock **aImportantBlock)
00625 {
00626     NS_PRECONDITION(*aNormalBlock, "unexpected null block");
00627     AssertInitialState();
00628 
00629     DoExpand(*aNormalBlock, PR_FALSE);
00630     *aNormalBlock = nsnull;
00631     if (*aImportantBlock) {
00632         DoExpand(*aImportantBlock, PR_TRUE);
00633         *aImportantBlock = nsnull;
00634     }
00635 }
00636 
00637 nsCSSExpandedDataBlock::ComputeSizeResult
00638 nsCSSExpandedDataBlock::ComputeSize()
00639 {
00640     ComputeSizeResult result = {0, 0};
00641     for (PRUint32 iHigh = 0; iHigh < NS_ARRAY_LENGTH(mPropertiesSet); ++iHigh) {
00642         if (mPropertiesSet[iHigh] == 0)
00643             continue;
00644         for (PRInt32 iLow = 0; iLow < kPropertiesSetChunkSize; ++iLow) {
00645             if ((mPropertiesSet[iHigh] & (1 << iLow)) == 0)
00646                 continue;
00647             nsCSSProperty iProp =
00648                 nsCSSProperty(iHigh * kPropertiesSetChunkSize + iLow);
00649             NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00650                          "out of range");
00651             void *prop = PropertyAt(iProp);
00652             PRUint32 increment = 0;
00653             switch (nsCSSProps::kTypeTable[iProp]) {
00654                 case eCSSType_Value: {
00655 #ifdef DEBUG
00656                     nsCSSValue* val = NS_STATIC_CAST(nsCSSValue*, prop);
00657                     NS_ASSERTION(val->GetUnit() != eCSSUnit_Null,
00658                                  "null value while computing size");
00659 #endif
00660                     increment = CDBValueStorage_advance;
00661                 } break;
00662 
00663                 case eCSSType_Rect: {
00664 #ifdef DEBUG
00665                     nsCSSRect* val = NS_STATIC_CAST(nsCSSRect*, prop);
00666                     NS_ASSERTION(val->HasValue(),
00667                                  "Valueless rect while computing size");
00668 #endif
00669                     increment = CDBRectStorage_advance;
00670                 } break;
00671 
00672                 case eCSSType_ValuePair: {
00673 #ifdef DEBUG
00674                     nsCSSValuePair* val = NS_STATIC_CAST(nsCSSValuePair*, prop);
00675                     NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00676                                  val->mYValue.GetUnit() != eCSSUnit_Null,
00677                                  "Valueless pair while computing size");
00678 #endif
00679                     increment = CDBValuePairStorage_advance;
00680                 } break;
00681 
00682                 case eCSSType_ValueList:
00683                 case eCSSType_CounterData:
00684                 case eCSSType_Quotes:
00685                 case eCSSType_Shadow: {
00686 #ifdef DEBUG
00687                     void* val = *NS_STATIC_CAST(void**, prop);
00688                     NS_ASSERTION(val, "Null pointer while computing size");
00689 #endif
00690                     increment = CDBPointerStorage_advance;
00691                 } break;
00692             }
00693             if ((mPropertiesImportant[iHigh] & (1 << iLow)) == 0)
00694                 result.normal += increment;
00695             else
00696                 result.important += increment;
00697         }
00698     }
00699     return result;
00700 }
00701 
00702 void
00703 nsCSSExpandedDataBlock::Compress(nsCSSCompressedDataBlock **aNormalBlock,
00704                                  nsCSSCompressedDataBlock **aImportantBlock)
00705 {
00706     nsCSSCompressedDataBlock *result_normal, *result_important;
00707     char *cursor_normal, *cursor_important;
00708 
00709     ComputeSizeResult size = ComputeSize();
00710     
00711     result_normal = new(size.normal) nsCSSCompressedDataBlock();
00712     if (!result_normal) {
00713         *aNormalBlock = nsnull;
00714         *aImportantBlock = nsnull;
00715         return;
00716     }
00717     cursor_normal = result_normal->Block();
00718 
00719     if (size.important != 0) {
00720         result_important = new(size.important) nsCSSCompressedDataBlock();
00721         if (!result_important) {
00722             delete result_normal;
00723             *aNormalBlock = nsnull;
00724             *aImportantBlock = nsnull;
00725             return;
00726         }
00727         cursor_important = result_important->Block();
00728     } else {
00729         result_important = nsnull;
00730     }
00731 
00732     /*
00733      * Save needless copying and allocation by copying the memory
00734      * corresponding to the stored data in the expanded block, and then
00735      * clearing the data in the expanded block.
00736      */
00737     for (PRUint32 iHigh = 0; iHigh < NS_ARRAY_LENGTH(mPropertiesSet); ++iHigh) {
00738         if (mPropertiesSet[iHigh] == 0)
00739             continue;
00740         for (PRInt32 iLow = 0; iLow < kPropertiesSetChunkSize; ++iLow) {
00741             if ((mPropertiesSet[iHigh] & (1 << iLow)) == 0)
00742                 continue;
00743             nsCSSProperty iProp =
00744                 nsCSSProperty(iHigh * kPropertiesSetChunkSize + iLow);
00745             NS_ASSERTION(0 <= iProp && iProp < eCSSProperty_COUNT_no_shorthands,
00746                          "out of range");
00747             void *prop = PropertyAt(iProp);
00748             PRBool important =
00749                 (mPropertiesImportant[iHigh] & (1 << iLow)) != 0;
00750             char *&cursor = important ? cursor_important : cursor_normal;
00751             nsCSSCompressedDataBlock *result =
00752                 important ? result_important : result_normal;
00753             switch (nsCSSProps::kTypeTable[iProp]) {
00754                 case eCSSType_Value: {
00755                     nsCSSValue* val = NS_STATIC_CAST(nsCSSValue*, prop);
00756                     NS_ASSERTION(val->GetUnit() != eCSSUnit_Null,
00757                                  "Null value while compressing");
00758                     CDBValueStorage *storage =
00759                         NS_REINTERPRET_CAST(CDBValueStorage*, cursor);
00760                     storage->property = iProp;
00761                     memcpy(&storage->value, val, sizeof(nsCSSValue));
00762                     new (val) nsCSSValue();
00763                     cursor += CDBValueStorage_advance;
00764                 } break;
00765 
00766                 case eCSSType_Rect: {
00767                     nsCSSRect* val = NS_STATIC_CAST(nsCSSRect*, prop);
00768                     NS_ASSERTION(val->HasValue(),
00769                                  "Valueless rect while compressing");
00770                     CDBRectStorage *storage =
00771                         NS_REINTERPRET_CAST(CDBRectStorage*, cursor);
00772                     storage->property = iProp;
00773                     memcpy(&storage->value, val, sizeof(nsCSSRect));
00774                     new (val) nsCSSRect();
00775                     cursor += CDBRectStorage_advance;
00776                 } break;
00777 
00778                 case eCSSType_ValuePair: {
00779                     nsCSSValuePair* val = NS_STATIC_CAST(nsCSSValuePair*, prop);
00780                     NS_ASSERTION(val->mXValue.GetUnit() != eCSSUnit_Null ||
00781                                  val->mYValue.GetUnit() != eCSSUnit_Null,
00782                                  "Valueless pair while compressing");
00783                     CDBValuePairStorage *storage =
00784                         NS_REINTERPRET_CAST(CDBValuePairStorage*, cursor);
00785                     storage->property = iProp;
00786                     memcpy(&storage->value, val, sizeof(nsCSSValuePair));
00787                     new (val) nsCSSValuePair();
00788                     cursor += CDBValuePairStorage_advance;
00789                 } break;
00790 
00791                 case eCSSType_ValueList:
00792                 case eCSSType_CounterData:
00793                 case eCSSType_Quotes:
00794                 case eCSSType_Shadow: {
00795                     void*& val = *NS_STATIC_CAST(void**, prop);
00796                     NS_ASSERTION(val, "Null pointer while compressing");
00797                     CDBPointerStorage *storage =
00798                         NS_REINTERPRET_CAST(CDBPointerStorage*, cursor);
00799                     storage->property = iProp;
00800                     storage->value = val;
00801                     val = nsnull;
00802                     cursor += CDBPointerStorage_advance;
00803                 } break;
00804             }
00805             result->mStyleBits |=
00806                 nsCachedStyleData::GetBitForSID(nsCSSProps::kSIDTable[iProp]);
00807         }
00808     }
00809 
00810     result_normal->mBlockEnd = cursor_normal;
00811     NS_ASSERTION(result_normal->DataSize() == ptrdiff_t(size.normal),
00812                  "size miscalculation");
00813     if (result_important) {
00814         result_important->mBlockEnd = cursor_important;
00815         NS_ASSERTION(result_important->DataSize() == ptrdiff_t(size.important),
00816                      "size miscalculation");
00817     }
00818 
00819     ClearSets();
00820     AssertInitialState();
00821     *aNormalBlock = result_normal;
00822     *aImportantBlock = result_important;
00823 }
00824 
00825 void
00826 nsCSSExpandedDataBlock::Clear()
00827 {
00828     for (PRUint32 iHigh = 0; iHigh < NS_ARRAY_LENGTH(mPropertiesSet); ++iHigh) {
00829         if (mPropertiesSet[iHigh] == 0)
00830             continue;
00831         for (PRInt32 iLow = 0; iLow < kPropertiesSetChunkSize; ++iLow) {
00832             if ((mPropertiesSet[iHigh] & (1 << iLow)) == 0)
00833                 continue;
00834             nsCSSProperty iProp =
00835                 nsCSSProperty(iHigh * kPropertiesSetChunkSize + iLow);
00836             ClearProperty(iProp);
00837         }
00838     }
00839 
00840     AssertInitialState();
00841 }
00842 
00843 void
00844 nsCSSExpandedDataBlock::ClearProperty(nsCSSProperty aPropID)
00845 {
00846     NS_ASSERTION(0 <= aPropID && aPropID < eCSSProperty_COUNT_no_shorthands,
00847                  "out of range");
00848 
00849     ClearPropertyBit(aPropID);
00850     ClearImportantBit(aPropID);
00851 
00852     void *prop = PropertyAt(aPropID);
00853     switch (nsCSSProps::kTypeTable[aPropID]) {
00854         case eCSSType_Value: {
00855             nsCSSValue* val = NS_STATIC_CAST(nsCSSValue*, prop);
00856             val->Reset();
00857         } break;
00858 
00859         case eCSSType_Rect: {
00860             nsCSSRect* val = NS_STATIC_CAST(nsCSSRect*, prop);
00861             val->Reset();
00862         } break;
00863 
00864         case eCSSType_ValuePair: {
00865             nsCSSValuePair* val = NS_STATIC_CAST(nsCSSValuePair*, prop);
00866             val->mXValue.Reset();
00867             val->mYValue.Reset();
00868         } break;
00869 
00870         case eCSSType_ValueList: {
00871             nsCSSValueList*& val = *NS_STATIC_CAST(nsCSSValueList**, prop);
00872             if (val) {
00873                 delete val;
00874                 val = nsnull;
00875             }
00876         } break;
00877 
00878         case eCSSType_CounterData: {
00879             nsCSSCounterData*& val =
00880                 *NS_STATIC_CAST(nsCSSCounterData**, prop);
00881             if (val) {
00882                 delete val;
00883                 val = nsnull;
00884             }
00885         } break;
00886 
00887         case eCSSType_Quotes: {
00888             nsCSSQuotes*& val = *NS_STATIC_CAST(nsCSSQuotes**, prop);
00889             if (val) {
00890                 delete val;
00891                 val = nsnull;
00892             }
00893         } break;
00894 
00895         case eCSSType_Shadow: {
00896             nsCSSShadow*& val = *NS_STATIC_CAST(nsCSSShadow**, prop);
00897             if (val) {
00898                 delete val;
00899                 val = nsnull;
00900             }
00901         } break;
00902     }
00903 }
00904 
00905 #ifdef DEBUG
00906 void
00907 nsCSSExpandedDataBlock::DoAssertInitialState()
00908 {
00909     PRUint32 i;
00910     for (i = 0; i < NS_ARRAY_LENGTH(mPropertiesSet); ++i) {
00911         NS_ASSERTION(mPropertiesSet[i] == 0, "not initial state");
00912     }
00913     for (i = 0; i < NS_ARRAY_LENGTH(mPropertiesImportant); ++i) {
00914         NS_ASSERTION(mPropertiesImportant[i] == 0, "not initial state");
00915     }
00916 
00917     for (i = 0; i < eCSSProperty_COUNT_no_shorthands; ++i) {
00918         void *prop = PropertyAt(nsCSSProperty(i));
00919         switch (nsCSSProps::kTypeTable[i]) {
00920             case eCSSType_Value: {
00921                 nsCSSValue* val = NS_STATIC_CAST(nsCSSValue*, prop);
00922                 NS_ASSERTION(val->GetUnit() == eCSSUnit_Null,
00923                              "not initial state");
00924             } break;
00925 
00926             case eCSSType_Rect: {
00927                 nsCSSRect* val = NS_STATIC_CAST(nsCSSRect*, prop);
00928                 NS_ASSERTION(val->mTop.GetUnit() == eCSSUnit_Null,
00929                              "not initial state");
00930                 NS_ASSERTION(val->mRight.GetUnit() == eCSSUnit_Null,
00931                              "not initial state");
00932                 NS_ASSERTION(val->mBottom.GetUnit() == eCSSUnit_Null,
00933                              "not initial state");
00934                 NS_ASSERTION(val->mLeft.GetUnit() == eCSSUnit_Null,
00935                              "not initial state");
00936             } break;
00937 
00938             case eCSSType_ValuePair: {
00939                 nsCSSValuePair* val = NS_STATIC_CAST(nsCSSValuePair*, prop);
00940                 NS_ASSERTION(val->mXValue.GetUnit() == eCSSUnit_Null,
00941                              "not initial state");
00942                 NS_ASSERTION(val->mYValue.GetUnit() == eCSSUnit_Null,
00943                              "not initial state");
00944             } break;
00945 
00946             case eCSSType_ValueList: {
00947                 nsCSSValueList* val = *NS_STATIC_CAST(nsCSSValueList**, prop);
00948                 NS_ASSERTION(val == nsnull, "not initial state");
00949             } break;
00950 
00951             case eCSSType_CounterData: {
00952                 nsCSSCounterData* val =
00953                     *NS_STATIC_CAST(nsCSSCounterData**, prop);
00954                 NS_ASSERTION(val == nsnull, "not initial state");
00955             } break;
00956 
00957             case eCSSType_Quotes: {
00958                 nsCSSQuotes* val = *NS_STATIC_CAST(nsCSSQuotes**, prop);
00959                 NS_ASSERTION(val == nsnull, "not initial state");
00960             } break;
00961 
00962             case eCSSType_Shadow: {
00963                 nsCSSShadow* val = *NS_STATIC_CAST(nsCSSShadow**, prop);
00964                 NS_ASSERTION(val == nsnull, "not initial state");
00965             } break;
00966         }
00967     }
00968 }
00969 #endif