gecko/layout/style/CSSVariableDeclarations.h
Cameron McCormack 589b3b4774 Bug 773296 - Part 8: Resolve and compute CSS variables. r=dbaron
We add a new class CSSVariableResolver whose job is to take the
inherited computed variables and the specified variable declarations and
to perform cycle removal and resolution of the variables, storing the
result in the CSSVariableValues object on an nsStyleVariables.  We use
CSSVariableResolver in nsRuleNode::ComputeVariablesData.

The variable resolver does this:

  1. Asks the CSSVariableValues and CSSVariableDeclarations objects
     to add their variables to it.
  2. Calls in to a new nsCSSParser function
     EnumerateVariableReferences that informs the resolver which
     other variables a given variable references, and by doing so,
     builds a graph of variable dependencies.
  3. Removes variables involved in cyclic references using Tarjan's
     strongly connected component algorithm, setting those variables
     to have an invalid value.
  4. Calls in to a new nsCSSParser function ResolveVariableValue
     to resolve the remaining valid variables by substituting variable
     references.

We extend nsCSSParser::ParseValueWithVariables to take a callback
function to be invoked when encountering a variable reference.  This
lets EnumerateVariableReferences re-use ParseValueWithVariables.

CSSParserImpl::ResolveValueWithVariableReferences needs different
error handling behaviour from ParseValueWithVariables, so we don't
re-use it.

CSSParserImpl::AppendImpliedEOFCharacters is used to take the
value returned from nsCSSScanner::GetImpliedEOFCharacters while
resolving variable references that were declared using custom
properties that encountered EOF before being closed properly.

The SeparatorRequiredBetweenTokens helper function in nsCSSParser.cpp
implements the serialization rules in CSS Syntax Module Level 3:

https://dvcs.w3.org/hg/csswg/raw-file/3479cdefc59a/css-syntax/Overview.html#serialization
2013-12-12 13:09:41 +11:00

141 lines
4.7 KiB
C++

/* 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/. */
/* CSS Custom Property assignments for a Declaration at a given priority */
#ifndef mozilla_CSSVariableDeclarations_h
#define mozilla_CSSVariableDeclarations_h
#include "nsDataHashtable.h"
namespace mozilla {
class CSSVariableResolver;
}
class nsRuleData;
namespace mozilla {
class CSSVariableDeclarations
{
public:
CSSVariableDeclarations();
CSSVariableDeclarations(const CSSVariableDeclarations& aOther);
#ifdef DEBUG
~CSSVariableDeclarations();
#endif
CSSVariableDeclarations& operator=(const CSSVariableDeclarations& aOther);
/**
* Returns whether this set of variable declarations includes a variable
* with a given name.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name).
*/
bool Has(const nsAString& aName) const;
/**
* Represents the type of a variable value.
*/
enum Type {
eTokenStream, // a stream of CSS tokens (the usual type for variables)
eInitial, // 'initial'
eInherit // 'inherit'
};
/**
* Gets the value of a variable in this set of variable declarations.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name).
* @param aType Out parameter into which the type of the variable value will
* be stored.
* @param aValue Out parameter into which the value of the variable will
* be stored. If the variable is 'initial' or 'inherit', this will be
* the empty string.
* @return Whether a variable with the given name was found. When false
* is returned, aType and aValue will not be modified.
*/
bool Get(const nsAString& aName, Type& aType, nsString& aValue) const;
/**
* Adds or modifies an existing entry in this set of variable declarations
* to have the value 'initial'.
*
* @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.
*/
void PutInitial(const nsAString& aName);
/**
* Adds or modifies an existing entry in this set of variable declarations
* to have the value 'inherit'.
*
* @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.
*/
void PutInherit(const nsAString& aName);
/**
* Adds or modifies an existing entry in this set of variable declarations
* to have a token stream value.
*
* @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 aTokenStream The CSS token stream.
*/
void PutTokenStream(const nsAString& aName, const nsString& aTokenStream);
/**
* Removes an entry in this set of variable declarations.
*
* @param aName The variable name (not including any "var-" prefix that would
* be part of the custom property name) whose entry is to be removed.
*/
void Remove(const nsAString& aName);
/**
* Returns the number of entries in this set of variable declarations.
*/
uint32_t Count() const { return mVariables.Count(); }
/**
* Copies each variable value from this object into aRuleData, unless that
* variable already exists on aRuleData.
*/
void MapRuleInfoInto(nsRuleData* aRuleData);
/**
* Copies the variables from this object into aResolver, marking them as
* specified values.
*/
void AddVariablesToResolver(CSSVariableResolver* aResolver) const;
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
private:
/**
* Adds all the variable declarations from aOther into this object.
*/
void CopyVariablesFrom(const CSSVariableDeclarations& aOther);
static PLDHashOperator EnumerateVariableForCopy(const nsAString& aName,
nsString aValue,
void* aData);
static PLDHashOperator
EnumerateVariableForMapRuleInfoInto(const nsAString& aName,
nsString aValue,
void* aData);
static PLDHashOperator
EnumerateVariableForAddVariablesToResolver(const nsAString& aName,
nsString aValue,
void* aData);
nsDataHashtable<nsStringHashKey, nsString> mVariables;
};
} // namespace mozilla
#endif