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 * style sheet and style rule processor representing style attributes
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
;
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
)
56 nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData
* aData
)
58 ElementRulesMatching(aData
->mPresContext
, aData
->mElement
,
63 nsHTMLCSSStyleSheet::ElementRulesMatching(nsPresContext
* aPresContext
,
65 nsRuleWalker
* aRuleWalker
)
67 // just get the one and only style rule from the content's STYLE attribute
68 css::StyleRule
* rule
= aElement
->GetInlineStyleRule();
71 aRuleWalker
->Forward(rule
);
74 rule
= aElement
->GetSMILOverrideStyleRule();
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
);
86 // Animation restyle (or non-restyle traversal of rules)
87 // Now we can walk SMIL overrride style, without triggering transitions.
89 aRuleWalker
->Forward(rule
);
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();
103 aData
->mRuleWalker
->Forward(rule
);
109 nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData
* aData
)
115 nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData
* aData
)
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);
134 nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData
* aData
)
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);
153 nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext
* aPresContext
)
159 SizeOfCachedStyleAttrsEntryExcludingThis(nsStringHashKey::KeyType
& aKey
,
160 MiscContainer
* const& aData
,
161 mozilla::MallocSizeOf aMallocSizeOf
,
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
);
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
,
181 nsHTMLCSSStyleSheet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const
183 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf
);
187 nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString
& aSerialized
,
188 MiscContainer
* aValue
)
190 mCachedStyleAttrs
.Put(aSerialized
, aValue
);
194 nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString
& aSerialized
,
195 MiscContainer
* aValue
)
199 NS_ASSERTION(aValue
= mCachedStyleAttrs
.Get(aSerialized
),
200 "Cached value does not match?!");
203 mCachedStyleAttrs
.Remove(aSerialized
);
207 nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString
& aSerialized
)
209 return mCachedStyleAttrs
.Get(aSerialized
);