Bug 1698238 return default dictionary from GetUserMediaRequest#getConstraints() if...
[gecko.git] / layout / style / ComputedStyle.h
blob22024f2d92a2e3bd26547f4862a370fbd3ea9981
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 /* the interface (to internal code) for retrieving computed style data */
9 #ifndef _ComputedStyle_h_
10 #define _ComputedStyle_h_
12 #include "mozilla/Assertions.h"
13 #include "mozilla/CachedInheritingStyles.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/PseudoStyleType.h"
16 #include "mozilla/ServoComputedData.h"
17 #include "mozilla/ServoComputedDataInlines.h"
18 #include "mozilla/ServoStyleConsts.h"
19 #include "nsCSSPseudoElements.h"
20 #include "nsColor.h"
22 #include "nsStyleStructFwd.h"
24 enum nsChangeHint : uint32_t;
25 class nsWindowSizes;
27 #define STYLE_STRUCT(name_) struct nsStyle##name_;
28 #include "nsStyleStructList.h"
29 #undef STYLE_STRUCT
31 extern "C" {
32 void Gecko_ComputedStyle_Destroy(mozilla::ComputedStyle*);
35 namespace mozilla {
37 namespace dom {
38 class Document;
41 /**
42 * A ComputedStyle represents the computed style data for an element.
44 * The computed style data are stored in a set of reference counted structs
45 * (see nsStyleStruct.h) that are stored directly on the ComputedStyle.
47 * Style structs are immutable once they have been produced, so when any change
48 * is made that needs a restyle, we create a new ComputedStyle.
50 * ComputedStyles are reference counted. References are generally held by:
52 * 1. nsIFrame::mComputedStyle, for every frame
53 * 2. Element::mServoData, for every element not inside a display:none subtree
54 * 3. nsComputedDOMStyle, when created for elements in display:none subtrees
55 * 4. media_queries::Device, which holds the initial value of every property
58 class ComputedStyle {
59 using Flag = StyleComputedValueFlags;
61 const StyleComputedValueFlags& Flags() const { return mSource.flags; }
63 public:
64 ComputedStyle(PseudoStyleType aPseudoType,
65 ServoComputedDataForgotten aComputedValues);
67 // Returns the computed (not resolved) value of the given property.
68 void GetComputedPropertyValue(nsCSSPropertyID aId, nsACString& aOut) const {
69 Servo_GetPropertyValue(this, aId, &aOut);
72 // Return the ComputedStyle whose style data should be used for the R,
73 // G, and B components of color, background-color, and border-*-color
74 // if RelevantLinkIsVisited().
76 // GetPseudo() and GetPseudoType() on this ComputedStyle return the
77 // same as on |this|, and its depth in the tree (number of GetParent()
78 // calls until null is returned) is the same as |this|, since its
79 // parent is either |this|'s parent or |this|'s parent's
80 // style-if-visited.
82 // Structs on this context should never be examined without also
83 // examining the corresponding struct on |this|. Doing so will likely
84 // both (1) lead to a privacy leak and (2) lead to dynamic change bugs
85 // related to the Peek code in ComputedStyle::CalcStyleDifference.
86 ComputedStyle* GetStyleIfVisited() const {
87 return mSource.visited_style.mPtr;
90 bool IsLazilyCascadedPseudoElement() const {
91 return IsPseudoElement() &&
92 !nsCSSPseudoElements::IsEagerlyCascadedInServo(GetPseudoType());
95 PseudoStyleType GetPseudoType() const { return mPseudoType; }
97 bool IsPseudoElement() const {
98 return PseudoStyle::IsPseudoElement(mPseudoType);
101 bool IsInheritingAnonBox() const {
102 return PseudoStyle::IsInheritingAnonBox(mPseudoType);
105 bool IsNonInheritingAnonBox() const {
106 return PseudoStyle::IsNonInheritingAnonBox(mPseudoType);
109 bool IsWrapperAnonBox() const {
110 return PseudoStyle::IsWrapperAnonBox(mPseudoType);
113 bool IsAnonBox() const { return PseudoStyle::IsAnonBox(mPseudoType); }
115 bool IsPseudoOrAnonBox() const {
116 return mPseudoType != PseudoStyleType::NotPseudo;
119 // Whether there are author-specified rules for padding properties.
120 // Only returns something meaningful if the appearance property is not `none`.
121 bool HasAuthorSpecifiedPadding() const {
122 return bool(Flags() & Flag::HAS_AUTHOR_SPECIFIED_PADDING);
125 // Whether there are author-specified rules for border or background
126 // properties.
127 // Only returns something meaningful if the appearance property is not `none`.
128 bool HasAuthorSpecifiedBorderOrBackground() const {
129 return bool(Flags() & Flag::HAS_AUTHOR_SPECIFIED_BORDER_BACKGROUND);
132 // Does this ComputedStyle or any of its ancestors have text
133 // decoration lines?
134 // Differs from nsStyleTextReset::HasTextDecorationLines, which tests
135 // only the data for a single context.
136 bool HasTextDecorationLines() const {
137 return bool(Flags() & Flag::HAS_TEXT_DECORATION_LINES);
140 // Whether any line break inside should be suppressed? If this returns
141 // true, the line should not be broken inside, which means inlines act
142 // as if nowrap is set, <br> is suppressed, and blocks are inlinized.
143 // This bit is propogated to all children of line partitipants. It is
144 // currently used by ruby to make its content frames unbreakable.
145 // NOTE: for nsTextFrame, use nsTextFrame::ShouldSuppressLineBreak()
146 // instead of this method.
147 bool ShouldSuppressLineBreak() const {
148 return bool(Flags() & Flag::SHOULD_SUPPRESS_LINEBREAK);
151 // Is this horizontal-in-vertical (tate-chu-yoko) text? This flag is
152 // only set on ComputedStyles whose pseudo is nsCSSAnonBoxes::mozText().
153 bool IsTextCombined() const { return bool(Flags() & Flag::IS_TEXT_COMBINED); }
155 // Whether there's any font metric dependency coming directly from our style.
156 bool DependsOnSelfFontMetrics() const {
157 return bool(Flags() & Flag::DEPENDS_ON_SELF_FONT_METRICS);
160 // Whether there's any font metric dependency coming directly from our parent
161 // style.
162 bool DependsOnInheritedFontMetrics() const {
163 return bool(Flags() & Flag::DEPENDS_ON_INHERITED_FONT_METRICS);
166 // Does this ComputedStyle represent the style for a pseudo-element or
167 // inherit data from such a ComputedStyle? Whether this returns true
168 // is equivalent to whether it or any of its ancestors returns
169 // non-null for IsPseudoElement().
170 bool HasPseudoElementData() const {
171 return bool(Flags() & Flag::IS_IN_PSEUDO_ELEMENT_SUBTREE);
174 // Is the only link whose visitedness is allowed to influence the
175 // style of the node this ComputedStyle is for (which is that element
176 // or its nearest ancestor that is a link) visited?
177 bool RelevantLinkVisited() const {
178 return bool(Flags() & Flag::IS_RELEVANT_LINK_VISITED);
181 // Whether this style is for the root element of the document.
182 bool IsRootElementStyle() const {
183 return bool(Flags() & Flag::IS_ROOT_ELEMENT_STYLE);
186 bool IsInOpacityZeroSubtree() const {
187 return bool(Flags() & Flag::IS_IN_OPACITY_ZERO_SUBTREE);
190 ComputedStyle* GetCachedInheritingAnonBoxStyle(
191 PseudoStyleType aPseudoType) const {
192 MOZ_ASSERT(PseudoStyle::IsInheritingAnonBox(aPseudoType));
193 return mCachedInheritingStyles.Lookup(aPseudoType);
196 void SetCachedInheritedAnonBoxStyle(ComputedStyle* aStyle) {
197 mCachedInheritingStyles.Insert(aStyle);
200 ComputedStyle* GetCachedLazyPseudoStyle(PseudoStyleType aPseudo) const;
202 void SetCachedLazyPseudoStyle(ComputedStyle* aStyle) {
203 MOZ_ASSERT(aStyle->IsPseudoElement());
204 MOZ_ASSERT(!GetCachedLazyPseudoStyle(aStyle->GetPseudoType()));
205 MOZ_ASSERT(aStyle->IsLazilyCascadedPseudoElement());
207 // Since we're caching lazy pseudo styles on the ComputedValues of the
208 // originating element, we can assume that we either have the same
209 // originating element, or that they were at least similar enough to share
210 // the same ComputedValues, which means that they would match the same
211 // pseudo rules. This allows us to avoid matching selectors and checking
212 // the rule node before deciding to share.
214 // The one place this optimization breaks is with pseudo-elements that
215 // support state (like :hover). So we just avoid sharing in those cases.
216 if (nsCSSPseudoElements::PseudoElementSupportsUserActionState(
217 aStyle->GetPseudoType())) {
218 return;
221 mCachedInheritingStyles.Insert(aStyle);
224 #define STYLE_STRUCT(name_) \
225 inline const nsStyle##name_* Style##name_() const MOZ_NONNULL_RETURN { \
226 return mSource.GetStyle##name_(); \
228 #include "nsStyleStructList.h"
229 #undef STYLE_STRUCT
232 * Compute the style changes needed during restyling when this style
233 * context is being replaced by aNewContext. (This is nonsymmetric since
234 * we optimize by skipping comparison for styles that have never been
235 * requested.)
237 * This method returns a change hint (see nsChangeHint.h). All change
238 * hints apply to the frame and its later continuations or ib-split
239 * siblings. Most (all of those except the "NotHandledForDescendants"
240 * hints) also apply to all descendants.
242 * aEqualStructs must not be null. Into it will be stored a bitfield
243 * representing which structs were compared to be non-equal.
245 * CSS Variables are not compared here. Instead, the caller is responsible for
246 * that when needed (basically only for elements).
248 nsChangeHint CalcStyleDifference(const ComputedStyle& aNewContext,
249 uint32_t* aEqualStructs) const;
251 #ifdef DEBUG
252 bool EqualForCachedAnonymousContentStyle(const ComputedStyle&) const;
253 #endif
255 public:
257 * Get a color that depends on link-visitedness using this and
258 * this->GetStyleIfVisited().
260 * @param aField A pointer to a member variable in a style struct.
261 * The member variable and its style struct must have
262 * been listed in nsCSSVisitedDependentPropList.h.
264 template <typename T, typename S>
265 nscolor GetVisitedDependentColor(T S::*aField) const;
268 * aColors should be a two element array of nscolor in which the first
269 * color is the unvisited color and the second is the visited color.
271 * Combine the R, G, and B components of whichever of aColors should
272 * be used based on aLinkIsVisited with the A component of aColors[0].
274 static nscolor CombineVisitedColors(nscolor* aColors, bool aLinkIsVisited);
277 * Start image loads for this style.
279 * The Document is used to get a hand on the image loader. The old style is a
280 * hack for bug 1439285.
282 inline void StartImageLoads(dom::Document&,
283 const ComputedStyle* aOldStyle = nullptr);
285 #ifdef DEBUG
286 void List(FILE* out, int32_t aIndent);
287 static const char* StructName(StyleStructID aSID);
288 static Maybe<StyleStructID> LookupStruct(const nsACString& aName);
289 #endif
291 // The |aCVsSize| outparam on this function is where the actual CVs size
292 // value is added. It's done that way because the callers know which value
293 // the size should be added to.
294 void AddSizeOfIncludingThis(nsWindowSizes& aSizes, size_t* aCVsSize) const;
296 StyleWritingMode WritingMode() const { return {mSource.WritingMode().mBits}; }
298 protected:
299 // Needs to be friend so that it can call the destructor without making it
300 // public.
301 friend void ::Gecko_ComputedStyle_Destroy(ComputedStyle*);
303 ~ComputedStyle() = default;
305 ServoComputedData mSource;
307 // A cache of anonymous box and lazy pseudo styles inheriting from this style.
308 CachedInheritingStyles mCachedInheritingStyles;
310 const PseudoStyleType mPseudoType;
313 } // namespace mozilla
315 #endif