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/. */
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
15 #include "mozilla/Attributes.h"
16 #include "mozilla/CSSStyleSheet.h"
17 #include "mozilla/MemoryReporting.h"
19 #include "nsIStyleRuleProcessor.h"
20 #include "nsBindingManager.h"
21 #include "nsRuleNode.h"
23 #include "nsCOMArray.h"
24 #include "nsAutoPtr.h"
25 #include "nsIStyleRule.h"
26 #include "nsCSSPseudoElements.h"
28 class gfxFontFeatureValueSet
;
29 class nsCSSFontFaceRule
;
30 class nsCSSKeyframesRule
;
31 class nsCSSFontFeatureValuesRule
;
33 class nsCSSCounterStyleRule
;
35 struct ElementDependentRuleProcessorData
;
36 struct TreeMatchContext
;
40 } // namespace mozilla
42 class nsEmptyStyleRule MOZ_FINAL
: public nsIStyleRule
45 ~nsEmptyStyleRule() {}
49 virtual void MapRuleInfoInto(nsRuleData
* aRuleData
) MOZ_OVERRIDE
;
51 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const MOZ_OVERRIDE
;
55 class nsInitialStyleRule MOZ_FINAL
: public nsIStyleRule
58 ~nsInitialStyleRule() {}
62 virtual void MapRuleInfoInto(nsRuleData
* aRuleData
) MOZ_OVERRIDE
;
64 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const MOZ_OVERRIDE
;
68 class nsDisableTextZoomStyleRule MOZ_FINAL
: public nsIStyleRule
71 ~nsDisableTextZoomStyleRule() {}
75 virtual void MapRuleInfoInto(nsRuleData
* aRuleData
) MOZ_OVERRIDE
;
77 virtual void List(FILE* out
= stdout
, int32_t aIndent
= 0) const MOZ_OVERRIDE
;
81 // The style set object is created by the document viewer and ownership is
82 // then handed off to the PresShell. Only the PresShell should delete a
90 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
92 void Init(nsPresContext
*aPresContext
);
94 nsRuleNode
* GetRuleTree() { return mRuleTree
; }
96 // enable / disable the Quirk style sheet
97 void EnableQuirkStyleSheet(bool aEnable
);
99 // get a style context for a non-pseudo frame.
100 already_AddRefed
<nsStyleContext
>
101 ResolveStyleFor(mozilla::dom::Element
* aElement
,
102 nsStyleContext
* aParentContext
);
104 already_AddRefed
<nsStyleContext
>
105 ResolveStyleFor(mozilla::dom::Element
* aElement
,
106 nsStyleContext
* aParentContext
,
107 TreeMatchContext
& aTreeMatchContext
);
109 // Get a style context (with the given parent) for the
110 // sequence of style rules in the |aRules| array.
111 already_AddRefed
<nsStyleContext
>
112 ResolveStyleForRules(nsStyleContext
* aParentContext
,
113 const nsTArray
< nsCOMPtr
<nsIStyleRule
> > &aRules
);
115 // Get a style context that represents aBaseContext, but as though
116 // it additionally matched the rules in the aRules array (in that
117 // order, as more specific than any other rules).
118 already_AddRefed
<nsStyleContext
>
119 ResolveStyleByAddingRules(nsStyleContext
* aBaseContext
,
120 const nsCOMArray
<nsIStyleRule
> &aRules
);
122 // Resolve style by making replacements in the list of style rules as
123 // described by aReplacements, but otherwise maintaining the status
125 already_AddRefed
<nsStyleContext
>
126 ResolveStyleWithReplacement(mozilla::dom::Element
* aElement
,
127 nsStyleContext
* aNewParentContext
,
128 nsStyleContext
* aOldStyleContext
,
129 nsRestyleHint aReplacements
);
131 // Get a style context for a non-element (which no rules will match),
132 // such as text nodes, placeholder frames, and the nsFirstLetterFrame
133 // for everything after the first letter.
135 // Perhaps this should go away and we shouldn't even create style
136 // contexts for such content nodes. However, not doing any rule
137 // matching for them is a first step.
138 already_AddRefed
<nsStyleContext
>
139 ResolveStyleForNonElement(nsStyleContext
* aParentContext
);
141 // Get a style context for a pseudo-element. aParentElement must be
142 // non-null. aPseudoID is the nsCSSPseudoElements::Type for the
143 // pseudo-element. aPseudoElement must be non-null if the pseudo-element
144 // type is one that allows user action pseudo-classes after it; otherwise,
146 already_AddRefed
<nsStyleContext
>
147 ResolvePseudoElementStyle(mozilla::dom::Element
* aParentElement
,
148 nsCSSPseudoElements::Type aType
,
149 nsStyleContext
* aParentContext
,
150 mozilla::dom::Element
* aPseudoElement
);
152 // This functions just like ResolvePseudoElementStyle except that it will
153 // return nullptr if there are no explicit style rules for that
155 already_AddRefed
<nsStyleContext
>
156 ProbePseudoElementStyle(mozilla::dom::Element
* aParentElement
,
157 nsCSSPseudoElements::Type aType
,
158 nsStyleContext
* aParentContext
);
159 already_AddRefed
<nsStyleContext
>
160 ProbePseudoElementStyle(mozilla::dom::Element
* aParentElement
,
161 nsCSSPseudoElements::Type aType
,
162 nsStyleContext
* aParentContext
,
163 TreeMatchContext
& aTreeMatchContext
,
164 mozilla::dom::Element
* aPseudoElement
= nullptr);
166 // Get a style context for an anonymous box. aPseudoTag is the
167 // pseudo-tag to use and must be non-null.
168 already_AddRefed
<nsStyleContext
>
169 ResolveAnonymousBoxStyle(nsIAtom
* aPseudoTag
, nsStyleContext
* aParentContext
);
172 // Get a style context for a XUL tree pseudo. aPseudoTag is the
173 // pseudo-tag to use and must be non-null. aParentContent must be
174 // non-null. aComparator must be non-null.
175 already_AddRefed
<nsStyleContext
>
176 ResolveXULTreePseudoStyle(mozilla::dom::Element
* aParentElement
,
178 nsStyleContext
* aParentContext
,
179 nsICSSPseudoComparator
* aComparator
);
182 // Append all the currently-active font face rules to aArray. Return
183 // true for success and false for failure.
184 bool AppendFontFaceRules(nsPresContext
* aPresContext
,
185 nsTArray
<nsFontFaceRuleContainer
>& aArray
);
187 // Return the winning (in the cascade) @keyframes rule for the given name.
188 nsCSSKeyframesRule
* KeyframesRuleForName(nsPresContext
* aPresContext
,
189 const nsString
& aName
);
191 // Return the winning (in the cascade) @counter-style rule for the given name.
192 nsCSSCounterStyleRule
* CounterStyleRuleForName(nsPresContext
* aPresContext
,
193 const nsAString
& aName
);
195 // Fetch object for looking up font feature values
196 already_AddRefed
<gfxFontFeatureValueSet
> GetFontFeatureValuesLookup();
198 // Append all the currently-active font feature values rules to aArray.
199 // Return true for success and false for failure.
200 bool AppendFontFeatureValuesRules(nsPresContext
* aPresContext
,
201 nsTArray
<nsCSSFontFeatureValuesRule
*>& aArray
);
203 // Append all the currently-active page rules to aArray. Return
204 // true for success and false for failure.
205 bool AppendPageRules(nsPresContext
* aPresContext
,
206 nsTArray
<nsCSSPageRule
*>& aArray
);
208 // Begin ignoring style context destruction, to avoid lots of unnecessary
209 // work on document teardown.
210 void BeginShutdown(nsPresContext
* aPresContext
);
212 // Free all of the data associated with this style set.
213 void Shutdown(nsPresContext
* aPresContext
);
215 // Notification that a style context is being destroyed.
216 void NotifyStyleContextDestroyed(nsPresContext
* aPresContext
,
217 nsStyleContext
* aStyleContext
);
219 // Get a new style context that lives in a different parent
220 // The new context will be the same as the old if the new parent is the
221 // same as the old parent.
222 // aElement should be non-null if this is a style context for an
223 // element or pseudo-element; in the latter case it should be the
224 // real element the pseudo-element is for.
225 // aElementOrPseudoElement should be the same, except for
226 // pseudo-elements it should be the pseudo-element. It is temporary
227 // until bug 960465 lands. It only really needs to be correct for
228 // things we run animations on (elements and ::before and ::after
230 already_AddRefed
<nsStyleContext
>
231 ReparentStyleContext(nsStyleContext
* aStyleContext
,
232 nsStyleContext
* aNewParentContext
,
233 mozilla::dom::Element
* aElement
,
234 mozilla::dom::Element
* aElementOrPseudoElement
);
236 // Test if style is dependent on a document state.
237 bool HasDocumentStateDependentStyle(nsPresContext
* aPresContext
,
238 nsIContent
* aContent
,
239 mozilla::EventStates aStateMask
);
241 // Test if style is dependent on content state
242 nsRestyleHint
HasStateDependentStyle(nsPresContext
* aPresContext
,
243 mozilla::dom::Element
* aElement
,
244 mozilla::EventStates aStateMask
);
245 nsRestyleHint
HasStateDependentStyle(nsPresContext
* aPresContext
,
246 mozilla::dom::Element
* aElement
,
247 nsCSSPseudoElements::Type aPseudoType
,
248 mozilla::dom::Element
* aPseudoElement
,
249 mozilla::EventStates aStateMask
);
251 // Test if style is dependent on the presence of an attribute.
252 nsRestyleHint
HasAttributeDependentStyle(nsPresContext
* aPresContext
,
253 mozilla::dom::Element
* aElement
,
256 bool aAttrHasChanged
);
259 * Do any processing that needs to happen as a result of a change in
260 * the characteristics of the medium, and return whether style rules
261 * may have changed as a result.
263 bool MediumFeaturesChanged(nsPresContext
* aPresContext
);
265 // APIs for registering objects that can supply additional
266 // rules during processing.
267 void SetBindingManager(nsBindingManager
* aBindingManager
)
269 mBindingManager
= aBindingManager
;
272 // The "origins" of the CSS cascade, from lowest precedence to
273 // highest (for non-!important rules).
281 eOverrideSheet
, // CSS
285 // be sure to keep the number of bits in |mDirty| below and in
286 // NS_RULE_NODE_LEVEL_MASK updated when changing the number of sheet
290 // APIs to manipulate the style sheet lists. The sheets in each
291 // list are stored with the most significant sheet last.
292 nsresult
AppendStyleSheet(sheetType aType
, nsIStyleSheet
*aSheet
);
293 nsresult
PrependStyleSheet(sheetType aType
, nsIStyleSheet
*aSheet
);
294 nsresult
RemoveStyleSheet(sheetType aType
, nsIStyleSheet
*aSheet
);
295 nsresult
ReplaceSheets(sheetType aType
,
296 const nsCOMArray
<nsIStyleSheet
> &aNewSheets
);
297 nsresult
InsertStyleSheetBefore(sheetType aType
, nsIStyleSheet
*aNewSheet
,
298 nsIStyleSheet
*aReferenceSheet
);
300 nsresult
DirtyRuleProcessors(sheetType aType
);
302 // Enable/Disable entire author style level (Doc, ScopedDoc & PresHint levels)
303 bool GetAuthorStyleDisabled();
304 nsresult
SetAuthorStyleDisabled(bool aStyleDisabled
);
306 int32_t SheetCount(sheetType aType
) const {
307 return mSheets
[aType
].Count();
310 nsIStyleSheet
* StyleSheetAt(sheetType aType
, int32_t aIndex
) const {
311 return mSheets
[aType
].ObjectAt(aIndex
);
314 nsresult
RemoveDocStyleSheet(nsIStyleSheet
* aSheet
);
315 nsresult
AddDocStyleSheet(nsIStyleSheet
* aSheet
, nsIDocument
* aDocument
);
318 nsresult
EndUpdate();
320 // Methods for reconstructing the tree; BeginReconstruct basically moves the
321 // old rule tree root and style context roots out of the way,
322 // and EndReconstruct destroys the old rule tree when we're done
323 nsresult
BeginReconstruct();
324 // Note: EndReconstruct should not be called if BeginReconstruct fails
325 void EndReconstruct();
327 bool IsInRuleTreeReconstruct() const {
328 return mInReconstruct
;
331 // Let the style set know that a particular sheet is the quirks sheet. This
332 // sheet must already have been added to the UA sheets. The pointer must not
333 // be null. This should only be called once for a given style set.
334 void SetQuirkStyleSheet(nsIStyleSheet
* aQuirkStyleSheet
);
336 // Return whether the rule tree has cached data such that we need to
337 // do dynamic change handling for changes that change the results of
338 // media queries or require rebuilding all style data.
339 // We don't care whether we have cached rule processors or whether
340 // they have cached rule cascades; getting the rule cascades again in
341 // order to do rule matching will get the correct rule cascade.
342 bool HasCachedStyleData() const {
343 return (mRuleTree
&& mRuleTree
->TreeHasCachedData()) || !mRoots
.IsEmpty();
346 // Notify the style set that a rulenode is no longer in use, or was
347 // just created and is not in use yet.
348 void RuleNodeUnused() {
349 ++mUnusedRuleNodeCount
;
352 // Notify the style set that a rulenode that wasn't in use now is
353 void RuleNodeInUse() {
354 --mUnusedRuleNodeCount
;
357 mozilla::CSSStyleSheet::EnsureUniqueInnerResult
EnsureUniqueInnerOnCSSSheets();
359 nsIStyleRule
* InitialStyleRule();
362 nsStyleSet(const nsStyleSet
& aCopy
) MOZ_DELETE
;
363 nsStyleSet
& operator=(const nsStyleSet
& aCopy
) MOZ_DELETE
;
365 // Run mark-and-sweep GC on mRuleTree and mOldRuleTrees, based on mRoots.
368 // Update the rule processor list after a change to the style sheet list.
369 nsresult
GatherRuleProcessors(sheetType aType
);
371 void AddImportantRules(nsRuleNode
* aCurrLevelNode
,
372 nsRuleNode
* aLastPrevLevelNode
,
373 nsRuleWalker
* aRuleWalker
);
375 // Move aRuleWalker forward by the appropriate rule if we need to add
376 // a rule due to property restrictions on pseudo-elements.
377 void WalkRestrictionRule(nsCSSPseudoElements::Type aPseudoType
,
378 nsRuleWalker
* aRuleWalker
);
380 void WalkDisableTextZoomRule(mozilla::dom::Element
* aElement
,
381 nsRuleWalker
* aRuleWalker
);
384 // Just like AddImportantRules except it doesn't actually add anything; it
385 // just asserts that there are no important rules between aCurrLevelNode and
386 // aLastPrevLevelNode.
387 void AssertNoImportantRules(nsRuleNode
* aCurrLevelNode
,
388 nsRuleNode
* aLastPrevLevelNode
);
390 // Just like AddImportantRules except it doesn't actually add anything; it
391 // just asserts that there are no CSS rules between aCurrLevelNode and
392 // aLastPrevLevelNode. Mostly useful for the preshint level.
393 void AssertNoCSSRules(nsRuleNode
* aCurrLevelNode
,
394 nsRuleNode
* aLastPrevLevelNode
);
397 // Enumerate the rules in a way that cares about the order of the
399 // aElement is the element the rules are for. It might be null. aData
400 // is the closure to pass to aCollectorFunc. If aContent is not null,
401 // aData must be a RuleProcessorData*
402 void FileRules(nsIStyleRuleProcessor::EnumFunc aCollectorFunc
,
403 RuleProcessorData
* aData
, mozilla::dom::Element
* aElement
,
404 nsRuleWalker
* aRuleWalker
);
406 // Enumerate all the rules in a way that doesn't care about the order
407 // of the rules and break out if the enumeration is halted.
408 void WalkRuleProcessors(nsIStyleRuleProcessor::EnumFunc aFunc
,
409 ElementDependentRuleProcessorData
* aData
,
410 bool aWalkAllXBLStylesheets
);
412 // Helper for ResolveStyleWithReplacement
413 nsRuleNode
* RuleNodeWithReplacement(mozilla::dom::Element
* aElement
,
414 nsRuleNode
* aOldRuleNode
,
415 nsCSSPseudoElements::Type aPseudoType
,
416 nsRestyleHint aReplacements
);
419 * Bit-flags that can be passed to GetContext() in its parameter 'aFlags'.
424 eIsVisitedLink
= 1 << 1,
425 eDoAnimation
= 1 << 2,
427 // Indicates that we should skip the flex/grid item specific chunk of
428 // ApplyStyleFixups(). This is useful if our parent has "display: flex"
429 // or "display: grid" but we can tell we're not going to honor that (e.g. if
430 // it's the outer frame of a button widget, and we're the inline frame for
431 // the button's label).
432 eSkipParentDisplayBasedStyleFixup
= 1 << 3
435 already_AddRefed
<nsStyleContext
>
436 GetContext(nsStyleContext
* aParentContext
,
437 nsRuleNode
* aRuleNode
,
438 nsRuleNode
* aVisitedRuleNode
,
440 nsCSSPseudoElements::Type aPseudoType
,
441 mozilla::dom::Element
* aElementForAnimation
,
444 nsPresContext
* PresContext() { return mRuleTree
->PresContext(); }
446 // The sheets in each array in mSheets are stored with the most significant
448 // The arrays for ePresHintSheet, eStyleAttrSheet, eTransitionSheet,
449 // and eAnimationSheet are always empty. (FIXME: We should reduce
450 // the storage needed for them.)
451 nsCOMArray
<nsIStyleSheet
> mSheets
[eSheetTypeCount
];
453 // mRuleProcessors[eScopedDocSheet] is always null; rule processors
454 // for scoped style sheets are stored in mScopedDocSheetRuleProcessors.
455 nsCOMPtr
<nsIStyleRuleProcessor
> mRuleProcessors
[eSheetTypeCount
];
457 // Rule processors for HTML5 scoped style sheets, one per scope.
458 nsTArray
<nsCOMPtr
<nsIStyleRuleProcessor
> > mScopedDocSheetRuleProcessors
;
460 // cached instance for enabling/disabling
461 nsCOMPtr
<nsIStyleSheet
> mQuirkStyleSheet
;
463 nsRefPtr
<nsBindingManager
> mBindingManager
;
465 nsRuleNode
* mRuleTree
; // This is the root of our rule tree. It is a
466 // lexicographic tree of matched rules that style
467 // contexts use to look up properties.
471 unsigned mInShutdown
: 1;
472 unsigned mAuthorStyleDisabled
: 1;
473 unsigned mInReconstruct
: 1;
474 unsigned mInitFontFeatureValuesLookup
: 1;
475 unsigned mDirty
: 9; // one dirty bit is used per sheet type
477 uint32_t mUnusedRuleNodeCount
; // used to batch rule node GC
478 nsTArray
<nsStyleContext
*> mRoots
; // style contexts with no parent
480 // Empty style rules to force things that restrict which properties
481 // apply into different branches of the rule tree.
482 nsRefPtr
<nsEmptyStyleRule
> mFirstLineRule
, mFirstLetterRule
, mPlaceholderRule
;
484 // Style rule which sets all properties to their initial values for
485 // determining when context-sensitive values are in use.
486 nsRefPtr
<nsInitialStyleRule
> mInitialStyleRule
;
488 // Style rule that sets the internal -x-text-zoom property on
489 // <svg:text> elements to disable the effect of text zooming.
490 nsRefPtr
<nsDisableTextZoomStyleRule
> mDisableTextZoomStyleRule
;
492 // Old rule trees, which should only be non-empty between
493 // BeginReconstruct and EndReconstruct, but in case of bugs that cause
494 // style contexts to exist too long, may last longer.
495 nsTArray
<nsRuleNode
*> mOldRuleTrees
;
497 // whether font feature values lookup object needs initialization
498 nsRefPtr
<gfxFontFeatureValueSet
> mFontFeatureValuesLookup
;
501 #ifdef MOZILLA_INTERNAL_API
503 void nsRuleNode::AddRef()
505 if (mRefCnt
++ == 0 && !IsRoot()) {
506 mPresContext
->StyleSet()->RuleNodeInUse();
511 void nsRuleNode::Release()
513 if (--mRefCnt
== 0 && !IsRoot()) {
514 mPresContext
->StyleSet()->RuleNodeUnused();