1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/style/CSSVariableResolver.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* object that resolves CSS variables using specified and inherited variable 1.10 + * values 1.11 + */ 1.12 + 1.13 +#ifndef mozilla_CSSVariableResolver_h 1.14 +#define mozilla_CSSVariableResolver_h 1.15 + 1.16 +#include "mozilla/DebugOnly.h" 1.17 +#include "nsCSSParser.h" 1.18 +#include "nsCSSScanner.h" 1.19 +#include "nsDataHashtable.h" 1.20 +#include "nsTArray.h" 1.21 + 1.22 +namespace mozilla { 1.23 + 1.24 +class CSSVariableDeclarations; 1.25 +class CSSVariableValues; 1.26 +class EnumerateVariableReferencesData; 1.27 + 1.28 +class CSSVariableResolver 1.29 +{ 1.30 + friend class CSSVariableDeclarations; 1.31 + friend class CSSVariableValues; 1.32 + friend class EnumerateVariableReferencesData; 1.33 +public: 1.34 + /** 1.35 + * Creates a new CSSVariableResolver that will output a set of resolved, 1.36 + * computed variables into aOutput. 1.37 + */ 1.38 + CSSVariableResolver(CSSVariableValues* aOutput) 1.39 + : mOutput(aOutput) 1.40 + , mResolved(false) 1.41 + { 1.42 + MOZ_ASSERT(aOutput); 1.43 + } 1.44 + 1.45 + /** 1.46 + * Resolves the set of inherited variables from aInherited and the 1.47 + * set of specified variables from aSpecified. The resoled variables 1.48 + * are written in to mOutput. 1.49 + */ 1.50 + void Resolve(const CSSVariableValues* aInherited, 1.51 + const CSSVariableDeclarations* aSpecified); 1.52 + 1.53 +private: 1.54 + struct Variable 1.55 + { 1.56 + Variable(const nsAString& aVariableName, 1.57 + nsString aValue, 1.58 + nsCSSTokenSerializationType aFirstToken, 1.59 + nsCSSTokenSerializationType aLastToken, 1.60 + bool aWasInherited) 1.61 + : mVariableName(aVariableName) 1.62 + , mValue(aValue) 1.63 + , mFirstToken(aFirstToken) 1.64 + , mLastToken(aLastToken) 1.65 + , mWasInherited(aWasInherited) 1.66 + , mResolved(false) 1.67 + , mReferencesNonExistentVariable(false) 1.68 + , mInStack(false) 1.69 + , mIndex(0) 1.70 + , mLowLink(0) { } 1.71 + 1.72 + nsString mVariableName; 1.73 + nsString mValue; 1.74 + nsCSSTokenSerializationType mFirstToken; 1.75 + nsCSSTokenSerializationType mLastToken; 1.76 + 1.77 + // Whether this variable came from the set of inherited variables. 1.78 + bool mWasInherited; 1.79 + 1.80 + // Whether this variable has been resolved yet. 1.81 + bool mResolved; 1.82 + 1.83 + // Whether this variables includes any references to non-existent variables. 1.84 + bool mReferencesNonExistentVariable; 1.85 + 1.86 + // Bookkeeping for the cycle remover algorithm. 1.87 + bool mInStack; 1.88 + size_t mIndex; 1.89 + size_t mLowLink; 1.90 + }; 1.91 + 1.92 + /** 1.93 + * Adds or modifies an existing entry in the set of variables to be resolved. 1.94 + * This is intended to be called by the AddVariablesToResolver functions on 1.95 + * the CSSVariableDeclarations and CSSVariableValues objects passed in to 1.96 + * Resolve. 1.97 + * 1.98 + * @param aName The variable name (not including any "--" prefix that would 1.99 + * be part of the custom property name) whose value is to be set. 1.100 + * @param aValue The variable value. 1.101 + * @param aFirstToken The type of token at the start of the variable value. 1.102 + * @param aLastToken The type of token at the en of the variable value. 1.103 + * @param aWasInherited Whether this variable came from the set of inherited 1.104 + * variables. 1.105 + */ 1.106 + void Put(const nsAString& aVariableName, 1.107 + nsString aValue, 1.108 + nsCSSTokenSerializationType aFirstToken, 1.109 + nsCSSTokenSerializationType aLastToken, 1.110 + bool aWasInherited); 1.111 + 1.112 + // Helper functions for Resolve. 1.113 + void RemoveCycles(size_t aID); 1.114 + void ResolveVariable(size_t aID); 1.115 + 1.116 + // A mapping of variable names to an ID that indexes into mVariables 1.117 + // and mReferences. 1.118 + nsDataHashtable<nsStringHashKey, size_t> mVariableIDs; 1.119 + 1.120 + // The set of variables. 1.121 + nsTArray<Variable> mVariables; 1.122 + 1.123 + // The list of variables that each variable references. 1.124 + nsTArray<nsTArray<size_t> > mReferences; 1.125 + 1.126 + // The next index to assign to a variable found during the cycle removing 1.127 + // algorithm's traversal of the variable reference graph. 1.128 + size_t mNextIndex; 1.129 + 1.130 + // Stack of variable IDs that we push to as we traverse the variable reference 1.131 + // graph while looking for cycles. Variable::mInStack reflects whether a 1.132 + // given variable has its ID in mStack. 1.133 + nsTArray<size_t> mStack; 1.134 + 1.135 + // CSS parser to use for parsing property values with variable references. 1.136 + nsCSSParser mParser; 1.137 + 1.138 + // The object to output the resolved variables into. 1.139 + CSSVariableValues* mOutput; 1.140 + 1.141 + // Whether Resolve has been called. 1.142 + DebugOnly<bool> mResolved; 1.143 +}; 1.144 + 1.145 +} 1.146 + 1.147 +#endif