2003-06-08 02:14:42 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is nsCSSDataBlock.h.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is L. David Baron.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* L. David Baron <dbaron@dbaron.org> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2006-03-25 08:47:31 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* compact representation of the property-value pairs within a CSS
|
|
|
|
* declaration, and the code for expanding and compacting it
|
|
|
|
*/
|
|
|
|
|
2003-06-08 02:14:42 +04:00
|
|
|
#ifndef nsCSSDataBlock_h__
|
|
|
|
#define nsCSSDataBlock_h__
|
|
|
|
|
|
|
|
#include "nsCSSStruct.h"
|
2009-02-06 19:52:13 +03:00
|
|
|
#include "nsCSSProps.h"
|
2009-09-11 14:46:36 +04:00
|
|
|
#include "nsCSSPropertySet.h"
|
2009-12-11 19:13:19 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
struct nsRuleData;
|
|
|
|
class nsCSSExpandedDataBlock;
|
2010-06-29 02:49:35 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace css {
|
|
|
|
class Declaration;
|
|
|
|
}
|
|
|
|
}
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
2009-07-10 01:50:39 +04:00
|
|
|
* An |nsCSSCompressedDataBlock| holds a usually-immutable chunk of
|
2003-06-08 02:14:42 +04:00
|
|
|
* property-value data for a CSS declaration block (which we misname a
|
2010-06-29 02:49:35 +04:00
|
|
|
* |css::Declaration|). Mutation is accomplished through
|
2009-07-10 01:50:39 +04:00
|
|
|
* |nsCSSExpandedDataBlock| or in some cases via direct slot access.
|
2009-12-11 19:13:19 +03:00
|
|
|
*
|
|
|
|
* Mutation is forbidden when the reference count is greater than one,
|
|
|
|
* since once a style rule has used a compressed data block, mutation of
|
|
|
|
* that block is forbidden, and any declarations that want to mutate it
|
|
|
|
* need to clone it first.
|
2003-06-08 02:14:42 +04:00
|
|
|
*/
|
|
|
|
class nsCSSCompressedDataBlock {
|
|
|
|
public:
|
|
|
|
friend class nsCSSExpandedDataBlock;
|
2010-06-29 02:49:35 +04:00
|
|
|
friend class mozilla::css::Declaration;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do what |nsIStyleRule::MapRuleInfoInto| needs to do for a style
|
|
|
|
* rule using this block for storage.
|
|
|
|
*/
|
2010-05-20 06:28:00 +04:00
|
|
|
void MapRuleInfoInto(nsRuleData *aRuleData) const;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the location at which the *value* for the property is
|
|
|
|
* stored, or null if the block does not contain a value for the
|
|
|
|
* property. This is either an |nsCSSValue*|, |nsCSSRect*|, or an
|
|
|
|
* |nsCSSValueList**|, etc.
|
|
|
|
*
|
|
|
|
* Inefficient (by design).
|
2009-07-10 01:50:39 +04:00
|
|
|
*
|
|
|
|
* Must not be called for shorthands.
|
2003-06-08 02:14:42 +04:00
|
|
|
*/
|
|
|
|
const void* StorageFor(nsCSSProperty aProperty) const;
|
|
|
|
|
2009-02-06 19:52:13 +03:00
|
|
|
/**
|
|
|
|
* A set of slightly more typesafe helpers for the above. All
|
|
|
|
* return null if the value is not present.
|
|
|
|
*/
|
|
|
|
const nsCSSValue* ValueStorageFor(nsCSSProperty aProperty) const {
|
|
|
|
NS_ABORT_IF_FALSE(nsCSSProps::kTypeTable[aProperty] == eCSSType_Value,
|
|
|
|
"type mismatch");
|
|
|
|
return static_cast<const nsCSSValue*>(StorageFor(aProperty));
|
|
|
|
}
|
|
|
|
const nsCSSRect* RectStorageFor(nsCSSProperty aProperty) const {
|
|
|
|
NS_ABORT_IF_FALSE(nsCSSProps::kTypeTable[aProperty] == eCSSType_Rect,
|
|
|
|
"type mismatch");
|
|
|
|
return static_cast<const nsCSSRect*>(StorageFor(aProperty));
|
|
|
|
}
|
|
|
|
const nsCSSValuePair* ValuePairStorageFor(nsCSSProperty aProperty) const {
|
|
|
|
NS_ABORT_IF_FALSE(nsCSSProps::kTypeTable[aProperty] ==
|
|
|
|
eCSSType_ValuePair,
|
|
|
|
"type mismatch");
|
|
|
|
return static_cast<const nsCSSValuePair*>(StorageFor(aProperty));
|
|
|
|
}
|
|
|
|
const nsCSSValueList*const*
|
|
|
|
ValueListStorageFor(nsCSSProperty aProperty) const {
|
|
|
|
NS_ABORT_IF_FALSE(nsCSSProps::kTypeTable[aProperty] ==
|
|
|
|
eCSSType_ValueList,
|
|
|
|
"type mismatch");
|
|
|
|
return static_cast<const nsCSSValueList*const*>(StorageFor(aProperty));
|
|
|
|
}
|
|
|
|
const nsCSSValuePairList*const*
|
|
|
|
ValuePairListStorageFor(nsCSSProperty aProperty) const {
|
|
|
|
NS_ABORT_IF_FALSE(nsCSSProps::kTypeTable[aProperty] ==
|
|
|
|
eCSSType_ValuePairList,
|
|
|
|
"type mismatch");
|
|
|
|
return static_cast<const nsCSSValuePairList*const*>(
|
|
|
|
StorageFor(aProperty));
|
|
|
|
}
|
|
|
|
|
2003-06-08 02:14:42 +04:00
|
|
|
/**
|
|
|
|
* Clone this block, or return null on out-of-memory.
|
|
|
|
*/
|
2009-12-11 19:13:19 +03:00
|
|
|
already_AddRefed<nsCSSCompressedDataBlock> Clone() const;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new nsCSSCompressedDataBlock holding no declarations.
|
|
|
|
*/
|
2009-12-11 19:13:19 +03:00
|
|
|
static already_AddRefed<nsCSSCompressedDataBlock> CreateEmptyBlock();
|
|
|
|
|
|
|
|
void AddRef() {
|
|
|
|
NS_ASSERTION(mRefCnt == 0 || mRefCnt == 1,
|
|
|
|
"unexpected reference count");
|
|
|
|
++mRefCnt;
|
|
|
|
}
|
|
|
|
void Release() {
|
|
|
|
NS_ASSERTION(mRefCnt == 1 || mRefCnt == 2,
|
|
|
|
"unexpected reference count");
|
|
|
|
if (--mRefCnt == 0) {
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsMutable() const {
|
|
|
|
NS_ASSERTION(mRefCnt == 1 || mRefCnt == 2,
|
|
|
|
"unexpected reference count");
|
|
|
|
return mRefCnt < 2;
|
|
|
|
}
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
PRInt32 mStyleBits; // the structs for which we have data, according to
|
|
|
|
// |nsCachedStyleData::GetBitForSID|.
|
2009-12-11 19:13:19 +03:00
|
|
|
nsAutoRefCnt mRefCnt;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
enum { block_chars = 4 }; // put 4 chars in the definition of the class
|
|
|
|
// to ensure size not inflated by alignment
|
|
|
|
|
|
|
|
void* operator new(size_t aBaseSize, size_t aDataSize) {
|
|
|
|
// subtract off the extra size to store |mBlock_|
|
|
|
|
return ::operator new(aBaseSize + aDataSize -
|
|
|
|
sizeof(char) * block_chars);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSCompressedDataBlock() : mStyleBits(0) {}
|
|
|
|
|
|
|
|
// Only this class (through |Destroy|) or nsCSSExpandedDataBlock (in
|
|
|
|
// |Expand|) can delete compressed data blocks.
|
|
|
|
~nsCSSCompressedDataBlock() { }
|
|
|
|
|
2009-12-11 19:13:19 +03:00
|
|
|
/**
|
|
|
|
* Delete all the data stored in this block, and the block itself.
|
|
|
|
*/
|
|
|
|
void Destroy();
|
|
|
|
|
2003-06-08 02:14:42 +04:00
|
|
|
char* mBlockEnd; // the byte after the last valid byte
|
|
|
|
char mBlock_[block_chars]; // must be the last member!
|
|
|
|
|
|
|
|
char* Block() { return mBlock_; }
|
|
|
|
char* BlockEnd() { return mBlockEnd; }
|
|
|
|
const char* Block() const { return mBlock_; }
|
|
|
|
const char* BlockEnd() const { return mBlockEnd; }
|
|
|
|
ptrdiff_t DataSize() const { return BlockEnd() - Block(); }
|
2009-07-10 01:50:39 +04:00
|
|
|
|
|
|
|
// Direct slot access to our values. See StorageFor above. Can
|
|
|
|
// return null. Must not be called for shorthand properties.
|
|
|
|
void* SlotForValue(nsCSSProperty aProperty) {
|
2009-12-11 19:13:19 +03:00
|
|
|
NS_ABORT_IF_FALSE(IsMutable(), "must be mutable");
|
2009-07-10 01:50:39 +04:00
|
|
|
return const_cast<void*>(StorageFor(aProperty));
|
|
|
|
}
|
2003-06-08 02:14:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsCSSExpandedDataBlock {
|
|
|
|
public:
|
|
|
|
nsCSSExpandedDataBlock();
|
|
|
|
~nsCSSExpandedDataBlock();
|
|
|
|
/*
|
|
|
|
* When setting properties in an |nsCSSExpandedDataBlock|, callers
|
|
|
|
* must make the appropriate |AddPropertyBit| call.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsCSSFont mFont;
|
|
|
|
nsCSSDisplay mDisplay;
|
|
|
|
nsCSSMargin mMargin;
|
|
|
|
nsCSSList mList;
|
|
|
|
nsCSSPosition mPosition;
|
|
|
|
nsCSSTable mTable;
|
|
|
|
nsCSSColor mColor;
|
|
|
|
nsCSSContent mContent;
|
|
|
|
nsCSSText mText;
|
|
|
|
nsCSSUserInterface mUserInterface;
|
|
|
|
nsCSSAural mAural;
|
|
|
|
nsCSSPage mPage;
|
|
|
|
nsCSSBreaks mBreaks;
|
|
|
|
nsCSSXUL mXUL;
|
|
|
|
nsCSSSVG mSVG;
|
2004-06-17 15:57:37 +04:00
|
|
|
nsCSSColumn mColumn;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer all of the state from the compressed block to this
|
|
|
|
* expanded block. The state of this expanded block must be clear
|
|
|
|
* beforehand.
|
|
|
|
*
|
2009-12-11 19:13:19 +03:00
|
|
|
* The compressed block passed in IS RELEASED by this method and
|
2003-06-08 02:14:42 +04:00
|
|
|
* set to null, and thus cannot be used again. (This is necessary
|
|
|
|
* because ownership of sub-objects is transferred to the expanded
|
2009-12-11 19:13:19 +03:00
|
|
|
* block in many cases.)
|
2003-06-08 02:14:42 +04:00
|
|
|
*/
|
2009-12-11 19:13:19 +03:00
|
|
|
void Expand(nsRefPtr<nsCSSCompressedDataBlock> *aNormalBlock,
|
|
|
|
nsRefPtr<nsCSSCompressedDataBlock> *aImportantBlock);
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a new compressed block and transfer all of the state
|
|
|
|
* from this expanded block to the new compressed block, clearing
|
|
|
|
* the state of this expanded block.
|
|
|
|
*/
|
|
|
|
void Compress(nsCSSCompressedDataBlock **aNormalBlock,
|
|
|
|
nsCSSCompressedDataBlock **aImportantBlock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear (and thus destroy) the state of this expanded block.
|
|
|
|
*/
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the data for the given property (including the set and
|
|
|
|
* important bits).
|
|
|
|
*/
|
|
|
|
void ClearProperty(nsCSSProperty aPropID);
|
|
|
|
|
|
|
|
void AssertInitialState() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
DoAssertInitialState();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Compute the size that will be occupied by the result of
|
|
|
|
* |Compress|.
|
|
|
|
*/
|
|
|
|
struct ComputeSizeResult {
|
|
|
|
PRUint32 normal, important;
|
|
|
|
};
|
|
|
|
ComputeSizeResult ComputeSize();
|
|
|
|
|
2009-12-11 19:13:19 +03:00
|
|
|
void DoExpand(nsRefPtr<nsCSSCompressedDataBlock> *aBlock,
|
|
|
|
PRBool aImportant);
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void DoAssertInitialState();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct PropertyOffsetInfo {
|
|
|
|
// XXX These could probably be pointer-to-member, if the casting can
|
|
|
|
// be done correctly.
|
|
|
|
size_t block_offset; // offset of value in nsCSSExpandedDataBlock
|
|
|
|
size_t ruledata_struct_offset; // offset of nsRuleData* in nsRuleData
|
|
|
|
size_t ruledata_member_offset; // offset of value in nsRuleData*
|
|
|
|
};
|
|
|
|
|
|
|
|
static const PropertyOffsetInfo kOffsetTable[];
|
|
|
|
|
|
|
|
/*
|
2005-04-29 00:19:24 +04:00
|
|
|
* mPropertiesSet stores a bit for every property that is present,
|
|
|
|
* to optimize compression of blocks with small numbers of
|
|
|
|
* properties (the norm) and to allow quickly checking whether a
|
|
|
|
* property is set in this block.
|
2003-06-08 02:14:42 +04:00
|
|
|
*/
|
2009-09-11 14:46:36 +04:00
|
|
|
nsCSSPropertySet mPropertiesSet;
|
2003-06-08 02:14:42 +04:00
|
|
|
/*
|
|
|
|
* mPropertiesImportant indicates which properties are '!important'.
|
|
|
|
*/
|
2009-09-11 14:46:36 +04:00
|
|
|
nsCSSPropertySet mPropertiesImportant;
|
2003-06-08 02:14:42 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* Return the storage location within |this| of the value of the
|
|
|
|
* property (i.e., either an |nsCSSValue*|, |nsCSSRect*|, or
|
|
|
|
* |nsCSSValueList**| (etc.).
|
|
|
|
*/
|
|
|
|
void* PropertyAt(nsCSSProperty aProperty) {
|
|
|
|
const PropertyOffsetInfo& offsets =
|
|
|
|
nsCSSExpandedDataBlock::kOffsetTable[aProperty];
|
2007-07-08 11:08:04 +04:00
|
|
|
return reinterpret_cast<void*>(reinterpret_cast<char*>(this) +
|
2003-06-08 02:14:42 +04:00
|
|
|
offsets.block_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the storage location within |aRuleData| of the value of
|
|
|
|
* the property (i.e., either an |nsCSSValue*|, |nsCSSRect*|, or
|
|
|
|
* |nsCSSValueList**| (etc.).
|
|
|
|
*/
|
|
|
|
static void* RuleDataPropertyAt(nsRuleData *aRuleData,
|
|
|
|
nsCSSProperty aProperty) {
|
|
|
|
const PropertyOffsetInfo& offsets =
|
|
|
|
nsCSSExpandedDataBlock::kOffsetTable[aProperty];
|
|
|
|
NS_ASSERTION(offsets.ruledata_struct_offset != size_t(-1),
|
|
|
|
"property should not use CSS_PROP_BACKENDONLY");
|
2007-07-08 11:08:04 +04:00
|
|
|
char* cssstruct = *reinterpret_cast<char**>
|
|
|
|
(reinterpret_cast<char*>(aRuleData) +
|
2003-06-08 02:14:42 +04:00
|
|
|
offsets.ruledata_struct_offset);
|
2007-07-08 11:08:04 +04:00
|
|
|
return reinterpret_cast<void*>
|
|
|
|
(cssstruct + offsets.ruledata_member_offset);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetPropertyBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
mPropertiesSet.AddProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearPropertyBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
mPropertiesSet.RemoveProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool HasPropertyBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
return mPropertiesSet.HasProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetImportantBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
mPropertiesImportant.AddProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearImportantBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
mPropertiesImportant.RemoveProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool HasImportantBit(nsCSSProperty aProperty) {
|
2009-09-11 14:46:36 +04:00
|
|
|
return mPropertiesImportant.HasProperty(aProperty);
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearSets() {
|
2009-09-11 14:46:36 +04:00
|
|
|
mPropertiesSet.Empty();
|
|
|
|
mPropertiesImportant.Empty();
|
2003-06-08 02:14:42 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* !defined(nsCSSDataBlock_h__) */
|