Line data Source code
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/. */
5 :
6 : /* object that resolves CSS variables using specified and inherited variable
7 : * values
8 : */
9 :
10 : #ifndef mozilla_CSSVariableResolver_h
11 : #define mozilla_CSSVariableResolver_h
12 :
13 : #include "mozilla/DebugOnly.h"
14 : #include "nsCSSParser.h"
15 : #include "nsCSSScanner.h"
16 : #include "nsDataHashtable.h"
17 : #include "nsTArray.h"
18 :
19 : namespace mozilla {
20 :
21 : class CSSVariableDeclarations;
22 : class CSSVariableValues;
23 : class EnumerateVariableReferencesData;
24 :
25 12 : class CSSVariableResolver
26 : {
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.
34 : */
35 12 : explicit CSSVariableResolver(CSSVariableValues* aOutput)
36 12 : : mOutput(aOutput)
37 : #ifdef DEBUG
38 12 : , mResolved(false)
39 : #endif
40 : {
41 12 : MOZ_ASSERT(aOutput);
42 12 : }
43 :
44 : /**
45 : * Resolves the set of inherited variables from aInherited and the
46 : * set of specified variables from aSpecified. The resolved variables
47 : * are written into mOutput.
48 : */
49 : void Resolve(const CSSVariableValues* aInherited,
50 : const CSSVariableDeclarations* aSpecified);
51 :
52 : private:
53 1461 : struct Variable
54 : {
55 487 : Variable(const nsAString& aVariableName,
56 : nsString aValue,
57 : nsCSSTokenSerializationType aFirstToken,
58 : nsCSSTokenSerializationType aLastToken,
59 : bool aWasInherited)
60 487 : : 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 487 : , mLowLink(0) { }
70 :
71 : nsString mVariableName;
72 : nsString mValue;
73 : nsCSSTokenSerializationType mFirstToken;
74 : nsCSSTokenSerializationType mLastToken;
75 :
76 : // Whether this variable came from the set of inherited variables.
77 : bool mWasInherited;
78 :
79 : // Whether this variable has been resolved yet.
80 : bool mResolved;
81 :
82 : // Whether this variables includes any references to non-existent variables.
83 : bool mReferencesNonExistentVariable;
84 :
85 : // Bookkeeping for the cycle remover algorithm.
86 : bool mInStack;
87 : size_t mIndex;
88 : size_t mLowLink;
89 : };
90 :
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.
96 : *
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.
104 : */
105 : void Put(const nsAString& aVariableName,
106 : nsString aValue,
107 : nsCSSTokenSerializationType aFirstToken,
108 : nsCSSTokenSerializationType aLastToken,
109 : bool aWasInherited);
110 :
111 : // Helper functions for Resolve.
112 : void RemoveCycles(size_t aID);
113 : void ResolveVariable(size_t aID);
114 :
115 : // A mapping of variable names to an ID that indexes into mVariables
116 : // and mReferences.
117 : nsDataHashtable<nsStringHashKey, size_t> mVariableIDs;
118 :
119 : // The set of variables.
120 : nsTArray<Variable> mVariables;
121 :
122 : // The list of variables that each variable references.
123 : nsTArray<nsTArray<size_t> > mReferences;
124 :
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;
128 :
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;
133 :
134 : // CSS parser to use for parsing property values with variable references.
135 : nsCSSParser mParser;
136 :
137 : // The object to output the resolved variables into.
138 : CSSVariableValues* mOutput;
139 :
140 : #ifdef DEBUG
141 : // Whether Resolve has been called.
142 : bool mResolved;
143 : #endif
144 : };
145 :
146 : } // namespace mozilla
147 :
148 : #endif
|