Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * style sheet and style rule processor representing style attributes |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsHTMLCSSStyleSheet.h" |
michael@0 | 11 | #include "mozilla/MemoryReporting.h" |
michael@0 | 12 | #include "mozilla/css/StyleRule.h" |
michael@0 | 13 | #include "nsIStyleRuleProcessor.h" |
michael@0 | 14 | #include "nsPresContext.h" |
michael@0 | 15 | #include "nsRuleWalker.h" |
michael@0 | 16 | #include "nsRuleProcessorData.h" |
michael@0 | 17 | #include "mozilla/dom/Element.h" |
michael@0 | 18 | #include "nsAttrValue.h" |
michael@0 | 19 | #include "nsAttrValueInlines.h" |
michael@0 | 20 | |
michael@0 | 21 | using namespace mozilla; |
michael@0 | 22 | using namespace mozilla::dom; |
michael@0 | 23 | |
michael@0 | 24 | namespace { |
michael@0 | 25 | |
michael@0 | 26 | PLDHashOperator |
michael@0 | 27 | ClearAttrCache(const nsAString& aKey, MiscContainer*& aValue, void*) |
michael@0 | 28 | { |
michael@0 | 29 | // Ideally we'd just call MiscContainer::Evict, but we can't do that since |
michael@0 | 30 | // we're iterating the hashtable. |
michael@0 | 31 | MOZ_ASSERT(aValue->mType == nsAttrValue::eCSSStyleRule); |
michael@0 | 32 | |
michael@0 | 33 | aValue->mValue.mCSSStyleRule->SetHTMLCSSStyleSheet(nullptr); |
michael@0 | 34 | aValue->mValue.mCached = 0; |
michael@0 | 35 | |
michael@0 | 36 | return PL_DHASH_REMOVE; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | } // anonymous namespace |
michael@0 | 40 | |
michael@0 | 41 | nsHTMLCSSStyleSheet::nsHTMLCSSStyleSheet() |
michael@0 | 42 | { |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | nsHTMLCSSStyleSheet::~nsHTMLCSSStyleSheet() |
michael@0 | 46 | { |
michael@0 | 47 | // We may go away before all of our cached style attributes do, |
michael@0 | 48 | // so clean up any that are left. |
michael@0 | 49 | mCachedStyleAttrs.Enumerate(ClearAttrCache, nullptr); |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | NS_IMPL_ISUPPORTS(nsHTMLCSSStyleSheet, nsIStyleRuleProcessor) |
michael@0 | 53 | |
michael@0 | 54 | /* virtual */ void |
michael@0 | 55 | nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData* aData) |
michael@0 | 56 | { |
michael@0 | 57 | Element* element = aData->mElement; |
michael@0 | 58 | |
michael@0 | 59 | // just get the one and only style rule from the content's STYLE attribute |
michael@0 | 60 | css::StyleRule* rule = element->GetInlineStyleRule(); |
michael@0 | 61 | if (rule) { |
michael@0 | 62 | rule->RuleMatched(); |
michael@0 | 63 | aData->mRuleWalker->Forward(rule); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | rule = element->GetSMILOverrideStyleRule(); |
michael@0 | 67 | if (rule) { |
michael@0 | 68 | if (aData->mPresContext->IsProcessingRestyles() && |
michael@0 | 69 | !aData->mPresContext->IsProcessingAnimationStyleChange()) { |
michael@0 | 70 | // Non-animation restyle -- don't process SMIL override style, because we |
michael@0 | 71 | // don't want SMIL animation to trigger new CSS transitions. Instead, |
michael@0 | 72 | // request an Animation restyle, so we still get noticed. |
michael@0 | 73 | aData->mPresContext->PresShell()->RestyleForAnimation(element, |
michael@0 | 74 | eRestyle_Self); |
michael@0 | 75 | } else { |
michael@0 | 76 | // Animation restyle (or non-restyle traversal of rules) |
michael@0 | 77 | // Now we can walk SMIL overrride style, without triggering transitions. |
michael@0 | 78 | rule->RuleMatched(); |
michael@0 | 79 | aData->mRuleWalker->Forward(rule); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | /* virtual */ void |
michael@0 | 85 | nsHTMLCSSStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData) |
michael@0 | 86 | { |
michael@0 | 87 | if (nsCSSPseudoElements::PseudoElementSupportsStyleAttribute(aData->mPseudoType)) { |
michael@0 | 88 | MOZ_ASSERT(aData->mPseudoElement, |
michael@0 | 89 | "If pseudo element is supposed to support style attribute, it must " |
michael@0 | 90 | "have a pseudo element set"); |
michael@0 | 91 | |
michael@0 | 92 | // just get the one and only style rule from the content's STYLE attribute |
michael@0 | 93 | css::StyleRule* rule = aData->mPseudoElement->GetInlineStyleRule(); |
michael@0 | 94 | if (rule) { |
michael@0 | 95 | rule->RuleMatched(); |
michael@0 | 96 | aData->mRuleWalker->Forward(rule); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* virtual */ void |
michael@0 | 102 | nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData) |
michael@0 | 103 | { |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | #ifdef MOZ_XUL |
michael@0 | 107 | /* virtual */ void |
michael@0 | 108 | nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData) |
michael@0 | 109 | { |
michael@0 | 110 | } |
michael@0 | 111 | #endif |
michael@0 | 112 | |
michael@0 | 113 | // Test if style is dependent on content state |
michael@0 | 114 | /* virtual */ nsRestyleHint |
michael@0 | 115 | nsHTMLCSSStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData) |
michael@0 | 116 | { |
michael@0 | 117 | return nsRestyleHint(0); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | /* virtual */ nsRestyleHint |
michael@0 | 121 | nsHTMLCSSStyleSheet::HasStateDependentStyle(PseudoElementStateRuleProcessorData* aData) |
michael@0 | 122 | { |
michael@0 | 123 | return nsRestyleHint(0); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | /* virtual */ bool |
michael@0 | 127 | nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData) |
michael@0 | 128 | { |
michael@0 | 129 | return false; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Test if style is dependent on attribute |
michael@0 | 133 | /* virtual */ nsRestyleHint |
michael@0 | 134 | nsHTMLCSSStyleSheet::HasAttributeDependentStyle(AttributeRuleProcessorData* aData) |
michael@0 | 135 | { |
michael@0 | 136 | // Perhaps should check that it's XUL, SVG, (or HTML) namespace, but |
michael@0 | 137 | // it doesn't really matter. |
michael@0 | 138 | if (aData->mAttrHasChanged && aData->mAttribute == nsGkAtoms::style) { |
michael@0 | 139 | return eRestyle_Self; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | return nsRestyleHint(0); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | /* virtual */ bool |
michael@0 | 146 | nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext) |
michael@0 | 147 | { |
michael@0 | 148 | return false; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | /* virtual */ size_t |
michael@0 | 152 | nsHTMLCSSStyleSheet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 153 | { |
michael@0 | 154 | return 0; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | /* virtual */ size_t |
michael@0 | 158 | nsHTMLCSSStyleSheet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const |
michael@0 | 159 | { |
michael@0 | 160 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void |
michael@0 | 164 | nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString& aSerialized, |
michael@0 | 165 | MiscContainer* aValue) |
michael@0 | 166 | { |
michael@0 | 167 | mCachedStyleAttrs.Put(aSerialized, aValue); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | void |
michael@0 | 171 | nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString& aSerialized, |
michael@0 | 172 | MiscContainer* aValue) |
michael@0 | 173 | { |
michael@0 | 174 | #ifdef DEBUG |
michael@0 | 175 | { |
michael@0 | 176 | NS_ASSERTION(aValue = mCachedStyleAttrs.Get(aSerialized), |
michael@0 | 177 | "Cached value does not match?!"); |
michael@0 | 178 | } |
michael@0 | 179 | #endif |
michael@0 | 180 | mCachedStyleAttrs.Remove(aSerialized); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | MiscContainer* |
michael@0 | 184 | nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString& aSerialized) |
michael@0 | 185 | { |
michael@0 | 186 | return mCachedStyleAttrs.Get(aSerialized); |
michael@0 | 187 | } |