1 /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* object that resolves CSS variables using specified and inherited variable
10 #ifndef mozilla_CSSVariableResolver_h
11 #define mozilla_CSSVariableResolver_h
13 #include "mozilla/DebugOnly.h"
14 #include "nsCSSParser.h"
15 #include "nsCSSScanner.h"
16 #include "nsDataHashtable.h"
21 class CSSVariableDeclarations
;
22 class CSSVariableValues
;
23 class EnumerateVariableReferencesData
;
25 class CSSVariableResolver
27 friend class CSSVariableDeclarations
;
28 friend class CSSVariableValues
;
29 friend class EnumerateVariableReferencesData
;
32 * Creates a new CSSVariableResolver that will output a set of resolved,
33 * computed variables into aOutput.
35 explicit CSSVariableResolver(CSSVariableValues
* aOutput
)
45 * Resolves the set of inherited variables from aInherited and the
46 * set of specified variables from aSpecified. The resoled variables
47 * are written in to mOutput.
49 void Resolve(const CSSVariableValues
* aInherited
,
50 const CSSVariableDeclarations
* aSpecified
);
55 Variable(const nsAString
& aVariableName
,
57 nsCSSTokenSerializationType aFirstToken
,
58 nsCSSTokenSerializationType aLastToken
,
60 : mVariableName(aVariableName
)
62 , mFirstToken(aFirstToken
)
63 , mLastToken(aLastToken
)
64 , mWasInherited(aWasInherited
)
66 , mReferencesNonExistentVariable(false)
71 nsString mVariableName
;
73 nsCSSTokenSerializationType mFirstToken
;
74 nsCSSTokenSerializationType mLastToken
;
76 // Whether this variable came from the set of inherited variables.
79 // Whether this variable has been resolved yet.
82 // Whether this variables includes any references to non-existent variables.
83 bool mReferencesNonExistentVariable
;
85 // Bookkeeping for the cycle remover algorithm.
92 * Adds or modifies an existing entry in the set of variables to be resolved.
93 * This is intended to be called by the AddVariablesToResolver functions on
94 * the CSSVariableDeclarations and CSSVariableValues objects passed in to
97 * @param aName The variable name (not including any "--" prefix that would
98 * be part of the custom property name) whose value is to be set.
99 * @param aValue The variable value.
100 * @param aFirstToken The type of token at the start of the variable value.
101 * @param aLastToken The type of token at the en of the variable value.
102 * @param aWasInherited Whether this variable came from the set of inherited
105 void Put(const nsAString
& aVariableName
,
107 nsCSSTokenSerializationType aFirstToken
,
108 nsCSSTokenSerializationType aLastToken
,
111 // Helper functions for Resolve.
112 void RemoveCycles(size_t aID
);
113 void ResolveVariable(size_t aID
);
115 // A mapping of variable names to an ID that indexes into mVariables
117 nsDataHashtable
<nsStringHashKey
, size_t> mVariableIDs
;
119 // The set of variables.
120 nsTArray
<Variable
> mVariables
;
122 // The list of variables that each variable references.
123 nsTArray
<nsTArray
<size_t> > mReferences
;
125 // The next index to assign to a variable found during the cycle removing
126 // algorithm's traversal of the variable reference graph.
129 // Stack of variable IDs that we push to as we traverse the variable reference
130 // graph while looking for cycles. Variable::mInStack reflects whether a
131 // given variable has its ID in mStack.
132 nsTArray
<size_t> mStack
;
134 // CSS parser to use for parsing property values with variable references.
137 // The object to output the resolved variables into.
138 CSSVariableValues
* mOutput
;
141 // Whether Resolve has been called.