Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / layout / style / CSSVariableResolver.h
blob01de94f6fc712a1cf058af3d89e3bd5459572e85
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
7 * values
8 */
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"
17 #include "nsTArray.h"
19 namespace mozilla {
21 class CSSVariableDeclarations;
22 class CSSVariableValues;
23 class EnumerateVariableReferencesData;
25 class CSSVariableResolver
27 friend class CSSVariableDeclarations;
28 friend class CSSVariableValues;
29 friend class EnumerateVariableReferencesData;
30 public:
31 /**
32 * Creates a new CSSVariableResolver that will output a set of resolved,
33 * computed variables into aOutput.
35 explicit CSSVariableResolver(CSSVariableValues* aOutput)
36 : mOutput(aOutput)
37 #ifdef DEBUG
38 , mResolved(false)
39 #endif
41 MOZ_ASSERT(aOutput);
44 /**
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);
52 private:
53 struct Variable
55 Variable(const nsAString& aVariableName,
56 nsString aValue,
57 nsCSSTokenSerializationType aFirstToken,
58 nsCSSTokenSerializationType aLastToken,
59 bool aWasInherited)
60 : mVariableName(aVariableName)
61 , mValue(aValue)
62 , mFirstToken(aFirstToken)
63 , mLastToken(aLastToken)
64 , mWasInherited(aWasInherited)
65 , mResolved(false)
66 , mReferencesNonExistentVariable(false)
67 , mInStack(false)
68 , mIndex(0)
69 , mLowLink(0) { }
71 nsString mVariableName;
72 nsString mValue;
73 nsCSSTokenSerializationType mFirstToken;
74 nsCSSTokenSerializationType mLastToken;
76 // Whether this variable came from the set of inherited variables.
77 bool mWasInherited;
79 // Whether this variable has been resolved yet.
80 bool mResolved;
82 // Whether this variables includes any references to non-existent variables.
83 bool mReferencesNonExistentVariable;
85 // Bookkeeping for the cycle remover algorithm.
86 bool mInStack;
87 size_t mIndex;
88 size_t mLowLink;
91 /**
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
95 * Resolve.
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
103 * variables.
105 void Put(const nsAString& aVariableName,
106 nsString aValue,
107 nsCSSTokenSerializationType aFirstToken,
108 nsCSSTokenSerializationType aLastToken,
109 bool aWasInherited);
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
116 // and mReferences.
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.
127 size_t mNextIndex;
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.
135 nsCSSParser mParser;
137 // The object to output the resolved variables into.
138 CSSVariableValues* mOutput;
140 #ifdef DEBUG
141 // Whether Resolve has been called.
142 bool mResolved;
143 #endif
148 #endif