Bug 867089 - Validate the playbackRate before using it. r=ehsan
[gecko.git] / layout / style / nsStyleSet.h
blobb3250b9029643bdc5159456df4dee1a1bf01038d
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /*
7 * the container for the style sheets that apply to a presentation, and
8 * the internal API that the style system exposes for creating (and
9 * potentially re-creating) style contexts
12 #ifndef nsStyleSet_h_
13 #define nsStyleSet_h_
15 #include "mozilla/Attributes.h"
17 #include "nsIStyleRuleProcessor.h"
18 #include "nsCSSStyleSheet.h"
19 #include "nsBindingManager.h"
20 #include "nsRuleNode.h"
21 #include "nsTArray.h"
22 #include "nsCOMArray.h"
23 #include "nsAutoPtr.h"
24 #include "nsIStyleRule.h"
25 #include "nsCSSPseudoElements.h"
26 #include "mozilla/Attributes.h"
28 class nsIURI;
29 class nsCSSFontFaceRule;
30 class nsCSSKeyframesRule;
31 class nsCSSPageRule;
32 class nsRuleWalker;
33 struct ElementDependentRuleProcessorData;
34 struct TreeMatchContext;
36 class nsEmptyStyleRule MOZ_FINAL : public nsIStyleRule
38 NS_DECL_ISUPPORTS
39 virtual void MapRuleInfoInto(nsRuleData* aRuleData);
40 #ifdef DEBUG
41 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
42 #endif
45 class nsInitialStyleRule MOZ_FINAL : public nsIStyleRule
47 NS_DECL_ISUPPORTS
48 virtual void MapRuleInfoInto(nsRuleData* aRuleData);
49 #ifdef DEBUG
50 virtual void List(FILE* out = stdout, int32_t aIndent = 0) const MOZ_OVERRIDE;
51 #endif
54 // The style set object is created by the document viewer and ownership is
55 // then handed off to the PresShell. Only the PresShell should delete a
56 // style set.
58 class nsStyleSet
60 public:
61 nsStyleSet();
63 size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf) const;
65 void Init(nsPresContext *aPresContext);
67 nsRuleNode* GetRuleTree() { return mRuleTree; }
69 // enable / disable the Quirk style sheet
70 void EnableQuirkStyleSheet(bool aEnable);
72 // get a style context for a non-pseudo frame.
73 already_AddRefed<nsStyleContext>
74 ResolveStyleFor(mozilla::dom::Element* aElement,
75 nsStyleContext* aParentContext);
77 already_AddRefed<nsStyleContext>
78 ResolveStyleFor(mozilla::dom::Element* aElement,
79 nsStyleContext* aParentContext,
80 TreeMatchContext& aTreeMatchContext);
82 // Get a style context (with the given parent) for the
83 // sequence of style rules in the |aRules| array.
84 already_AddRefed<nsStyleContext>
85 ResolveStyleForRules(nsStyleContext* aParentContext,
86 const nsTArray< nsCOMPtr<nsIStyleRule> > &aRules);
88 // used in ResolveStyleForRules below
89 struct RuleAndLevel
91 nsIStyleRule* mRule;
92 uint8_t mLevel;
95 // Get a new style context for aElement for the rules in aRules
96 // aRules is an array of rules and their levels in reverse order,
97 // that is from the leaf-most to the root-most rule in the rule tree.
98 already_AddRefed<nsStyleContext>
99 ResolveStyleForRules(nsStyleContext* aParentContext,
100 nsStyleContext* aOldStyle,
101 const nsTArray<RuleAndLevel>& aRules);
103 // Get a style context that represents aBaseContext, but as though
104 // it additionally matched the rules in the aRules array (in that
105 // order, as more specific than any other rules).
106 already_AddRefed<nsStyleContext>
107 ResolveStyleByAddingRules(nsStyleContext* aBaseContext,
108 const nsCOMArray<nsIStyleRule> &aRules);
110 // Get a style context for a non-element (which no rules will match),
111 // such as text nodes, placeholder frames, and the nsFirstLetterFrame
112 // for everything after the first letter.
114 // Perhaps this should go away and we shouldn't even create style
115 // contexts for such content nodes. However, not doing any rule
116 // matching for them is a first step.
117 already_AddRefed<nsStyleContext>
118 ResolveStyleForNonElement(nsStyleContext* aParentContext);
120 // Get a style context for a pseudo-element. aParentElement must be
121 // non-null. aPseudoID is the nsCSSPseudoElements::Type for the
122 // pseudo-element.
123 already_AddRefed<nsStyleContext>
124 ResolvePseudoElementStyle(mozilla::dom::Element* aParentElement,
125 nsCSSPseudoElements::Type aType,
126 nsStyleContext* aParentContext);
128 // This functions just like ResolvePseudoElementStyle except that it will
129 // return nullptr if there are no explicit style rules for that
130 // pseudo element.
131 already_AddRefed<nsStyleContext>
132 ProbePseudoElementStyle(mozilla::dom::Element* aParentElement,
133 nsCSSPseudoElements::Type aType,
134 nsStyleContext* aParentContext);
135 already_AddRefed<nsStyleContext>
136 ProbePseudoElementStyle(mozilla::dom::Element* aParentElement,
137 nsCSSPseudoElements::Type aType,
138 nsStyleContext* aParentContext,
139 TreeMatchContext& aTreeMatchContext);
141 // Get a style context for an anonymous box. aPseudoTag is the
142 // pseudo-tag to use and must be non-null.
143 already_AddRefed<nsStyleContext>
144 ResolveAnonymousBoxStyle(nsIAtom* aPseudoTag, nsStyleContext* aParentContext);
146 #ifdef MOZ_XUL
147 // Get a style context for a XUL tree pseudo. aPseudoTag is the
148 // pseudo-tag to use and must be non-null. aParentContent must be
149 // non-null. aComparator must be non-null.
150 already_AddRefed<nsStyleContext>
151 ResolveXULTreePseudoStyle(mozilla::dom::Element* aParentElement,
152 nsIAtom* aPseudoTag,
153 nsStyleContext* aParentContext,
154 nsICSSPseudoComparator* aComparator);
155 #endif
157 // Append all the currently-active font face rules to aArray. Return
158 // true for success and false for failure.
159 bool AppendFontFaceRules(nsPresContext* aPresContext,
160 nsTArray<nsFontFaceRuleContainer>& aArray);
162 // Append all the currently-active keyframes rules to aArray. Return
163 // true for success and false for failure.
164 bool AppendKeyframesRules(nsPresContext* aPresContext,
165 nsTArray<nsCSSKeyframesRule*>& aArray);
167 // Append all the currently-active page rules to aArray. Return
168 // true for success and false for failure.
169 bool AppendPageRules(nsPresContext* aPresContext,
170 nsTArray<nsCSSPageRule*>& aArray);
172 // Begin ignoring style context destruction, to avoid lots of unnecessary
173 // work on document teardown.
174 void BeginShutdown(nsPresContext* aPresContext);
176 // Free all of the data associated with this style set.
177 void Shutdown(nsPresContext* aPresContext);
179 // Notification that a style context is being destroyed.
180 void NotifyStyleContextDestroyed(nsPresContext* aPresContext,
181 nsStyleContext* aStyleContext);
183 // Get a new style context that lives in a different parent
184 // The new context will be the same as the old if the new parent is the
185 // same as the old parent.
186 // aElement should be non-null if this is a style context for an
187 // element or pseudo-element; in the latter case it should be the
188 // real element the pseudo-element is for.
189 already_AddRefed<nsStyleContext>
190 ReparentStyleContext(nsStyleContext* aStyleContext,
191 nsStyleContext* aNewParentContext,
192 mozilla::dom::Element* aElement);
194 // Test if style is dependent on a document state.
195 bool HasDocumentStateDependentStyle(nsPresContext* aPresContext,
196 nsIContent* aContent,
197 nsEventStates aStateMask);
199 // Test if style is dependent on content state
200 nsRestyleHint HasStateDependentStyle(nsPresContext* aPresContext,
201 mozilla::dom::Element* aElement,
202 nsEventStates aStateMask);
204 // Test if style is dependent on the presence of an attribute.
205 nsRestyleHint HasAttributeDependentStyle(nsPresContext* aPresContext,
206 mozilla::dom::Element* aElement,
207 nsIAtom* aAttribute,
208 int32_t aModType,
209 bool aAttrHasChanged);
212 * Do any processing that needs to happen as a result of a change in
213 * the characteristics of the medium, and return whether style rules
214 * may have changed as a result.
216 bool MediumFeaturesChanged(nsPresContext* aPresContext);
218 // APIs for registering objects that can supply additional
219 // rules during processing.
220 void SetBindingManager(nsBindingManager* aBindingManager)
222 mBindingManager = aBindingManager;
225 // The "origins" of the CSS cascade, from lowest precedence to
226 // highest (for non-!important rules).
227 enum sheetType {
228 eAgentSheet, // CSS
229 eUserSheet, // CSS
230 ePresHintSheet,
231 eDocSheet, // CSS
232 eScopedDocSheet,
233 eStyleAttrSheet,
234 eOverrideSheet, // CSS
235 eAnimationSheet,
236 eTransitionSheet,
237 eSheetTypeCount
238 // be sure to keep the number of bits in |mDirty| below and in
239 // NS_RULE_NODE_LEVEL_MASK updated when changing the number of sheet
240 // types
243 // APIs to manipulate the style sheet lists. The sheets in each
244 // list are stored with the most significant sheet last.
245 nsresult AppendStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
246 nsresult PrependStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
247 nsresult RemoveStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
248 nsresult ReplaceSheets(sheetType aType,
249 const nsCOMArray<nsIStyleSheet> &aNewSheets);
250 nsresult InsertStyleSheetBefore(sheetType aType, nsIStyleSheet *aNewSheet,
251 nsIStyleSheet *aReferenceSheet);
253 // Enable/Disable entire author style level (Doc, ScopedDoc & PresHint levels)
254 bool GetAuthorStyleDisabled();
255 nsresult SetAuthorStyleDisabled(bool aStyleDisabled);
257 int32_t SheetCount(sheetType aType) const {
258 return mSheets[aType].Count();
261 nsIStyleSheet* StyleSheetAt(sheetType aType, int32_t aIndex) const {
262 return mSheets[aType].ObjectAt(aIndex);
265 nsresult RemoveDocStyleSheet(nsIStyleSheet* aSheet);
266 nsresult AddDocStyleSheet(nsIStyleSheet* aSheet, nsIDocument* aDocument);
268 void BeginUpdate();
269 nsresult EndUpdate();
271 // Methods for reconstructing the tree; BeginReconstruct basically moves the
272 // old rule tree root and style context roots out of the way,
273 // and EndReconstruct destroys the old rule tree when we're done
274 nsresult BeginReconstruct();
275 // Note: EndReconstruct should not be called if BeginReconstruct fails
276 void EndReconstruct();
278 // Let the style set know that a particular sheet is the quirks sheet. This
279 // sheet must already have been added to the UA sheets. The pointer must not
280 // be null. This should only be called once for a given style set.
281 void SetQuirkStyleSheet(nsIStyleSheet* aQuirkStyleSheet);
283 // Return whether the rule tree has cached data such that we need to
284 // do dynamic change handling for changes that change the results of
285 // media queries or require rebuilding all style data.
286 // We don't care whether we have cached rule processors or whether
287 // they have cached rule cascades; getting the rule cascades again in
288 // order to do rule matching will get the correct rule cascade.
289 bool HasCachedStyleData() const {
290 return (mRuleTree && mRuleTree->TreeHasCachedData()) || !mRoots.IsEmpty();
293 // Notify the style set that a rulenode is no longer in use, or was
294 // just created and is not in use yet.
295 void RuleNodeUnused() {
296 ++mUnusedRuleNodeCount;
299 // Notify the style set that a rulenode that wasn't in use now is
300 void RuleNodeInUse() {
301 --mUnusedRuleNodeCount;
304 nsCSSStyleSheet::EnsureUniqueInnerResult EnsureUniqueInnerOnCSSSheets();
306 nsIStyleRule* InitialStyleRule();
308 private:
309 nsStyleSet(const nsStyleSet& aCopy) MOZ_DELETE;
310 nsStyleSet& operator=(const nsStyleSet& aCopy) MOZ_DELETE;
312 // Run mark-and-sweep GC on mRuleTree and mOldRuleTrees, based on mRoots.
313 void GCRuleTrees();
315 // Update the rule processor list after a change to the style sheet list.
316 nsresult GatherRuleProcessors(sheetType aType);
318 void AddImportantRules(nsRuleNode* aCurrLevelNode,
319 nsRuleNode* aLastPrevLevelNode,
320 nsRuleWalker* aRuleWalker);
322 // Move aRuleWalker forward by the appropriate rule if we need to add
323 // a rule due to property restrictions on pseudo-elements.
324 void WalkRestrictionRule(nsCSSPseudoElements::Type aPseudoType,
325 nsRuleWalker* aRuleWalker);
327 #ifdef DEBUG
328 // Just like AddImportantRules except it doesn't actually add anything; it
329 // just asserts that there are no important rules between aCurrLevelNode and
330 // aLastPrevLevelNode.
331 void AssertNoImportantRules(nsRuleNode* aCurrLevelNode,
332 nsRuleNode* aLastPrevLevelNode);
334 // Just like AddImportantRules except it doesn't actually add anything; it
335 // just asserts that there are no CSS rules between aCurrLevelNode and
336 // aLastPrevLevelNode. Mostly useful for the preshint level.
337 void AssertNoCSSRules(nsRuleNode* aCurrLevelNode,
338 nsRuleNode* aLastPrevLevelNode);
339 #endif
341 // Enumerate the rules in a way that cares about the order of the
342 // rules.
343 // aElement is the element the rules are for. It might be null. aData
344 // is the closure to pass to aCollectorFunc. If aContent is not null,
345 // aData must be a RuleProcessorData*
346 void FileRules(nsIStyleRuleProcessor::EnumFunc aCollectorFunc,
347 RuleProcessorData* aData, mozilla::dom::Element* aElement,
348 nsRuleWalker* aRuleWalker);
350 // Enumerate all the rules in a way that doesn't care about the order
351 // of the rules and break out if the enumeration is halted.
352 void WalkRuleProcessors(nsIStyleRuleProcessor::EnumFunc aFunc,
353 ElementDependentRuleProcessorData* aData,
354 bool aWalkAllXBLStylesheets);
357 * Bit-flags that can be passed to GetContext() in its parameter 'aFlags'.
359 enum {
360 eNoFlags = 0,
361 eIsLink = 1 << 0,
362 eIsVisitedLink = 1 << 1,
363 eDoAnimation = 1 << 2,
365 // Indicates that we should skip the flex-item-specific chunk of
366 // ApplyStyleFixups(). This is useful if our parent has "display: flex"
367 // but we can tell it's not going to actually be a flex container (e.g. if
368 // it's the outer frame of a button widget, and we're the inline frame for
369 // the button's label).
370 eSkipFlexItemStyleFixup = 1 << 3
373 already_AddRefed<nsStyleContext>
374 GetContext(nsStyleContext* aParentContext,
375 nsRuleNode* aRuleNode,
376 nsRuleNode* aVisitedRuleNode,
377 nsIAtom* aPseudoTag,
378 nsCSSPseudoElements::Type aPseudoType,
379 mozilla::dom::Element* aElementForAnimation,
380 uint32_t aFlags);
382 nsPresContext* PresContext() { return mRuleTree->PresContext(); }
384 // The sheets in each array in mSheets are stored with the most significant
385 // sheet last.
386 nsCOMArray<nsIStyleSheet> mSheets[eSheetTypeCount];
388 // mRuleProcessors[eScopedDocSheet] is always null; rule processors
389 // for scoped style sheets are stored in mScopedDocSheetRuleProcessors.
390 nsCOMPtr<nsIStyleRuleProcessor> mRuleProcessors[eSheetTypeCount];
392 // Rule processors for HTML5 scoped style sheets, one per scope.
393 nsTArray<nsCOMPtr<nsIStyleRuleProcessor> > mScopedDocSheetRuleProcessors;
395 // cached instance for enabling/disabling
396 nsCOMPtr<nsIStyleSheet> mQuirkStyleSheet;
398 nsRefPtr<nsBindingManager> mBindingManager;
400 nsRuleNode* mRuleTree; // This is the root of our rule tree. It is a
401 // lexicographic tree of matched rules that style
402 // contexts use to look up properties.
404 uint16_t mBatching;
406 unsigned mInShutdown : 1;
407 unsigned mAuthorStyleDisabled: 1;
408 unsigned mInReconstruct : 1;
409 unsigned mDirty : 9; // one dirty bit is used per sheet type
411 uint32_t mUnusedRuleNodeCount; // used to batch rule node GC
412 nsTArray<nsStyleContext*> mRoots; // style contexts with no parent
414 // Empty style rules to force things that restrict which properties
415 // apply into different branches of the rule tree.
416 nsRefPtr<nsEmptyStyleRule> mFirstLineRule, mFirstLetterRule, mPlaceholderRule;
418 // Style rule which sets all properties to their initial values for
419 // determining when context-sensitive values are in use.
420 nsRefPtr<nsInitialStyleRule> mInitialStyleRule;
422 // Old rule trees, which should only be non-empty between
423 // BeginReconstruct and EndReconstruct, but in case of bugs that cause
424 // style contexts to exist too long, may last longer.
425 nsTArray<nsRuleNode*> mOldRuleTrees;
428 #ifdef _IMPL_NS_LAYOUT
429 inline
430 void nsRuleNode::AddRef()
432 if (mRefCnt++ == 0 && !IsRoot()) {
433 mPresContext->StyleSet()->RuleNodeInUse();
437 inline
438 void nsRuleNode::Release()
440 if (--mRefCnt == 0 && !IsRoot()) {
441 mPresContext->StyleSet()->RuleNodeUnused();
444 #endif
446 #endif