1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8  * Generic representation of a container of specified CSS values, which
9  * could potentially be Servo- or Gecko- format. Used to make attribute mapping
10  * code generic over style backends.
11  */
12 
13 #ifndef mozilla_GenericSpecifiedValues_h
14 #define mozilla_GenericSpecifiedValues_h
15 
16 #include "mozilla/ServoUtils.h"
17 #include "nsCSSProps.h"
18 #include "nsCSSValue.h"
19 #include "StyleBackendType.h"
20 
21 class nsAttrValue;
22 struct nsRuleData;
23 
24 namespace mozilla {
25 
26 class ServoSpecifiedValues;
27 
28 // This provides a common interface for attribute mappers
29 // (MapAttributesIntoRule) to use regardless of the style backend. If the style
30 // backend is Gecko, this will contain an nsRuleData. If it is Servo, it will be
31 // a PropertyDeclarationBlock.
32 class GenericSpecifiedValues {
33  protected:
GenericSpecifiedValues(StyleBackendType aType,nsIDocument * aDoc,uint32_t aSIDs)34   explicit GenericSpecifiedValues(StyleBackendType aType, nsIDocument* aDoc,
35                                   uint32_t aSIDs)
36       : mType(aType), mDocument(aDoc), mSIDs(aSIDs) {}
37 
38  public:
MOZ_DECL_STYLO_METHODS(nsRuleData,ServoSpecifiedValues)39   MOZ_DECL_STYLO_METHODS(nsRuleData, ServoSpecifiedValues)
40 
41   nsIDocument* Document() { return mDocument; }
42 
43   // Whether we should ignore document colors.
44   inline bool ShouldIgnoreColors() const;
45 
46   // Check if we already contain a certain longhand
47   inline bool PropertyIsSet(nsCSSPropertyID aId);
48 
49   // Check if we are able to hold longhands from a given
50   // style struct. Pass the result of NS_STYLE_INHERIT_BIT to this
51   // function. Can accept multiple inherit bits or'd together.
ShouldComputeStyleStruct(uint64_t aInheritBits)52   inline bool ShouldComputeStyleStruct(uint64_t aInheritBits) {
53     return aInheritBits & mSIDs;
54   }
55 
56   // Set a property to an identifier (string)
57   inline void SetIdentStringValue(nsCSSPropertyID aId, const nsString& aValue);
58   inline void SetIdentStringValueIfUnset(nsCSSPropertyID aId,
59                                          const nsString& aValue);
60 
61   inline void SetIdentAtomValue(nsCSSPropertyID aId, nsAtom* aValue);
62   inline void SetIdentAtomValueIfUnset(nsCSSPropertyID aId, nsAtom* aValue);
63 
64   // Set a property to a keyword (usually NS_STYLE_* or StyleFoo::*)
65   inline void SetKeywordValue(nsCSSPropertyID aId, int32_t aValue);
66   inline void SetKeywordValueIfUnset(nsCSSPropertyID aId, int32_t aValue);
67 
68   template <typename T,
69             typename = typename std::enable_if<std::is_enum<T>::value>::type>
SetKeywordValue(nsCSSPropertyID aId,T aValue)70   void SetKeywordValue(nsCSSPropertyID aId, T aValue) {
71     static_assert(mozilla::EnumTypeFitsWithin<T, int32_t>::value,
72                   "aValue must be an enum that fits within 32 bits");
73     SetKeywordValue(aId, static_cast<int32_t>(aValue));
74   }
75   template <typename T,
76             typename = typename std::enable_if<std::is_enum<T>::value>::type>
SetKeywordValueIfUnset(nsCSSPropertyID aId,T aValue)77   void SetKeywordValueIfUnset(nsCSSPropertyID aId, T aValue) {
78     static_assert(mozilla::EnumTypeFitsWithin<T, int32_t>::value,
79                   "aValue must be an enum that fits within 32 bits");
80     SetKeywordValueIfUnset(aId, static_cast<int32_t>(aValue));
81   }
82 
83   // Set a property to an integer value
84   inline void SetIntValue(nsCSSPropertyID aId, int32_t aValue);
85   // Set a property to a pixel value
86   inline void SetPixelValue(nsCSSPropertyID aId, float aValue);
87   inline void SetPixelValueIfUnset(nsCSSPropertyID aId, float aValue);
88 
89   inline void SetLengthValue(nsCSSPropertyID aId, nsCSSValue aValue);
90 
91   // Set a property to a number value
92   inline void SetNumberValue(nsCSSPropertyID aId, float aValue);
93 
94   // Set a property to a percent value
95   inline void SetPercentValue(nsCSSPropertyID aId, float aValue);
96   inline void SetPercentValueIfUnset(nsCSSPropertyID aId, float aValue);
97 
98   // Set a property to `auto`
99   inline void SetAutoValue(nsCSSPropertyID aId);
100   inline void SetAutoValueIfUnset(nsCSSPropertyID aId);
101 
102   // Set a property to `currentcolor`
103   inline void SetCurrentColor(nsCSSPropertyID aId);
104   inline void SetCurrentColorIfUnset(nsCSSPropertyID aId);
105 
106   // Set a property to an RGBA nscolor value
107   inline void SetColorValue(nsCSSPropertyID aId, nscolor aValue);
108   inline void SetColorValueIfUnset(nsCSSPropertyID aId, nscolor aValue);
109 
110   // Set font-family to a string
111   inline void SetFontFamily(const nsString& aValue);
112   // Add a quirks-mode override to the decoration color of elements nested in
113   // <a>
114   inline void SetTextDecorationColorOverride();
115   inline void SetBackgroundImage(nsAttrValue& value);
116 
117   const mozilla::StyleBackendType mType;
118   nsIDocument* const mDocument;
119   const uint32_t mSIDs;
120 };
121 
122 }  // namespace mozilla
123 
124 #endif  // mozilla_GenericSpecifiedValues_h
125