Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / style / nsStyleSet.h
blob1f8c6f98c7b31e6e00830e3d6823b24c568483cf
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Daniel Glazman <glazman@netscape.com>
24 * Brian Ryner <bryner@brianryner.com>
25 * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
42 * the container for the style sheets that apply to a presentation, and
43 * the internal API that the style system exposes for creating (and
44 * potentially re-creating) style contexts
47 #ifndef nsStyleSet_h_
48 #define nsStyleSet_h_
50 #include "nsIStyleRuleProcessor.h"
51 #include "nsCSSStyleSheet.h"
52 #include "nsBindingManager.h"
53 #include "nsRuleNode.h"
54 #include "nsTArray.h"
55 #include "nsCOMArray.h"
56 #include "nsAutoPtr.h"
57 #include "nsIStyleRule.h"
58 #include "nsCSSPseudoElements.h"
59 #include "nsCSSAnonBoxes.h"
61 class nsIURI;
62 class nsCSSFontFaceRule;
63 class nsRuleWalker;
64 struct RuleProcessorData;
66 class nsEmptyStyleRule : public nsIStyleRule
68 NS_DECL_ISUPPORTS
69 virtual void MapRuleInfoInto(nsRuleData* aRuleData);
70 #ifdef DEBUG
71 virtual void List(FILE* out = stdout, PRInt32 aIndent = 0) const;
72 #endif
75 // The style set object is created by the document viewer and ownership is
76 // then handed off to the PresShell. Only the PresShell should delete a
77 // style set.
79 class nsStyleSet
81 public:
82 nsStyleSet();
84 // Initialize the object. You must check the return code and not use
85 // the nsStyleSet if Init() fails.
87 nsresult Init(nsPresContext *aPresContext);
89 // For getting the cached default data in case we hit out-of-memory.
90 // To be used only by nsRuleNode.
91 nsCachedStyleData* DefaultStyleData() { return &mDefaultStyleData; }
93 nsRuleNode* GetRuleTree() { return mRuleTree; }
95 // enable / disable the Quirk style sheet
96 void EnableQuirkStyleSheet(PRBool aEnable);
98 // get a style context for a non-pseudo frame.
99 already_AddRefed<nsStyleContext>
100 ResolveStyleFor(mozilla::dom::Element* aElement,
101 nsStyleContext* aParentContext);
103 // Get a style context (with the given parent) for the
104 // sequence of style rules in the |aRules| array.
105 already_AddRefed<nsStyleContext>
106 ResolveStyleForRules(nsStyleContext* aParentContext,
107 const nsCOMArray<nsIStyleRule> &aRules);
109 // Get a style context that represents aBaseContext, but as though
110 // it additionally matched the rules in the aRules array (in that
111 // order, as more specific than any other rules).
112 already_AddRefed<nsStyleContext>
113 ResolveStyleByAddingRules(nsStyleContext* aBaseContext,
114 const nsCOMArray<nsIStyleRule> &aRules);
116 // Get a style context for a non-element (which no rules will match),
117 // such as text nodes, placeholder frames, and the nsFirstLetterFrame
118 // for everything after the first letter.
120 // Perhaps this should go away and we shouldn't even create style
121 // contexts for such content nodes. However, not doing any rule
122 // matching for them is a first step.
123 already_AddRefed<nsStyleContext>
124 ResolveStyleForNonElement(nsStyleContext* aParentContext);
126 // Get a style context for a pseudo-element. aParentElement must be
127 // non-null. aPseudoID is the nsCSSPseudoElements::Type for the
128 // pseudo-element.
129 already_AddRefed<nsStyleContext>
130 ResolvePseudoElementStyle(mozilla::dom::Element* aParentElement,
131 nsCSSPseudoElements::Type aType,
132 nsStyleContext* aParentContext);
134 // This functions just like ResolvePseudoElementStyle except that it will
135 // return nsnull if there are no explicit style rules for that
136 // pseudo element.
137 already_AddRefed<nsStyleContext>
138 ProbePseudoElementStyle(mozilla::dom::Element* aParentElement,
139 nsCSSPseudoElements::Type aType,
140 nsStyleContext* aParentContext);
142 // Get a style context for an anonymous box. aPseudoTag is the
143 // pseudo-tag to use and must be non-null.
144 already_AddRefed<nsStyleContext>
145 ResolveAnonymousBoxStyle(nsIAtom* aPseudoTag, nsStyleContext* aParentContext);
147 #ifdef MOZ_XUL
148 // Get a style context for a XUL tree pseudo. aPseudoTag is the
149 // pseudo-tag to use and must be non-null. aParentContent must be
150 // non-null. aComparator must be non-null.
151 already_AddRefed<nsStyleContext>
152 ResolveXULTreePseudoStyle(mozilla::dom::Element* aParentElement,
153 nsIAtom* aPseudoTag,
154 nsStyleContext* aParentContext,
155 nsICSSPseudoComparator* aComparator);
156 #endif
158 // Append all the currently-active font face rules to aArray. Return
159 // true for success and false for failure.
160 PRBool AppendFontFaceRules(nsPresContext* aPresContext,
161 nsTArray<nsFontFaceRuleContainer>& aArray);
163 // Begin ignoring style context destruction, to avoid lots of unnecessary
164 // work on document teardown.
165 void BeginShutdown(nsPresContext* aPresContext);
167 // Free all of the data associated with this style set.
168 void Shutdown(nsPresContext* aPresContext);
170 // Notification that a style context is being destroyed.
171 void NotifyStyleContextDestroyed(nsPresContext* aPresContext,
172 nsStyleContext* aStyleContext);
174 // Get a new style context that lives in a different parent
175 // The new context will be the same as the old if the new parent is the
176 // same as the old parent.
177 // aElement should be non-null if this is a style context for an
178 // element or pseudo-element; in the latter case it should be the
179 // real element the pseudo-element is for.
180 already_AddRefed<nsStyleContext>
181 ReparentStyleContext(nsStyleContext* aStyleContext,
182 nsStyleContext* aNewParentContext,
183 mozilla::dom::Element* aElement);
185 // Test if style is dependent on a document state.
186 PRBool HasDocumentStateDependentStyle(nsPresContext* aPresContext,
187 nsIContent* aContent,
188 nsEventStates aStateMask);
190 // Test if style is dependent on content state
191 nsRestyleHint HasStateDependentStyle(nsPresContext* aPresContext,
192 mozilla::dom::Element* aElement,
193 nsEventStates aStateMask);
195 // Test if style is dependent on the presence of an attribute.
196 nsRestyleHint HasAttributeDependentStyle(nsPresContext* aPresContext,
197 mozilla::dom::Element* aElement,
198 nsIAtom* aAttribute,
199 PRInt32 aModType,
200 PRBool aAttrHasChanged);
203 * Do any processing that needs to happen as a result of a change in
204 * the characteristics of the medium, and return whether style rules
205 * may have changed as a result.
207 PRBool MediumFeaturesChanged(nsPresContext* aPresContext);
209 // APIs for registering objects that can supply additional
210 // rules during processing.
211 void SetBindingManager(nsBindingManager* aBindingManager)
213 mBindingManager = aBindingManager;
216 // The "origins" of the CSS cascade, from lowest precedence to
217 // highest (for non-!important rules).
218 enum sheetType {
219 eAgentSheet, // CSS
220 eUserSheet, // CSS
221 ePresHintSheet,
222 eDocSheet, // CSS
223 eStyleAttrSheet,
224 eOverrideSheet, // CSS
225 eTransitionSheet,
226 eSheetTypeCount
227 // be sure to keep the number of bits in |mDirty| below and in
228 // NS_RULE_NODE_LEVEL_MASK updated when changing the number of sheet
229 // types
232 // APIs to manipulate the style sheet lists. The sheets in each
233 // list are stored with the most significant sheet last.
234 nsresult AppendStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
235 nsresult PrependStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
236 nsresult RemoveStyleSheet(sheetType aType, nsIStyleSheet *aSheet);
237 nsresult ReplaceSheets(sheetType aType,
238 const nsCOMArray<nsIStyleSheet> &aNewSheets);
240 // Enable/Disable entire author style level (Doc & PresHint levels)
241 PRBool GetAuthorStyleDisabled();
242 nsresult SetAuthorStyleDisabled(PRBool aStyleDisabled);
244 PRInt32 SheetCount(sheetType aType) const {
245 return mSheets[aType].Count();
248 nsIStyleSheet* StyleSheetAt(sheetType aType, PRInt32 aIndex) const {
249 return mSheets[aType].ObjectAt(aIndex);
252 nsresult AddDocStyleSheet(nsIStyleSheet* aSheet, nsIDocument* aDocument);
254 void BeginUpdate();
255 nsresult EndUpdate();
257 // Methods for reconstructing the tree; BeginReconstruct basically moves the
258 // old rule tree root and style context roots out of the way,
259 // and EndReconstruct destroys the old rule tree when we're done
260 nsresult BeginReconstruct();
261 // Note: EndReconstruct should not be called if BeginReconstruct fails
262 void EndReconstruct();
264 // Let the style set know that a particular sheet is the quirks sheet. This
265 // sheet must already have been added to the UA sheets. The pointer must not
266 // be null. This should only be called once for a given style set.
267 void SetQuirkStyleSheet(nsIStyleSheet* aQuirkStyleSheet);
269 // Return whether the rule tree has cached data such that we need to
270 // do dynamic change handling for changes that change the results of
271 // media queries or require rebuilding all style data.
272 // We don't care whether we have cached rule processors or whether
273 // they have cached rule cascades; getting the rule cascades again in
274 // order to do rule matching will get the correct rule cascade.
275 PRBool HasCachedStyleData() const {
276 return (mRuleTree && mRuleTree->TreeHasCachedData()) || !mRoots.IsEmpty();
279 // Notify the style set that a rulenode is no longer in use, or was
280 // just created and is not in use yet.
281 void RuleNodeUnused() {
282 ++mUnusedRuleNodeCount;
285 // Notify the style set that a rulenode that wasn't in use now is
286 void RuleNodeInUse() {
287 --mUnusedRuleNodeCount;
290 nsCSSStyleSheet::EnsureUniqueInnerResult EnsureUniqueInnerOnCSSSheets();
292 private:
293 // Not to be implemented
294 nsStyleSet(const nsStyleSet& aCopy);
295 nsStyleSet& operator=(const nsStyleSet& aCopy);
297 // Returns false on out-of-memory.
298 PRBool BuildDefaultStyleData(nsPresContext* aPresContext);
300 // Run mark-and-sweep GC on mRuleTree and mOldRuleTrees, based on mRoots.
301 void GCRuleTrees();
303 // Update the rule processor list after a change to the style sheet list.
304 nsresult GatherRuleProcessors(sheetType aType);
306 void AddImportantRules(nsRuleNode* aCurrLevelNode,
307 nsRuleNode* aLastPrevLevelNode,
308 nsRuleWalker* aRuleWalker);
310 // Move aRuleWalker forward by the appropriate rule if we need to add
311 // a rule due to property restrictions on pseudo-elements.
312 void WalkRestrictionRule(nsCSSPseudoElements::Type aPseudoType,
313 nsRuleWalker* aRuleWalker);
315 #ifdef DEBUG
316 // Just like AddImportantRules except it doesn't actually add anything; it
317 // just asserts that there are no important rules between aCurrLevelNode and
318 // aLastPrevLevelNode.
319 void AssertNoImportantRules(nsRuleNode* aCurrLevelNode,
320 nsRuleNode* aLastPrevLevelNode);
322 // Just like AddImportantRules except it doesn't actually add anything; it
323 // just asserts that there are no CSS rules between aCurrLevelNode and
324 // aLastPrevLevelNode. Mostly useful for the preshint level.
325 void AssertNoCSSRules(nsRuleNode* aCurrLevelNode,
326 nsRuleNode* aLastPrevLevelNode);
327 #endif
329 // Enumerate the rules in a way that cares about the order of the
330 // rules.
331 // aContent is the node the rules are for. It might be null. aData
332 // is the closure to pass to aCollectorFunc. If aContent is not null,
333 // aData must be a RuleProcessorData*
334 void FileRules(nsIStyleRuleProcessor::EnumFunc aCollectorFunc,
335 void* aData, nsIContent* aContent, nsRuleWalker* aRuleWalker);
337 // Enumerate all the rules in a way that doesn't care about the order
338 // of the rules and break out if the enumeration is halted.
339 void WalkRuleProcessors(nsIStyleRuleProcessor::EnumFunc aFunc,
340 RuleProcessorData* aData,
341 PRBool aWalkAllXBLStylesheets);
343 already_AddRefed<nsStyleContext>
344 GetContext(nsStyleContext* aParentContext,
345 nsRuleNode* aRuleNode,
346 nsRuleNode* aVisitedRuleNode,
347 PRBool aIsLink,
348 PRBool aIsVisitedLink,
349 nsIAtom* aPseudoTag,
350 nsCSSPseudoElements::Type aPseudoType);
352 nsPresContext* PresContext() { return mRuleTree->GetPresContext(); }
354 // The sheets in each array in mSheets are stored with the most significant
355 // sheet last.
356 nsCOMArray<nsIStyleSheet> mSheets[eSheetTypeCount];
358 nsCOMPtr<nsIStyleRuleProcessor> mRuleProcessors[eSheetTypeCount];
360 // cached instance for enabling/disabling
361 nsCOMPtr<nsIStyleSheet> mQuirkStyleSheet;
363 nsRefPtr<nsBindingManager> mBindingManager;
365 // To be used only in case of emergency, such as being out of memory
366 // or operating on a deleted rule node. The latter should never
367 // happen, of course.
368 nsCachedStyleData mDefaultStyleData;
370 nsRuleNode* mRuleTree; // This is the root of our rule tree. It is a
371 // lexicographic tree of matched rules that style
372 // contexts use to look up properties.
374 PRUint32 mUnusedRuleNodeCount; // used to batch rule node GC
375 nsTArray<nsStyleContext*> mRoots; // style contexts with no parent
377 // Empty style rules to force things that restrict which properties
378 // apply into different branches of the rule tree.
379 nsRefPtr<nsEmptyStyleRule> mFirstLineRule, mFirstLetterRule;
381 PRUint16 mBatching;
383 // Old rule trees, which should only be non-empty between
384 // BeginReconstruct and EndReconstruct, but in case of bugs that cause
385 // style contexts to exist too long, may last longer.
386 nsTArray<nsRuleNode*> mOldRuleTrees;
388 unsigned mInShutdown : 1;
389 unsigned mAuthorStyleDisabled: 1;
390 unsigned mInReconstruct : 1;
391 unsigned mDirty : 8; // one dirty bit is used per sheet type
395 #ifdef _IMPL_NS_LAYOUT
396 inline
397 void nsRuleNode::AddRef()
399 if (mRefCnt++ == 0 && !IsRoot()) {
400 mPresContext->StyleSet()->RuleNodeInUse();
404 inline
405 void nsRuleNode::Release()
407 if (--mRefCnt == 0 && !IsRoot()) {
408 mPresContext->StyleSet()->RuleNodeUnused();
411 #endif
413 #endif