Files
firefox/layout/style/CSSVariableDeclarations.cpp
T
Cameron McCormack ffbf813968 Bug 773296 - Part 5: Map variables on a Declaration to nsRuleData. r=dbaron
This adds a CSSVariableDeclarations object to nsRuleData and adds a
MapRuleInfoInto function to CSSVariableDeclarations so the can copy
variable declarations into a nsRuleData's object.  We call that from
Declaration::Map{Normal,Important}RuleInfoInto.

We make HasImportantData return true if we have important variables
but no important non-custom properties on a declaration, since that
is used to determine whether we have a rule node for important
declarations.  This means MapImportantRuleInfoInto can no longer
assume that mImportantData is non-null.
2013-12-12 13:09:41 +11:00

172 lines
4.5 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
/* CSS Custom Property assignments for a Declaration at a given priority */
#include "CSSVariableDeclarations.h"
#include "nsRuleData.h"
// These two special string values are used to represent specified values of
// 'initial' and 'inherit'. (Note that neither is a valid variable value.)
#define INITIAL_VALUE "!"
#define INHERIT_VALUE ";"
namespace mozilla {
CSSVariableDeclarations::CSSVariableDeclarations()
{
MOZ_COUNT_CTOR(CSSVariableDeclarations);
}
CSSVariableDeclarations::CSSVariableDeclarations(const CSSVariableDeclarations& aOther)
{
MOZ_COUNT_CTOR(CSSVariableDeclarations);
CopyVariablesFrom(aOther);
}
#ifdef DEBUG
CSSVariableDeclarations::~CSSVariableDeclarations()
{
MOZ_COUNT_DTOR(CSSVariableDeclarations);
}
#endif
CSSVariableDeclarations&
CSSVariableDeclarations::operator=(const CSSVariableDeclarations& aOther)
{
if (this == &aOther) {
return *this;
}
mVariables.Clear();
CopyVariablesFrom(aOther);
return *this;
}
/* static */ PLDHashOperator
CSSVariableDeclarations::EnumerateVariableForCopy(const nsAString& aName,
nsString aValue,
void* aData)
{
CSSVariableDeclarations* variables = static_cast<CSSVariableDeclarations*>(aData);
variables->mVariables.Put(aName, aValue);
return PL_DHASH_NEXT;
}
void
CSSVariableDeclarations::CopyVariablesFrom(const CSSVariableDeclarations& aOther)
{
aOther.mVariables.EnumerateRead(EnumerateVariableForCopy, this);
}
bool
CSSVariableDeclarations::Has(const nsAString& aName) const
{
nsString value;
return mVariables.Get(aName, &value);
}
bool
CSSVariableDeclarations::Get(const nsAString& aName,
Type& aType,
nsString& aTokenStream) const
{
nsString value;
if (!mVariables.Get(aName, &value)) {
return false;
}
if (value.EqualsLiteral(INITIAL_VALUE)) {
aType = eInitial;
aTokenStream.Truncate();
} else if (value.EqualsLiteral(INHERIT_VALUE)) {
aType = eInitial;
aTokenStream.Truncate();
} else {
aType = eTokenStream;
aTokenStream = value;
}
return true;
}
void
CSSVariableDeclarations::PutTokenStream(const nsAString& aName,
const nsString& aTokenStream)
{
MOZ_ASSERT(!aTokenStream.EqualsLiteral(INITIAL_VALUE) &&
!aTokenStream.EqualsLiteral(INHERIT_VALUE));
mVariables.Put(aName, aTokenStream);
}
void
CSSVariableDeclarations::PutInitial(const nsAString& aName)
{
mVariables.Put(aName, NS_LITERAL_STRING(INITIAL_VALUE));
}
void
CSSVariableDeclarations::PutInherit(const nsAString& aName)
{
mVariables.Put(aName, NS_LITERAL_STRING(INHERIT_VALUE));
}
void
CSSVariableDeclarations::Remove(const nsAString& aName)
{
mVariables.Remove(aName);
}
/* static */ PLDHashOperator
CSSVariableDeclarations::EnumerateVariableForMapRuleInfoInto(
const nsAString& aName,
nsString aValue,
void* aData)
{
nsDataHashtable<nsStringHashKey, nsString>* variables =
static_cast<nsDataHashtable<nsStringHashKey, nsString>*>(aData);
if (!variables->Contains(aName)) {
variables->Put(aName, aValue);
}
return PL_DHASH_NEXT;
}
void
CSSVariableDeclarations::MapRuleInfoInto(nsRuleData* aRuleData)
{
if (!(aRuleData->mSIDs & NS_STYLE_INHERIT_BIT(Variables))) {
return;
}
if (!aRuleData->mVariables) {
aRuleData->mVariables = new CSSVariableDeclarations(*this);
} else {
mVariables.EnumerateRead(EnumerateVariableForMapRuleInfoInto,
aRuleData->mVariables.get());
}
}
static size_t
SizeOfTableEntry(const nsAString& aKey,
const nsString& aValue,
MallocSizeOf aMallocSizeOf,
void* aUserArg)
{
size_t n = 0;
n += aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
n += aValue.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
return n;
}
size_t
CSSVariableDeclarations::SizeOfIncludingThis(
mozilla::MallocSizeOf aMallocSizeOf) const
{
size_t n = aMallocSizeOf(this);
n += mVariables.SizeOfExcludingThis(SizeOfTableEntry, aMallocSizeOf);
return n;
}
} // namespace mozilla