Line data Source code
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 : * representation of a declaration block in a CSS stylesheet, or of
9 : * a style attribute
10 : */
11 :
12 : #ifndef mozilla_DeclarationBlock_h
13 : #define mozilla_DeclarationBlock_h
14 :
15 : #include "mozilla/ServoUtils.h"
16 : #include "mozilla/StyleBackendType.h"
17 :
18 : #include "nsCSSPropertyID.h"
19 :
20 : class nsHTMLCSSStyleSheet;
21 :
22 : namespace mozilla {
23 :
24 : class ServoDeclarationBlock;
25 :
26 : namespace css {
27 : class Declaration;
28 : class Rule;
29 : } // namespace css
30 :
31 : class DeclarationBlock
32 : {
33 : protected:
34 3101 : explicit DeclarationBlock(StyleBackendType aType)
35 3101 : : mImmutable(false)
36 : , mIsDirty(false)
37 3101 : , mType(aType)
38 : {
39 3101 : mContainer.mRaw = 0;
40 3101 : }
41 :
42 15 : DeclarationBlock(const DeclarationBlock& aCopy)
43 15 : : DeclarationBlock(aCopy.mType) {}
44 :
45 : public:
46 655 : MOZ_DECL_STYLO_METHODS(css::Declaration, ServoDeclarationBlock)
47 :
48 : inline MozExternalRefCountType AddRef();
49 : inline MozExternalRefCountType Release();
50 :
51 : inline already_AddRefed<DeclarationBlock> Clone() const;
52 :
53 : /**
54 : * Return whether |this| may be modified.
55 : */
56 11372 : bool IsMutable() const {
57 11372 : return !mImmutable;
58 : }
59 :
60 : /**
61 : * Crash if |this| cannot be modified.
62 : */
63 322 : void AssertMutable() const {
64 322 : MOZ_ASSERT(IsMutable(), "someone forgot to call EnsureMutable");
65 322 : }
66 :
67 : /**
68 : * Mark this declaration as unmodifiable. It's 'const' so it can
69 : * be called from ToString.
70 : */
71 10860 : void SetImmutable() { mImmutable = true; }
72 :
73 : /**
74 : * Return whether |this| has been restyled after modified.
75 : */
76 0 : bool IsDirty() const { return mIsDirty; }
77 :
78 : /**
79 : * Mark this declaration as dirty.
80 : */
81 5 : void SetDirty() { mIsDirty = true; }
82 :
83 : /**
84 : * Mark this declaration as not dirty.
85 : */
86 0 : void UnsetDirty() { mIsDirty = false; }
87 :
88 : /**
89 : * Copy |this|, if necessary to ensure that it can be modified.
90 : */
91 : inline already_AddRefed<DeclarationBlock> EnsureMutable();
92 :
93 3027 : void SetOwningRule(css::Rule* aRule) {
94 3027 : MOZ_ASSERT(!mContainer.mOwningRule || !aRule,
95 : "should never overwrite one rule with another");
96 3027 : mContainer.mOwningRule = aRule;
97 3027 : }
98 :
99 150 : css::Rule* GetOwningRule() const {
100 150 : if (mContainer.mRaw & 0x1) {
101 0 : return nullptr;
102 : }
103 150 : return mContainer.mOwningRule;
104 : }
105 :
106 0 : void SetHTMLCSSStyleSheet(nsHTMLCSSStyleSheet* aHTMLCSSStyleSheet) {
107 0 : MOZ_ASSERT(!mContainer.mHTMLCSSStyleSheet || !aHTMLCSSStyleSheet,
108 : "should never overwrite one sheet with another");
109 0 : mContainer.mHTMLCSSStyleSheet = aHTMLCSSStyleSheet;
110 0 : if (aHTMLCSSStyleSheet) {
111 0 : mContainer.mRaw |= uintptr_t(1);
112 : }
113 0 : }
114 :
115 0 : nsHTMLCSSStyleSheet* GetHTMLCSSStyleSheet() const {
116 0 : if (!(mContainer.mRaw & 0x1)) {
117 0 : return nullptr;
118 : }
119 0 : auto c = mContainer;
120 0 : c.mRaw &= ~uintptr_t(1);
121 0 : return c.mHTMLCSSStyleSheet;
122 : }
123 :
124 : inline void ToString(nsAString& aString) const;
125 :
126 : inline uint32_t Count() const;
127 : inline bool GetNthProperty(uint32_t aIndex, nsAString& aReturn) const;
128 :
129 : inline void GetPropertyValue(const nsAString& aProperty,
130 : nsAString& aValue) const;
131 : inline void GetPropertyValueByID(nsCSSPropertyID aPropID,
132 : nsAString& aValue) const;
133 : inline void GetAuthoredPropertyValue(const nsAString& aProperty,
134 : nsAString& aValue) const;
135 : inline bool GetPropertyIsImportant(const nsAString& aProperty) const;
136 : inline void RemoveProperty(const nsAString& aProperty);
137 : inline void RemovePropertyByID(nsCSSPropertyID aProperty);
138 :
139 : private:
140 : union {
141 : // We only ever have one of these since we have an
142 : // nsHTMLCSSStyleSheet only for style attributes, and style
143 : // attributes never have an owning rule.
144 :
145 : // It's an nsHTMLCSSStyleSheet if the low bit is set.
146 :
147 : uintptr_t mRaw;
148 :
149 : // The style rule that owns this declaration. May be null.
150 : css::Rule* mOwningRule;
151 :
152 : // The nsHTMLCSSStyleSheet that is responsible for this declaration.
153 : // Only non-null for style attributes.
154 : nsHTMLCSSStyleSheet* mHTMLCSSStyleSheet;
155 : } mContainer;
156 :
157 : // set when declaration put in the rule tree;
158 : bool mImmutable;
159 : // True if this declaration has not been restyled after modified.
160 : bool mIsDirty;
161 :
162 : const StyleBackendType mType;
163 : };
164 :
165 : } // namespace mozilla
166 :
167 : #endif // mozilla_DeclarationBlock_h
|