summaryrefslogtreecommitdiffstats
path: root/layout/style/MappedDeclarationsBuilder.h
blob: c7388d1358b9e1e49df301ee50c32fdc11fc43b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Representation of a declaration block used for attribute mapping */

#ifndef mozilla_MappedDeclarationsBuilder_h
#define mozilla_MappedDeclarationsBuilder_h

#include "mozilla/FontPropertyTypes.h"
#include "mozilla/ServoBindingTypes.h"
#include "mozilla/dom/Element.h"
#include "mozilla/ServoBindings.h"
#include "nsCSSPropertyID.h"
#include "nsCSSValue.h"
#include "nsColor.h"

class nsAttrValue;

namespace mozilla {

// This provides a convenient interface for attribute mappers
// (MapAttributesIntoRule) to modify the presentation attribute declaration
// block for a given element.
class MOZ_STACK_CLASS MappedDeclarationsBuilder final {
 public:
  explicit MappedDeclarationsBuilder(
      dom::Element& aElement, dom::Document& aDoc,
      StyleLockedDeclarationBlock* aDecls = nullptr)
      : mDocument(aDoc), mElement(aElement), mDecls(aDecls) {
    if (mDecls) {
      Servo_DeclarationBlock_Clear(mDecls);
    }
  }

  ~MappedDeclarationsBuilder() {
    MOZ_ASSERT(!mDecls, "Forgot to take the block?");
  }

  dom::Document& Document() { return mDocument; }

  already_AddRefed<StyleLockedDeclarationBlock> TakeDeclarationBlock() {
    return mDecls.forget();
  }

  // Check if we already contain a certain longhand
  bool PropertyIsSet(nsCSSPropertyID aId) const {
    return mDecls && Servo_DeclarationBlock_PropertyIsSet(mDecls, aId);
  }

  // Set a property to an identifier (string)
  void SetIdentStringValue(nsCSSPropertyID aId, const nsString& aValue) {
    RefPtr<nsAtom> atom = NS_AtomizeMainThread(aValue);
    SetIdentAtomValue(aId, atom);
  }

  void SetIdentStringValueIfUnset(nsCSSPropertyID aId, const nsString& aValue) {
    if (!PropertyIsSet(aId)) {
      SetIdentStringValue(aId, aValue);
    }
  }

  void SetIdentAtomValue(nsCSSPropertyID aId, nsAtom* aValue);

  void SetIdentAtomValueIfUnset(nsCSSPropertyID aId, nsAtom* aValue) {
    if (!PropertyIsSet(aId)) {
      SetIdentAtomValue(aId, aValue);
    }
  }

  // Set a property to a keyword (usually NS_STYLE_* or StyleFoo::*)
  void SetKeywordValue(nsCSSPropertyID aId, int32_t aValue) {
    Servo_DeclarationBlock_SetKeywordValue(&EnsureDecls(), aId, aValue);
  }

  void SetKeywordValueIfUnset(nsCSSPropertyID aId, int32_t aValue) {
    if (!PropertyIsSet(aId)) {
      SetKeywordValue(aId, aValue);
    }
  }

  template <typename T,
            typename = typename std::enable_if<std::is_enum<T>::value>::type>
  void SetKeywordValue(nsCSSPropertyID aId, T aValue) {
    static_assert(EnumTypeFitsWithin<T, int32_t>::value,
                  "aValue must be an enum that fits within 32 bits");
    SetKeywordValue(aId, static_cast<int32_t>(aValue));
  }
  template <typename T,
            typename = typename std::enable_if<std::is_enum<T>::value>::type>
  void SetKeywordValueIfUnset(nsCSSPropertyID aId, T aValue) {
    static_assert(EnumTypeFitsWithin<T, int32_t>::value,
                  "aValue must be an enum that fits within 32 bits");
    SetKeywordValueIfUnset(aId, static_cast<int32_t>(aValue));
  }

  // Set a property to an integer value
  void SetIntValue(nsCSSPropertyID aId, int32_t aValue) {
    Servo_DeclarationBlock_SetIntValue(&EnsureDecls(), aId, aValue);
  }

  // Set "math-depth: <integer>" or "math-depth: add(<integer>)"
  void SetMathDepthValue(int32_t aValue, bool aIsRelative) {
    Servo_DeclarationBlock_SetMathDepthValue(&EnsureDecls(), aValue,
                                             aIsRelative);
  }

