CSSVariableResolver.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* object that resolves CSS variables using specified and inherited variable
  5. * values
  6. */
  7. #ifndef mozilla_CSSVariableResolver_h
  8. #define mozilla_CSSVariableResolver_h
  9. #include "mozilla/DebugOnly.h"
  10. #include "nsCSSParser.h"
  11. #include "nsCSSScanner.h"
  12. #include "nsDataHashtable.h"
  13. #include "nsTArray.h"
  14. namespace mozilla {
  15. class CSSVariableDeclarations;
  16. class CSSVariableValues;
  17. class EnumerateVariableReferencesData;
  18. class CSSVariableResolver
  19. {
  20. friend class CSSVariableDeclarations;
  21. friend class CSSVariableValues;
  22. friend class EnumerateVariableReferencesData;
  23. public:
  24. /**
  25. * Creates a new CSSVariableResolver that will output a set of resolved,
  26. * computed variables into aOutput.
  27. */
  28. explicit CSSVariableResolver(CSSVariableValues* aOutput)
  29. : mOutput(aOutput)
  30. #ifdef DEBUG
  31. , mResolved(false)
  32. #endif
  33. {
  34. MOZ_ASSERT(aOutput);
  35. }
  36. /**
  37. * Resolves the set of inherited variables from aInherited and the
  38. * set of specified variables from aSpecified. The resolved variables
  39. * are written into mOutput.
  40. */
  41. void Resolve(const CSSVariableValues* aInherited,
  42. const CSSVariableDeclarations* aSpecified);
  43. private:
  44. struct Variable
  45. {
  46. Variable(const nsAString& aVariableName,
  47. nsString aValue,
  48. nsCSSTokenSerializationType aFirstToken,
  49. nsCSSTokenSerializationType aLastToken,
  50. bool aWasInherited)
  51. : mVariableName(aVariableName)
  52. , mValue(aValue)
  53. , mFirstToken(aFirstToken)
  54. , mLastToken(aLastToken)
  55. , mWasInherited(aWasInherited)
  56. , mResolved(false)
  57. , mReferencesNonExistentVariable(false)
  58. , mInStack(false)
  59. , mIndex(0)
  60. , mLowLink(0) { }
  61. nsString mVariableName;
  62. nsString mValue;
  63. nsCSSTokenSerializationType mFirstToken;
  64. nsCSSTokenSerializationType mLastToken;
  65. // Whether this variable came from the set of inherited variables.
  66. bool mWasInherited;
  67. // Whether this variable has been resolved yet.
  68. bool mResolved;
  69. // Whether this variables includes any references to non-existent variables.
  70. bool mReferencesNonExistentVariable;
  71. // Bookkeeping for the cycle remover algorithm.
  72. bool mInStack;
  73. size_t mIndex;
  74. size_t mLowLink;
  75. };
  76. /**
  77. * Adds or modifies an existing entry in the set of variables to be resolved.
  78. * This is intended to be called by the AddVariablesToResolver functions on
  79. * the CSSVariableDeclarations and CSSVariableValues objects passed in to
  80. * Resolve.
  81. *
  82. * @param aName The variable name (not including any "--" prefix that would
  83. * be part of the custom property name) whose value is to be set.
  84. * @param aValue The variable value.
  85. * @param aFirstToken The type of token at the start of the variable value.
  86. * @param aLastToken The type of token at the en of the variable value.
  87. * @param aWasInherited Whether this variable came from the set of inherited
  88. * variables.
  89. */
  90. void Put(const nsAString& aVariableName,
  91. nsString aValue,
  92. nsCSSTokenSerializationType aFirstToken,
  93. nsCSSTokenSerializationType aLastToken,
  94. bool aWasInherited);
  95. // Helper functions for Resolve.
  96. void RemoveCycles(size_t aID);
  97. void ResolveVariable(size_t aID);
  98. // A mapping of variable names to an ID that indexes into mVariables
  99. // and mReferences.
  100. nsDataHashtable<nsStringHashKey, size_t> mVariableIDs;
  101. // The set of variables.
  102. nsTArray<Variable> mVariables;
  103. // The list of variables that each variable references.
  104. nsTArray<nsTArray<size_t> > mReferences;
  105. // The next index to assign to a variable found during the cycle removing
  106. // algorithm's traversal of the variable reference graph.
  107. size_t mNextIndex;
  108. // Stack of variable IDs that we push to as we traverse the variable reference
  109. // graph while looking for cycles. Variable::mInStack reflects whether a
  110. // given variable has its ID in mStack.
  111. nsTArray<size_t> mStack;
  112. // CSS parser to use for parsing property values with variable references.
  113. nsCSSParser mParser;
  114. // The object to output the resolved variables into.
  115. CSSVariableValues* mOutput;
  116. #ifdef DEBUG
  117. // Whether Resolve has been called.
  118. bool mResolved;
  119. #endif
  120. };
  121. } // namespace mozilla
  122. #endif