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