CSSVariableValues.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /* computed CSS Variable values */
  5. #ifndef mozilla_CSSVariableValues_h
  6. #define mozilla_CSSVariableValues_h
  7. #include "nsCSSScanner.h"
  8. #include "nsDataHashtable.h"
  9. #include "nsTArray.h"
  10. namespace mozilla {
  11. class CSSVariableResolver;
  12. class CSSVariableValues
  13. {
  14. public:
  15. CSSVariableValues();
  16. CSSVariableValues(const CSSVariableValues& aOther);
  17. #ifdef DEBUG
  18. ~CSSVariableValues();
  19. #endif
  20. CSSVariableValues& operator=(const CSSVariableValues& aOther);
  21. bool operator==(const CSSVariableValues& aOther) const;
  22. bool operator!=(const CSSVariableValues& aOther) const
  23. { return !(*this == aOther); }
  24. /**
  25. * Gets the value of a variable in this set of computed variables.
  26. *
  27. * @param aName The variable name (not including any "--" prefix that would
  28. * be part of the custom property name).
  29. * @param aValue Out parameter into which the value of the variable will
  30. * be stored.
  31. * @return Whether a variable with the given name was found. When false
  32. * is returned, aValue will not be modified.
  33. */
  34. bool Get(const nsAString& aName, nsString& aValue) const;
  35. /**
  36. * Gets the value of a variable in this set of computed variables, along
  37. * with information on the types of tokens that appear at the start and
  38. * end of the token stream.
  39. *
  40. * @param aName The variable name (not including any "--" prefix that would
  41. * be part of the custom property name).
  42. * @param aValue Out parameter into which the value of the variable will
  43. * be stored.
  44. * @param aFirstToken The type of token at the start of the variable value.
  45. * @param aLastToken The type of token at the en of the variable value.
  46. * @return Whether a variable with the given name was found. When false
  47. * is returned, aValue, aFirstToken and aLastToken will not be modified.
  48. */
  49. bool Get(const nsAString& aName,
  50. nsString& aValue,
  51. nsCSSTokenSerializationType& aFirstToken,
  52. nsCSSTokenSerializationType& aLastToken) const;
  53. /**
  54. * Gets the name of the variable at the given index.
  55. *
  56. * Variables on this object are ordered, and that order is just determined
  57. * based on the order that they are added to the object. A consistent
  58. * ordering is required for CSSDeclaration objects in the DOM.
  59. * CSSDeclarations expose property names as indexed properties, which need to
  60. * be stable.
  61. *
  62. * @param aIndex The index of the variable to get.
  63. * @param aName Out parameter into which the name of the variable will be
  64. * stored.
  65. */
  66. void GetVariableAt(size_t aIndex, nsAString& aName) const;
  67. /**
  68. * Gets the number of variables stored on this object.
  69. */
  70. size_t Count() const;
  71. /**
  72. * Adds or modifies an existing entry in this set of variable values.
  73. *
  74. * @param aName The variable name (not including any "--" prefix that would
  75. * be part of the custom property name) whose value is to be set.
  76. * @param aValue The variable value.
  77. * @param aFirstToken The type of token at the start of the variable value.
  78. * @param aLastToken The type of token at the en of the variable value.
  79. */
  80. void Put(const nsAString& aName,
  81. nsString aValue,
  82. nsCSSTokenSerializationType aFirstToken,
  83. nsCSSTokenSerializationType aLastToken);
  84. /**
  85. * Copies the variables from this object into aResolver, marking them as
  86. * computed, inherited values.
  87. */
  88. void AddVariablesToResolver(CSSVariableResolver* aResolver) const;
  89. private:
  90. struct Variable
  91. {
  92. Variable()
  93. : mFirstToken(eCSSTokenSerialization_Nothing)
  94. , mLastToken(eCSSTokenSerialization_Nothing)
  95. {}
  96. Variable(const nsAString& aVariableName,
  97. nsString aValue,
  98. nsCSSTokenSerializationType aFirstToken,
  99. nsCSSTokenSerializationType aLastToken)
  100. : mVariableName(aVariableName)
  101. , mValue(aValue)
  102. , mFirstToken(aFirstToken)
  103. , mLastToken(aLastToken)
  104. {}
  105. nsString mVariableName;
  106. nsString mValue;
  107. nsCSSTokenSerializationType mFirstToken;
  108. nsCSSTokenSerializationType mLastToken;
  109. };
  110. /**
  111. * Adds all the variables from aOther into this object.
  112. */
  113. void CopyVariablesFrom(const CSSVariableValues& aOther);
  114. /**
  115. * Map of variable names to IDs. Variable IDs are indexes into
  116. * mVariables.
  117. */
  118. nsDataHashtable<nsStringHashKey, size_t> mVariableIDs;
  119. /**
  120. * Array of variables, indexed by variable ID.
  121. */
  122. nsTArray<Variable> mVariables;
  123. };
  124. } // namespace mozilla
  125. #endif