Bug 773296 - Part 7: Add a field to nsStyleVariables to store computed variable values. r=dbaron

This defines a class CSSVariableValues which is used to store
computed variable values.  We store them a bit differently from
CSSVariableDeclarations -- here we have a hash table of variable names
to integer IDs, and then an array of variables where the array index
is the ID.  This is because later on we'll want a stable order for the
variables to return from DOM APIs.

In addition to the string value of the variable, we store the type
of the first and last token of the variable value.  This information
will be used when resolving entire variable reference containing
values, to determine when to insert "/**/" before and after a resolved
var(blah) token.

We add a CSSVariableValues member to nsStyleVariables.
This commit is contained in:
Cameron McCormack 2013-12-12 13:09:41 +11:00
parent 274a25e67e
commit 02059bc249
4 changed files with 219 additions and 1 deletions

View File

@ -0,0 +1,99 @@
/* -*- 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/. */
/* computed CSS Variable values */
#include "CSSVariableValues.h"
namespace mozilla {
CSSVariableValues::CSSVariableValues()
{
MOZ_COUNT_CTOR(CSSVariableValues);
}
CSSVariableValues::CSSVariableValues(const CSSVariableValues& aOther)
{
MOZ_COUNT_CTOR(CSSVariableValues);
CopyVariablesFrom(aOther);
}
#ifdef DEBUG
CSSVariableValues::~CSSVariableValues()
{
MOZ_COUNT_DTOR(CSSVariableValues);
}
#endif
CSSVariableValues&
CSSVariableValues::operator=(const CSSVariableValues& aOther)
{
if (this == &aOther) {
return *this;
}
mVariableIDs.Clear();
mVariables.Clear();
CopyVariablesFrom(aOther);
return *this;
}
bool
CSSVariableValues::Get(const nsAString& aName, nsString& aValue) const
{
size_t id;
if (!mVariableIDs.Get(aName, &id)) {
return false;
}
aValue = mVariables[id].mValue;
return true;
}
bool
CSSVariableValues::Get(const nsAString& aName,
nsString& aValue,
nsCSSTokenSerializationType& aFirstToken,
nsCSSTokenSerializationType& aLastToken) const
{
size_t id;
if (!mVariableIDs.Get(aName, &id)) {
return false;
}
aValue = mVariables[id].mValue;
aFirstToken = mVariables[id].mFirstToken;
aLastToken = mVariables[id].mLastToken;
return true;
}
void
CSSVariableValues::Put(const nsAString& aName,
nsString aValue,
nsCSSTokenSerializationType aFirstToken,
nsCSSTokenSerializationType aLastToken)
{
size_t id;
if (mVariableIDs.Get(aName, &id)) {
mVariables[id].mValue = aValue;
mVariables[id].mFirstToken = aFirstToken;
mVariables[id].mLastToken = aLastToken;
} else {
id = mVariables.Length();
mVariableIDs.Put(aName, id);
mVariables.AppendElement(Variable(aName, aValue, aFirstToken, aLastToken));
}
}
void
CSSVariableValues::CopyVariablesFrom(const CSSVariableValues& aOther)
{
for (size_t i = 0, n = aOther.mVariables.Length(); i < n; i++) {
Put(aOther.mVariables[i].mVariableName,
aOther.mVariables[i].mValue,
aOther.mVariables[i].mFirstToken,
aOther.mVariables[i].mLastToken);
}
}
} // namespace mozilla

View File

@ -0,0 +1,115 @@
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
/* 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/. */
/* computed CSS Variable values */
#ifndef mozilla_CSSVariableValues_h
#define mozilla_CSSVariableValues_h
#include "nsCSSScanner.h"
#include "nsDataHashtable.h"
#include "nsTArray.h"
namespace mozilla {
class CSSVariableValues
{
public:
CSSVariableValues();
CSSVariableValues(const CSSVariableValues& aOther);
#ifdef DEBUG
~CSSVariableValues();
#endif
CSSVariableValues& operator=(const CSSVariableValues& aOther);
/**
* Gets the value of a variable in this set of computed variables.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name).
* @param aValue Out parameter into which the value of the variable will
* be stored.
* @return Whether a variable with the given name was found. When false
* is returned, aValue will not be modified.
*/
bool Get(const nsAString& aName, nsString& aValue) const;
/**
* Gets the value of a variable in this set of computed variables, along
* with information on the types of tokens that appear at the start and
* end of the token stream.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name).
* @param aValue Out parameter into which the value of the variable will
* be stored.
* @param aFirstToken The type of token at the start of the variable value.
* @param aLastToken The type of token at the en of the variable value.
* @return Whether a variable with the given name was found. When false
* is returned, aValue, aFirstToken and aLastToken will not be modified.
*/
bool Get(const nsAString& aName,
nsString& aValue,
nsCSSTokenSerializationType& aFirstToken,
nsCSSTokenSerializationType& aLastToken) const;
/**
* Adds or modifies an existing entry in this set of variable values.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name) whose value is to be set.
* @param aValue The variable value.
* @param aFirstToken The type of token at the start of the variable value.
* @param aLastToken The type of token at the en of the variable value.
*/
void Put(const nsAString& aName,
nsString aValue,
nsCSSTokenSerializationType aFirstToken,
nsCSSTokenSerializationType aLastToken);
private:
struct Variable
{
Variable()
: mFirstToken(eCSSTokenSerialization_Nothing)
, mLastToken(eCSSTokenSerialization_Nothing)
{}
Variable(const nsAString& aVariableName,
nsString aValue,
nsCSSTokenSerializationType aFirstToken,
nsCSSTokenSerializationType aLastToken)
: mVariableName(aVariableName)
, mValue(aValue)
, mFirstToken(aFirstToken)
, mLastToken(aLastToken)
{}
nsString mVariableName;
nsString mValue;
nsCSSTokenSerializationType mFirstToken;
nsCSSTokenSerializationType mLastToken;
};
/**
* Adds all the variables from aOther into this object.
*/
void CopyVariablesFrom(const CSSVariableValues& aOther);
/**
* Map of variable names to IDs. Variable IDs are indexes into
* mVariables.
*/
nsDataHashtable<nsStringHashKey, size_t> mVariableIDs;
/**
* Array of variables, indexed by variable ID.
*/
nsTArray<Variable> mVariables;
};
} // namespace mozilla
#endif

View File

@ -60,6 +60,7 @@ EXPORTS += [
EXPORTS.mozilla += [
'CSSVariableDeclarations.h',
'CSSVariableValues.h',
]
EXPORTS.mozilla.dom += [
@ -83,6 +84,7 @@ UNIFIED_SOURCES += [
'AnimationCommon.cpp',
'CSS.cpp',
'CSSVariableDeclarations.cpp',
'CSSVariableValues.cpp',
'Declaration.cpp',
'ErrorReporter.cpp',
'ImageLoader.cpp',

View File

@ -12,7 +12,7 @@
#define nsStyleStruct_h___
#include "mozilla/Attributes.h"
#include "mozilla/CSSVariableValues.h"
#include "nsColor.h"
#include "nsCoord.h"
#include "nsMargin.h"
@ -2519,6 +2519,8 @@ struct nsStyleVariables {
static nsChangeHint MaxDifference() {
return nsChangeHint(0);
}
mozilla::CSSVariableValues mVariables;
};
#endif /* nsStyleStruct_h___ */