Bumping manifests a=b2g-bump
[gecko.git] / layout / style / nsHTMLCSSStyleSheet.cpp
blob7349a467f5894c7596318f6040662c539c683837
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 * style sheet and style rule processor representing style attributes
8 */
10 #include "nsHTMLCSSStyleSheet.h"
11 #include "mozilla/MemoryReporting.h"
12 #include "mozilla/css/StyleRule.h"
13 #include "nsIStyleRuleProcessor.h"
14 #include "nsPresContext.h"
15 #include "nsRuleWalker.h"
16 #include "nsRuleProcessorData.h"
17 #include "mozilla/dom/Element.h"
18 #include "nsAttrValue.h"
19 #include "nsAttrValueInlines.h"
20 #include "RestyleManager.h"
22 using namespace mozilla;
23 using namespace mozilla::dom;
25 namespace {
27 PLDHashOperator
28 ClearAttrCache(const nsAString& aKey, MiscContainer*& aValue, void*)
30 // Ideally we'd just call MiscContainer::Evict, but we can't do that since
31 // we're iterating the hashtable.
32 MOZ_ASSERT(aValue->mType == nsAttrValue::eCSSStyleRule);
34 aValue->mValue.mCSSStyleRule->SetHTMLCSSStyleSheet(nullptr);
35 aValue->mValue.mCached = 0;
37 return PL_DHASH_REMOVE;
40 } // anonymous namespace
42 nsHTMLCSSStyleSheet::nsHTMLCSSStyleSheet()
46 nsHTMLCSSStyleSheet::~nsHTMLCSSStyleSheet()
48 // We may go away before all of our cached style attributes do,
49 // so clean up any that are left.
50 mCachedStyleAttrs.Enumerate(ClearAttrCache, nullptr);
53 NS_IMPL_ISUPPORTS(nsHTMLCSSStyleSheet, nsIStyleRuleProcessor)
55 /* virtual */ void
56 nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
58 ElementRulesMatching(aData->mPresContext, aData->mElement,
59 aData->mRuleWalker);
62 void
63 nsHTMLCSSStyleSheet::ElementRulesMatching(nsPresContext* aPresContext,
64 Element* aElement,
65 nsRuleWalker* aRuleWalker)
67 // just get the one and only style rule from the content's STYLE attribute
68 css::StyleRule* rule = aElement->GetInlineStyleRule();
69 if (rule) {
70 rule->RuleMatched();
71 aRuleWalker->Forward(rule);
74 rule = aElement->GetSMILOverrideStyleRule();
75 if (rule) {
76 RestyleManager* restyleManager = aPresContext->RestyleManager();
77 if (restyleManager->SkipAnimationRules()) {
78 // Non-animation restyle -- don't process SMIL override style, because we
79 // don't want SMIL animation to trigger new CSS transitions. Instead,
80 // request an Animation restyle, so we still get noticed.
81 if (restyleManager->PostAnimationRestyles()) {
82 aPresContext->PresShell()->RestyleForAnimation(aElement,
83 eRestyle_StyleAttribute | eRestyle_ChangeAnimationPhase);
85 } else {
86 // Animation restyle (or non-restyle traversal of rules)
87 // Now we can walk SMIL overrride style, without triggering transitions.
88 rule->RuleMatched();
89 aRuleWalker->Forward(rule);
94 /* virtual */ void
95 nsHTMLCSSStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData)
97 if (nsCSSPseudoElements::PseudoElementSupportsStyleAttribute(aData->mPseudoType) &&
98 aData->mPseudoElement) {
99 // just get the one and only style rule from the content's STYLE attribute
100 css::StyleRule* rule = aData->mPseudoElement->GetInlineStyleRule();
101 if (rule) {
102 rule->RuleMatched();
103 aData->mRuleWalker->Forward(rule);
108 /* virtual */ void
109 nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData)
113 #ifdef MOZ_XUL
114 /* virtual */ void
115 nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData)
118 #endif
120 // Test if style is dependent on content state
121 /* virtual */ nsRestyleHint
122 nsHTMLCSSStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData)
124 return nsRestyleHint(0);
127 /* virtual */ nsRestyleHint
128 nsHTMLCSSStyleSheet::HasStateDependentStyle(PseudoElementStateRuleProcessorData* aData)
130 return nsRestyleHint(0);
133 /* virtual */ bool
134 nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData)
136 return false;
139 // Test if style is dependent on attribute
140 /* virtual */ nsRestyleHint
141 nsHTMLCSSStyleSheet::HasAttributeDependentStyle(AttributeRuleProcessorData* aData)
143 // Perhaps should check that it's XUL, SVG, (or HTML) namespace, but
144 // it doesn't really matter.
145 if (aData->mAttrHasChanged && aData->mAttribute == nsGkAtoms::style) {
146 return eRestyle_StyleAttribute | eRestyle_ChangeAnimationPhase;
149 return nsRestyleHint(0);
152 /* virtual */ bool
153 nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext)
155 return false;
158 size_t
159 SizeOfCachedStyleAttrsEntryExcludingThis(nsStringHashKey::KeyType& aKey,
160 MiscContainer* const& aData,
161 mozilla::MallocSizeOf aMallocSizeOf,
162 void* userArg)
164 // We don't own the MiscContainers so we don't count them. We do care about
165 // the size of the nsString members in the keys though.
166 return aKey.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
169 /* virtual */ size_t
170 nsHTMLCSSStyleSheet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
172 // The size of mCachedStyleAttrs's mTable member (a PLDHashTable) is
173 // significant in itself, but more significant is the size of the nsString
174 // members of the nsStringHashKeys.
175 return mCachedStyleAttrs.SizeOfExcludingThis(SizeOfCachedStyleAttrsEntryExcludingThis,
176 aMallocSizeOf,
177 nullptr);
180 /* virtual */ size_t
181 nsHTMLCSSStyleSheet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
183 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
186 void
187 nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString& aSerialized,
188 MiscContainer* aValue)
190 mCachedStyleAttrs.Put(aSerialized, aValue);
193 void
194 nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString& aSerialized,
195 MiscContainer* aValue)
197 #ifdef DEBUG
199 NS_ASSERTION(aValue = mCachedStyleAttrs.Get(aSerialized),
200 "Cached value does not match?!");
202 #endif
203 mCachedStyleAttrs.Remove(aSerialized);
206 MiscContainer*
207 nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString& aSerialized)
209 return mCachedStyleAttrs.Get(aSerialized);