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/. */
7 /* Representation of a declaration block used for attribute mapping */
9 #ifndef mozilla_MappedDeclarations_h
10 #define mozilla_MappedDeclarations_h
12 #include "mozilla/FontPropertyTypes.h"
13 #include "mozilla/ServoBindingTypes.h"
14 #include "mozilla/ServoBindings.h"
15 #include "nsCSSPropertyID.h"
16 #include "nsCSSValue.h"
23 // This provides a convenient interface for attribute mappers
24 // (MapAttributesIntoRule) to modify the presentation attribute declaration
25 // block for a given element.
26 class MappedDeclarations final
{
28 explicit MappedDeclarations(dom::Document
* aDoc
,
29 already_AddRefed
<RawServoDeclarationBlock
> aDecls
)
30 : mDocument(aDoc
), mDecl(aDecls
) {
34 ~MappedDeclarations() { MOZ_ASSERT(!mDecl
, "Forgot to take the block?"); }
36 dom::Document
* Document() { return mDocument
; }
38 already_AddRefed
<RawServoDeclarationBlock
> TakeDeclarationBlock() {
40 return mDecl
.forget();
43 // Check if we already contain a certain longhand
44 bool PropertyIsSet(nsCSSPropertyID aId
) const {
45 return Servo_DeclarationBlock_PropertyIsSet(mDecl
, aId
);
48 // Set a property to an identifier (string)
49 void SetIdentStringValue(nsCSSPropertyID aId
, const nsString
& aValue
) {
50 RefPtr
<nsAtom
> atom
= NS_AtomizeMainThread(aValue
);
51 SetIdentAtomValue(aId
, atom
);
54 void SetIdentStringValueIfUnset(nsCSSPropertyID aId
, const nsString
& aValue
) {
55 if (!PropertyIsSet(aId
)) {
56 SetIdentStringValue(aId
, aValue
);
60 void SetIdentAtomValue(nsCSSPropertyID aId
, nsAtom
* aValue
);
62 void SetIdentAtomValueIfUnset(nsCSSPropertyID aId
, nsAtom
* aValue
) {
63 if (!PropertyIsSet(aId
)) {
64 SetIdentAtomValue(aId
, aValue
);
68 // Set a property to a keyword (usually NS_STYLE_* or StyleFoo::*)
69 void SetKeywordValue(nsCSSPropertyID aId
, int32_t aValue
) {
70 Servo_DeclarationBlock_SetKeywordValue(mDecl
, aId
, aValue
);
73 void SetKeywordValueIfUnset(nsCSSPropertyID aId
, int32_t aValue
) {
74 if (!PropertyIsSet(aId
)) {
75 SetKeywordValue(aId
, aValue
);
80 typename
= typename
std::enable_if
<std::is_enum
<T
>::value
>::type
>
81 void SetKeywordValue(nsCSSPropertyID aId
, T aValue
) {
82 static_assert(EnumTypeFitsWithin
<T
, int32_t>::value
,
83 "aValue must be an enum that fits within 32 bits");
84 SetKeywordValue(aId
, static_cast<int32_t>(aValue
));
87 typename
= typename
std::enable_if
<std::is_enum
<T
>::value
>::type
>
88 void SetKeywordValueIfUnset(nsCSSPropertyID aId
, T aValue
) {
89 static_assert(EnumTypeFitsWithin
<T
, int32_t>::value
,
90 "aValue must be an enum that fits within 32 bits");
91 SetKeywordValueIfUnset(aId
, static_cast<int32_t>(aValue
));
94 // Set a property to an integer value
95 void SetIntValue(nsCSSPropertyID aId
, int32_t aValue
) {
96 Servo_DeclarationBlock_SetIntValue(mDecl
, aId
, aValue
);
99 // Set "math-depth: <integer>" or "math-depth: add(<integer>)"
100 void SetMathDepthValue(int32_t aValue
, bool aIsRelative
) {
101 Servo_DeclarationBlock_SetMathDepthValue(mDecl
, aValue
, aIsRelative
);
104 // Set "counter-reset: list-item <integer>".
105 void SetCounterResetListItem(int32_t aValue
) {
106 Servo_DeclarationBlock_SetCounterResetListItem(mDecl
, aValue
);
109 // Set "counter-set: list-item <integer>".
110 void SetCounterSetListItem(int32_t aValue
) {
111 Servo_DeclarationBlock_SetCounterSetListItem(mDecl
, aValue
);
114 // Set a property to a pixel value
115 void SetPixelValue(nsCSSPropertyID aId
, float aValue
) {
116 Servo_DeclarationBlock_SetPixelValue(mDecl
, aId
, aValue
);
119 void SetPixelValueIfUnset(nsCSSPropertyID aId
, float aValue
) {
120 if (!PropertyIsSet(aId
)) {
121 SetPixelValue(aId
, aValue
);
125 void SetLengthValue(nsCSSPropertyID aId
, const nsCSSValue
& aValue
) {
126 MOZ_ASSERT(aValue
.IsLengthUnit());
127 Servo_DeclarationBlock_SetLengthValue(mDecl
, aId
, aValue
.GetFloatValue(),
131 // Set a property to a number value
132 void SetNumberValue(nsCSSPropertyID aId
, float aValue
) {
133 Servo_DeclarationBlock_SetNumberValue(mDecl
, aId
, aValue
);
136 // Set a property to a percent value
137 void SetPercentValue(nsCSSPropertyID aId
, float aValue
) {
138 Servo_DeclarationBlock_SetPercentValue(mDecl
, aId
, aValue
);
141 void SetPercentValueIfUnset(nsCSSPropertyID aId
, float aValue
) {
142 if (!PropertyIsSet(aId
)) {
143 SetPercentValue(aId
, aValue
);
147 // Set a property to `auto`
148 void SetAutoValue(nsCSSPropertyID aId
) {
149 Servo_DeclarationBlock_SetAutoValue(mDecl
, aId
);
152 void SetAutoValueIfUnset(nsCSSPropertyID aId
) {
153 if (!PropertyIsSet(aId
)) {
158 // Set a property to `currentcolor`
159 void SetCurrentColor(nsCSSPropertyID aId
) {
160 Servo_DeclarationBlock_SetCurrentColor(mDecl
, aId
);
163 void SetCurrentColorIfUnset(nsCSSPropertyID aId
) {
164 if (!PropertyIsSet(aId
)) {
165 SetCurrentColor(aId
);
169 // Set a property to an RGBA nscolor value
170 void SetColorValue(nsCSSPropertyID aId
, nscolor aValue
) {
171 Servo_DeclarationBlock_SetColorValue(mDecl
, aId
, aValue
);
174 void SetColorValueIfUnset(nsCSSPropertyID aId
, nscolor aValue
) {
175 if (!PropertyIsSet(aId
)) {
176 SetColorValue(aId
, aValue
);
180 // Set font-family to a string
181 void SetFontFamily(const nsACString
& aValue
) {
182 Servo_DeclarationBlock_SetFontFamily(mDecl
, &aValue
);
185 // Add a quirks-mode override to the decoration color of elements nested in
187 void SetTextDecorationColorOverride() {
188 Servo_DeclarationBlock_SetTextDecorationColorOverride(mDecl
);
191 void SetBackgroundImage(const nsAttrValue
& value
);
193 void SetAspectRatio(float aWidth
, float aHeight
) {
194 Servo_DeclarationBlock_SetAspectRatio(mDecl
, aWidth
, aHeight
);
198 dom::Document
* const mDocument
;
199 RefPtr
<RawServoDeclarationBlock
> mDecl
;
202 } // namespace mozilla
204 #endif // mozilla_MappedDeclarations_h