  // Set "counter-reset: list-item <integer>".  If aIsReversed is true then
  // "list-item" instead becomes "reversed(list-item)".
  void SetCounterResetListItem(int32_t aValue, bool aIsReversed) {
    Servo_DeclarationBlock_SetCounterResetListItem(&EnsureDecls(), aValue,
                                                   aIsReversed);
  }

  // Set "counter-set: list-item <integer>".
  void SetCounterSetListItem(int32_t aValue) {
    Servo_DeclarationBlock_SetCounterSetListItem(&EnsureDecls(), aValue);
  }

  // Set a property to a pixel value
  void SetPixelValue(nsCSSPropertyID aId, float aValue) {
    Servo_DeclarationBlock_SetPixelValue(&EnsureDecls(), aId, aValue);
  }

  void SetPixelValueIfUnset(nsCSSPropertyID aId, float aValue) {
    if (!PropertyIsSet(aId)) {
      SetPixelValue(aId, aValue);
    }
  }

  void SetLengthValue(nsCSSPropertyID aId, const nsCSSValue& aValue) {
    MOZ_ASSERT(aValue.IsLengthUnit());
    Servo_DeclarationBlock_SetLengthValue(
        &EnsureDecls(), aId, aValue.GetFloatValue(), aValue.GetUnit());
  }

  // Set a property to a percent value
  void SetPercentValue(nsCSSPropertyID aId, float aValue) {
    Servo_DeclarationBlock_SetPercentValue(&EnsureDecls(), aId, aValue);
  }

  void SetPercentValueIfUnset(nsCSSPropertyID aId, float aValue) {
    if (!PropertyIsSet(aId)) {
      SetPercentValue(aId, aValue);
    }
  }

  // Set a property to `auto`
  void SetAutoValue(nsCSSPropertyID aId) {
    Servo_DeclarationBlock_SetAutoValue(&EnsureDecls(), aId);
  }

  void SetAutoValueIfUnset(nsCSSPropertyID aId) {
    if (!PropertyIsSet(aId)) {
      SetAutoValue(aId);
    }
  }

  // Set a property to `currentcolor`
  void SetCurrentColor(nsCSSPropertyID aId) {
    Servo_DeclarationBlock_SetCurrentColor(&EnsureDecls(), aId);
  }

  void SetCurrentColorIfUnset(nsCSSPropertyID aId) {
    if (!PropertyIsSet(aId)) {
      SetCurrentColor(aId);
    }
  }

  // Set a property to an RGBA nscolor value
  void SetColorValue(nsCSSPropertyID aId, nscolor aValue) {
    Servo_DeclarationBlock_SetColorValue(&EnsureDecls(), aId, aValue);
  }

  void SetColorValueIfUnset(nsCSSPropertyID aId, nscolor aValue) {
    if (!PropertyIsSet(aId)) {
      SetColorValue(aId, aValue);
    }
  }

  // Set font-family to a string
  void SetFontFamily(const nsACString& aValue) {
    Servo_DeclarationBlock_SetFontFamily(&EnsureDecls(), &aValue);
  }

  // Add a quirks-mode override to the decoration color of elements nested in
  // <a>
  void SetTextDecorationColorOverride() {
    Servo_DeclarationBlock_SetTextDecorationColorOverride(&EnsureDecls());
  }

  void SetBackgroundImage(const nsAttrValue& value);

  void SetAspectRatio(float aWidth, float aHeight) {
    Servo_DeclarationBlock_SetAspectRatio(&EnsureDecls(), aWidth, aHeight);
  }

  const nsAttrValue* GetAttr(nsAtom* aName) {
    MOZ_ASSERT(mElement.IsAttributeMapped(aName));
    return mElement.GetParsedAttr(aName);
  }

 private:
  StyleLockedDeclarationBlock& EnsureDecls() {
    if (!mDecls) {
      mDecls = Servo_DeclarationBlock_CreateEmpty().Consume();
    }
    return *mDecls;
  }

  dom::Document& mDocument;
  dom::Element& mElement;
  RefPtr<StyleLockedDeclarationBlock> mDecls;
};

}  // namespace mozilla

#endif