nsROCSSPrimitiveValue.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. /* DOM object representing values in DOM computed style */
  6. #ifndef nsROCSSPrimitiveValue_h___
  7. #define nsROCSSPrimitiveValue_h___
  8. #include "nsIDOMCSSValue.h"
  9. #include "nsIDOMCSSPrimitiveValue.h"
  10. #include "nsCSSKeywords.h"
  11. #include "CSSValue.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsCoord.h"
  14. class nsIURI;
  15. class nsDOMCSSRect;
  16. class nsDOMCSSRGBColor;
  17. // There is no CSS_TURN constant on the CSSPrimitiveValue interface,
  18. // since that unit is newer than DOM Level 2 Style, and CSS OM will
  19. // probably expose CSS values in some other way in the future. We
  20. // use this value in mType for "turn"-unit angles, but we define it
  21. // here to avoid exposing it to content.
  22. #define CSS_TURN 30U
  23. // Likewise we have some internal aliases for CSS_NUMBER that we don't
  24. // want to expose.
  25. #define CSS_NUMBER_INT32 31U
  26. #define CSS_NUMBER_UINT32 32U
  27. /**
  28. * Read-only CSS primitive value - a DOM object representing values in DOM
  29. * computed style.
  30. */
  31. class nsROCSSPrimitiveValue final : public mozilla::dom::CSSValue,
  32. public nsIDOMCSSPrimitiveValue
  33. {
  34. public:
  35. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  36. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(nsROCSSPrimitiveValue, mozilla::dom::CSSValue)
  37. // nsIDOMCSSPrimitiveValue
  38. NS_DECL_NSIDOMCSSPRIMITIVEVALUE
  39. // nsIDOMCSSValue
  40. NS_DECL_NSIDOMCSSVALUE
  41. // CSSValue
  42. virtual void GetCssText(nsString& aText, mozilla::ErrorResult& aRv) override final;
  43. virtual void SetCssText(const nsAString& aText, mozilla::ErrorResult& aRv) override final;
  44. virtual uint16_t CssValueType() const override final;
  45. // CSSPrimitiveValue
  46. uint16_t PrimitiveType()
  47. {
  48. // New value types were introduced but not added to CSS OM.
  49. // Return CSS_UNKNOWN to avoid exposing CSS_TURN to content.
  50. if (mType > CSS_RGBCOLOR) {
  51. if (mType == CSS_NUMBER_INT32 || mType == CSS_NUMBER_UINT32) {
  52. return CSS_NUMBER;
  53. }
  54. return CSS_UNKNOWN;
  55. }
  56. return mType;
  57. }
  58. void SetFloatValue(uint16_t aUnitType, float aValue,
  59. mozilla::ErrorResult& aRv);
  60. float GetFloatValue(uint16_t aUnitType, mozilla::ErrorResult& aRv);
  61. void GetStringValue(nsString& aString, mozilla::ErrorResult& aRv);
  62. void SetStringValue(uint16_t aUnitType, const nsAString& aString,
  63. mozilla::ErrorResult& aRv);
  64. already_AddRefed<nsIDOMCounter> GetCounterValue(mozilla::ErrorResult& aRv);
  65. nsDOMCSSRect* GetRectValue(mozilla::ErrorResult& aRv);
  66. nsDOMCSSRGBColor *GetRGBColorValue(mozilla::ErrorResult& aRv);
  67. // nsROCSSPrimitiveValue
  68. nsROCSSPrimitiveValue();
  69. void SetNumber(float aValue);
  70. void SetNumber(int32_t aValue);
  71. void SetNumber(uint32_t aValue);
  72. void SetPercent(float aValue);
  73. void SetDegree(float aValue);
  74. void SetGrad(float aValue);
  75. void SetRadian(float aValue);
  76. void SetTurn(float aValue);
  77. void SetAppUnits(nscoord aValue);
  78. void SetAppUnits(float aValue);
  79. void SetIdent(nsCSSKeyword aKeyword);
  80. // FIXME: CSS_STRING should imply a string with "" and a need for escaping.
  81. void SetString(const nsACString& aString, uint16_t aType = CSS_STRING);
  82. // FIXME: CSS_STRING should imply a string with "" and a need for escaping.
  83. void SetString(const nsAString& aString, uint16_t aType = CSS_STRING);
  84. void SetURI(nsIURI *aURI);
  85. void SetColor(nsDOMCSSRGBColor* aColor);
  86. void SetRect(nsDOMCSSRect* aRect);
  87. void SetTime(float aValue);
  88. void Reset();
  89. nsISupports* GetParentObject() const
  90. {
  91. return nullptr;
  92. }
  93. virtual JSObject *WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
  94. private:
  95. ~nsROCSSPrimitiveValue();
  96. uint16_t mType;
  97. union {
  98. nscoord mAppUnits;
  99. float mFloat;
  100. int32_t mInt32;
  101. uint32_t mUint32;
  102. // These can't be nsCOMPtr/nsRefPtr's because they are used inside a union.
  103. nsDOMCSSRGBColor* MOZ_OWNING_REF mColor;
  104. nsDOMCSSRect* MOZ_OWNING_REF mRect;
  105. char16_t* mString;
  106. nsIURI* MOZ_OWNING_REF mURI;
  107. nsCSSKeyword mKeyword;
  108. } mValue;
  109. };
  110. inline nsROCSSPrimitiveValue *mozilla::dom::CSSValue::AsPrimitiveValue()
  111. {
  112. return CssValueType() == nsIDOMCSSValue::CSS_PRIMITIVE_VALUE ?
  113. static_cast<nsROCSSPrimitiveValue*>(this) : nullptr;
  114. }
  115. #endif /* nsROCSSPrimitiveValue_h___ */