2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-03-25 05:47:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* representation of a declaration block (or style attribute) in a CSS
|
|
|
|
* stylesheet
|
|
|
|
*/
|
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
#ifndef mozilla_css_Declaration_h
|
|
|
|
#define mozilla_css_Declaration_h
|
|
|
|
|
2011-12-16 19:42:07 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
// This header is in EXPORTS because it's used in several places in content/,
|
|
|
|
// but it's not really a public interface.
|
|
|
|
#ifndef _IMPL_NS_LAYOUT
|
|
|
|
#error "This file should only be included within the layout library"
|
|
|
|
#endif
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2010-08-19 19:33:44 +00:00
|
|
|
#include "nsCSSDataBlock.h"
|
|
|
|
#include "nsCSSProperty.h"
|
1999-07-18 00:33:53 +00:00
|
|
|
#include "nsCSSProps.h"
|
2010-08-19 19:33:44 +00:00
|
|
|
#include "nsStringFwd.h"
|
2008-01-10 20:13:24 +00:00
|
|
|
#include "nsTArray.h"
|
2010-08-19 19:33:44 +00:00
|
|
|
#include <stdio.h>
|
2002-01-15 22:43:37 +00:00
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace css {
|
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
// Declaration objects have unusual lifetime rules. Every declaration
|
|
|
|
// begins life in an invalid state which ends when InitializeEmpty or
|
|
|
|
// CompressFrom is called upon it. After that, it can be attached to
|
|
|
|
// exactly one style rule, and will be destroyed when that style rule
|
|
|
|
// is destroyed. A declaration becomes immutable when its style rule's
|
|
|
|
// |RuleMatched| method is called; after that, it must be copied before
|
|
|
|
// it can be modified, which is taken care of by |EnsureMutable|.
|
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
class Declaration {
|
1998-04-13 20:24:54 +00:00
|
|
|
public:
|
2003-06-07 22:14:42 +00:00
|
|
|
/**
|
2010-06-28 22:49:35 +00:00
|
|
|
* Construct an |Declaration| that is in an invalid state (null
|
2003-06-07 22:14:42 +00:00
|
|
|
* |mData|) and cannot be used until its |CompressFrom| method or
|
|
|
|
* |InitializeEmpty| method is called.
|
|
|
|
*/
|
2010-06-28 22:49:35 +00:00
|
|
|
Declaration();
|
2000-04-20 13:00:08 +00:00
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
Declaration(const Declaration& aCopy);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
~Declaration();
|
|
|
|
|
2003-06-07 22:14:42 +00:00
|
|
|
/**
|
|
|
|
* |ValueAppended| must be called to maintain this declaration's
|
|
|
|
* |mOrder| whenever a property is parsed into an expanded data block
|
2009-03-06 04:05:01 +00:00
|
|
|
* for this declaration. aProperty must not be a shorthand.
|
2003-06-07 22:14:42 +00:00
|
|
|
*/
|
2010-07-23 18:00:34 +00:00
|
|
|
void ValueAppended(nsCSSProperty aProperty);
|
1998-06-26 05:51:55 +00:00
|
|
|
|
2010-07-23 18:00:34 +00:00
|
|
|
void RemoveProperty(nsCSSProperty aProperty);
|
2003-06-07 22:14:42 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool HasProperty(nsCSSProperty aProperty) const;
|
2010-11-24 18:31:52 +00:00
|
|
|
|
2010-07-23 18:00:34 +00:00
|
|
|
void GetValue(nsCSSProperty aProperty, nsAString& aValue) const;
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool HasImportantData() const { return mImportantData != nsnull; }
|
|
|
|
bool GetValueIsImportant(nsCSSProperty aProperty) const;
|
|
|
|
bool GetValueIsImportant(const nsAString& aProperty) const;
|
1998-09-25 00:51:45 +00:00
|
|
|
|
2008-01-10 20:13:24 +00:00
|
|
|
PRUint32 Count() const {
|
2010-06-28 22:49:35 +00:00
|
|
|
return mOrder.Length();
|
2008-01-10 20:13:24 +00:00
|
|
|
}
|
2010-07-23 18:00:34 +00:00
|
|
|
void GetNthProperty(PRUint32 aIndex, nsAString& aReturn) const;
|
1998-06-26 05:51:55 +00:00
|
|
|
|
2010-07-23 18:00:34 +00:00
|
|
|
void ToString(nsAString& aString) const;
|
1998-09-10 01:19:26 +00:00
|
|
|
|
2009-12-11 16:13:20 +00:00
|
|
|
nsCSSCompressedDataBlock* GetNormalBlock() const { return mData; }
|
|
|
|
nsCSSCompressedDataBlock* GetImportantBlock() const { return mImportantData; }
|
2003-06-07 22:14:42 +00:00
|
|
|
|
|
|
|
/**
|
2010-07-23 18:00:34 +00:00
|
|
|
* Initialize this declaration as holding no data. Cannot fail.
|
2003-06-07 22:14:42 +00:00
|
|
|
*/
|
2010-07-23 18:00:34 +00:00
|
|
|
void InitializeEmpty();
|
2003-06-07 22:14:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer all of the state from |aExpandedData| into this declaration.
|
|
|
|
* After calling, |aExpandedData| should be in its initial state.
|
2012-02-19 20:49:34 +00:00
|
|
|
* Callers must make sure mOrder is updated as necessary.
|
2003-06-07 22:14:42 +00:00
|
|
|
*/
|
|
|
|
void CompressFrom(nsCSSExpandedDataBlock *aExpandedData) {
|
2010-08-19 19:33:44 +00:00
|
|
|
NS_ABORT_IF_FALSE(!mData, "oops");
|
|
|
|
NS_ABORT_IF_FALSE(!mImportantData, "oops");
|
2010-07-23 18:00:52 +00:00
|
|
|
aExpandedData->Compress(getter_Transfers(mData),
|
|
|
|
getter_Transfers(mImportantData));
|
2009-07-09 21:50:39 +00:00
|
|
|
aExpandedData->AssertInitialState();
|
2003-06-07 22:14:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transfer all of the state from this declaration into
|
|
|
|
* |aExpandedData| and put this declaration temporarily into an
|
|
|
|
* invalid state (ended by |CompressFrom| or |InitializeEmpty|) that
|
|
|
|
* should last only during parsing. During this time only
|
|
|
|
* |ValueAppended| should be called.
|
|
|
|
*/
|
|
|
|
void ExpandTo(nsCSSExpandedDataBlock *aExpandedData) {
|
2010-07-23 18:00:49 +00:00
|
|
|
AssertMutable();
|
2003-06-07 22:14:42 +00:00
|
|
|
aExpandedData->AssertInitialState();
|
|
|
|
|
2010-08-19 19:33:44 +00:00
|
|
|
NS_ABORT_IF_FALSE(mData, "oops");
|
2010-07-23 18:00:52 +00:00
|
|
|
aExpandedData->Expand(mData.forget(), mImportantData.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do what |nsIStyleRule::MapRuleInfoInto| needs to do for a style
|
|
|
|
* rule using this declaration for storage.
|
|
|
|
*/
|
|
|
|
void MapNormalRuleInfoInto(nsRuleData *aRuleData) const {
|
|
|
|
NS_ABORT_IF_FALSE(mData, "called while expanded");
|
|
|
|
mData->MapRuleInfoInto(aRuleData);
|
|
|
|
}
|
|
|
|
void MapImportantRuleInfoInto(nsRuleData *aRuleData) const {
|
|
|
|
NS_ABORT_IF_FALSE(mData, "called while expanded");
|
|
|
|
NS_ABORT_IF_FALSE(mImportantData, "must have important data");
|
|
|
|
mImportantData->MapRuleInfoInto(aRuleData);
|
2003-06-07 22:14:42 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 21:50:39 +00:00
|
|
|
/**
|
2010-08-19 19:33:44 +00:00
|
|
|
* Attempt to replace the value for |aProperty| stored in this
|
|
|
|
* declaration with the matching value from |aFromBlock|.
|
|
|
|
* This method may only be called on a mutable declaration.
|
2011-10-17 14:59:28 +00:00
|
|
|
* It will fail (returning false) if |aProperty| is shorthand,
|
2010-08-19 19:33:44 +00:00
|
|
|
* is not already in this declaration, or does not have the indicated
|
2011-10-17 14:59:28 +00:00
|
|
|
* importance level. If it returns true, it erases the value in
|
|
|
|
* |aFromBlock|. |aChanged| is set to true if the declaration
|
|
|
|
* changed as a result of the call, and to false otherwise.
|
2009-07-09 21:50:39 +00:00
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool TryReplaceValue(nsCSSProperty aProperty, bool aIsImportant,
|
2010-08-19 19:33:44 +00:00
|
|
|
nsCSSExpandedDataBlock& aFromBlock,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aChanged)
|
2010-08-19 19:33:44 +00:00
|
|
|
{
|
2010-07-23 18:00:49 +00:00
|
|
|
AssertMutable();
|
2010-07-23 18:00:39 +00:00
|
|
|
NS_ABORT_IF_FALSE(mData, "called while expanded");
|
|
|
|
|
2009-07-09 21:50:39 +00:00
|
|
|
if (nsCSSProps::IsShorthand(aProperty)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aChanged = false;
|
|
|
|
return false;
|
2009-07-09 21:50:39 +00:00
|
|
|
}
|
2010-07-23 18:00:39 +00:00
|
|
|
nsCSSCompressedDataBlock *block = aIsImportant ? mImportantData : mData;
|
|
|
|
// mImportantData might be null
|
|
|
|
if (!block) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aChanged = false;
|
|
|
|
return false;
|
2010-07-23 18:00:39 +00:00
|
|
|
}
|
2009-07-09 21:50:39 +00:00
|
|
|
|
2010-07-23 18:00:39 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCSSCompressedDataBlock *other = aIsImportant ? mData : mImportantData;
|
2010-08-19 19:33:44 +00:00
|
|
|
NS_ABORT_IF_FALSE(!other || !other->ValueFor(aProperty) ||
|
|
|
|
!block->ValueFor(aProperty),
|
2010-07-23 18:00:39 +00:00
|
|
|
"Property both important and not?");
|
|
|
|
}
|
|
|
|
#endif
|
2010-08-19 19:33:44 +00:00
|
|
|
return block->TryReplaceValue(aProperty, aFromBlock, aChanged);
|
2009-07-09 21:50:39 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool HasNonImportantValueFor(nsCSSProperty aProperty) const {
|
2009-12-11 16:13:19 +00:00
|
|
|
NS_ABORT_IF_FALSE(!nsCSSProps::IsShorthand(aProperty), "must be longhand");
|
2010-08-19 19:33:44 +00:00
|
|
|
return !!mData->ValueFor(aProperty);
|
2009-12-11 16:13:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
/**
|
|
|
|
* Return whether |this| may be modified.
|
|
|
|
*/
|
|
|
|
bool IsMutable() const {
|
|
|
|
return !mImmutable;
|
|
|
|
}
|
|
|
|
|
2009-12-11 16:13:19 +00:00
|
|
|
/**
|
2010-07-23 18:00:49 +00:00
|
|
|
* Copy |this|, if necessary to ensure that it can be modified.
|
2009-12-11 16:13:19 +00:00
|
|
|
*/
|
2010-07-23 18:00:49 +00:00
|
|
|
Declaration* EnsureMutable();
|
|
|
|
|
|
|
|
/**
|
2010-07-23 18:00:52 +00:00
|
|
|
* Crash if |this| cannot be modified.
|
2010-07-23 18:00:49 +00:00
|
|
|
*/
|
|
|
|
void AssertMutable() const {
|
|
|
|
NS_ABORT_IF_FALSE(IsMutable(), "someone forgot to call EnsureMutable");
|
|
|
|
}
|
2009-12-11 16:13:19 +00:00
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
/**
|
2010-08-02 22:40:35 +00:00
|
|
|
* Mark this declaration as unmodifiable. It's 'const' so it can
|
|
|
|
* be called from ToString.
|
2010-07-23 18:00:52 +00:00
|
|
|
*/
|
2011-10-17 14:59:28 +00:00
|
|
|
void SetImmutable() const { mImmutable = true; }
|
2010-07-23 18:00:52 +00:00
|
|
|
|
2003-06-07 22:14:42 +00:00
|
|
|
/**
|
|
|
|
* Clear the data, in preparation for its replacement with entirely
|
|
|
|
* new data by a call to |CompressFrom|.
|
|
|
|
*/
|
|
|
|
void ClearData() {
|
2010-07-23 18:00:49 +00:00
|
|
|
AssertMutable();
|
2003-06-07 22:14:42 +00:00
|
|
|
mData = nsnull;
|
2009-12-11 16:13:19 +00:00
|
|
|
mImportantData = nsnull;
|
2005-04-28 20:19:24 +00:00
|
|
|
mOrder.Clear();
|
2003-06-07 22:14:42 +00:00
|
|
|
}
|
|
|
|
|
2001-10-16 05:31:36 +00:00
|
|
|
#ifdef DEBUG
|
2002-01-15 22:43:37 +00:00
|
|
|
void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
|
2001-10-16 05:31:36 +00:00
|
|
|
#endif
|
2010-06-28 22:49:35 +00:00
|
|
|
|
2002-01-15 22:43:37 +00:00
|
|
|
private:
|
2011-12-16 19:42:07 +00:00
|
|
|
Declaration& operator=(const Declaration& aCopy) MOZ_DELETE;
|
|
|
|
bool operator==(const Declaration& aCopy) const MOZ_DELETE;
|
2002-01-15 22:43:37 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static void AppendImportanceToString(bool aIsImportant, nsAString& aString);
|
2003-06-07 22:14:42 +00:00
|
|
|
// return whether there was a value in |aValue| (i.e., it had a non-null unit)
|
2011-09-29 06:19:26 +00:00
|
|
|
bool AppendValueToString(nsCSSProperty aProperty, nsAString& aResult) const;
|
2008-12-23 14:06:57 +00:00
|
|
|
// Helper for ToString with strange semantics regarding aValue.
|
2010-07-23 18:00:52 +00:00
|
|
|
void AppendPropertyAndValueToString(nsCSSProperty aProperty,
|
|
|
|
nsAutoString& aValue,
|
|
|
|
nsAString& aResult) const;
|
2010-06-28 22:49:35 +00:00
|
|
|
|
2011-04-23 01:36:23 +00:00
|
|
|
public:
|
2008-01-10 20:13:24 +00:00
|
|
|
nsCSSProperty OrderValueAt(PRUint32 aValue) const {
|
|
|
|
return nsCSSProperty(mOrder.ElementAt(aValue));
|
2003-06-07 22:14:42 +00:00
|
|
|
}
|
2002-01-15 22:43:37 +00:00
|
|
|
|
2012-01-03 02:19:14 +00:00
|
|
|
size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
|
|
|
|
|
2011-04-23 01:36:23 +00:00
|
|
|
private:
|
2010-07-23 18:00:52 +00:00
|
|
|
nsAutoTArray<PRUint8, 8> mOrder;
|
2010-07-23 18:00:49 +00:00
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
// never null, except while expanded, or before the first call to
|
|
|
|
// InitializeEmpty or CompressFrom.
|
|
|
|
nsAutoPtr<nsCSSCompressedDataBlock> mData;
|
2009-12-11 16:13:19 +00:00
|
|
|
|
2010-07-23 18:00:52 +00:00
|
|
|
// may be null
|
|
|
|
nsAutoPtr<nsCSSCompressedDataBlock> mImportantData;
|
2009-12-11 16:13:19 +00:00
|
|
|
|
2010-08-02 22:40:35 +00:00
|
|
|
// set by style rules when |RuleMatched| is called;
|
|
|
|
// also by ToString (hence the 'mutable').
|
2011-09-29 06:19:26 +00:00
|
|
|
mutable bool mImmutable;
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
} // namespace css
|
2010-06-28 22:49:35 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2010-06-28 22:49:35 +00:00
|
|
|
#endif /* mozilla_css_Declaration_h */
|