nsHTMLCSSStyleSheet.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. /*
  6. * style sheet and style rule processor representing style attributes
  7. */
  8. #include "nsHTMLCSSStyleSheet.h"
  9. #include "mozilla/MemoryReporting.h"
  10. #include "mozilla/css/StyleRule.h"
  11. #include "mozilla/DeclarationBlockInlines.h"
  12. #include "nsIStyleRuleProcessor.h"
  13. #include "nsPresContext.h"
  14. #include "nsRuleWalker.h"
  15. #include "nsRuleProcessorData.h"
  16. #include "mozilla/dom/Element.h"
  17. #include "nsAttrValue.h"
  18. #include "nsAttrValueInlines.h"
  19. #include "nsCSSPseudoElements.h"
  20. #include "mozilla/RestyleManagerHandle.h"
  21. #include "mozilla/RestyleManagerHandleInlines.h"
  22. using namespace mozilla;
  23. using namespace mozilla::dom;
  24. nsHTMLCSSStyleSheet::nsHTMLCSSStyleSheet()
  25. {
  26. }
  27. nsHTMLCSSStyleSheet::~nsHTMLCSSStyleSheet()
  28. {
  29. // We may go away before all of our cached style attributes do,
  30. // so clean up any that are left.
  31. for (auto iter = mCachedStyleAttrs.Iter(); !iter.Done(); iter.Next()) {
  32. MiscContainer*& value = iter.Data();
  33. // Ideally we'd just call MiscContainer::Evict, but we can't do that since
  34. // we're iterating the hashtable.
  35. if (value->mType == nsAttrValue::eCSSDeclaration) {
  36. DeclarationBlock* declaration = value->mValue.mCSSDeclaration;
  37. declaration->SetHTMLCSSStyleSheet(nullptr);
  38. } else {
  39. MOZ_ASSERT_UNREACHABLE("unexpected cached nsAttrValue type");
  40. }
  41. value->mValue.mCached = 0;
  42. iter.Remove();
  43. }
  44. }
  45. NS_IMPL_ISUPPORTS(nsHTMLCSSStyleSheet, nsIStyleRuleProcessor)
  46. /* virtual */ void
  47. nsHTMLCSSStyleSheet::RulesMatching(ElementRuleProcessorData* aData)
  48. {
  49. ElementRulesMatching(aData->mPresContext, aData->mElement,
  50. aData->mRuleWalker);
  51. }
  52. void
  53. nsHTMLCSSStyleSheet::ElementRulesMatching(nsPresContext* aPresContext,
  54. Element* aElement,
  55. nsRuleWalker* aRuleWalker)
  56. {
  57. // just get the one and only style rule from the content's STYLE attribute
  58. DeclarationBlock* declaration = aElement->GetInlineStyleDeclaration();
  59. if (declaration) {
  60. declaration->SetImmutable();
  61. aRuleWalker->Forward(declaration->AsGecko());
  62. }
  63. declaration = aElement->GetSMILOverrideStyleDeclaration();
  64. if (declaration) {
  65. MOZ_ASSERT(aPresContext->RestyleManager()->IsGecko(),
  66. "stylo: ElementRulesMatching must not be called when we have "
  67. "a Servo-backed style system");
  68. RestyleManager* restyleManager = aPresContext->RestyleManager()->AsGecko();
  69. if (!restyleManager->SkipAnimationRules()) {
  70. // Animation restyle (or non-restyle traversal of rules)
  71. // Now we can walk SMIL overrride style, without triggering transitions.
  72. declaration->SetImmutable();
  73. aRuleWalker->Forward(declaration->AsGecko());
  74. }
  75. }
  76. }
  77. void
  78. nsHTMLCSSStyleSheet::PseudoElementRulesMatching(Element* aPseudoElement,
  79. CSSPseudoElementType
  80. aPseudoType,
  81. nsRuleWalker* aRuleWalker)
  82. {
  83. MOZ_ASSERT(nsCSSPseudoElements::
  84. PseudoElementSupportsStyleAttribute(aPseudoType));
  85. MOZ_ASSERT(aPseudoElement);
  86. // just get the one and only style rule from the content's STYLE attribute
  87. DeclarationBlock* declaration = aPseudoElement->GetInlineStyleDeclaration();
  88. if (declaration) {
  89. declaration->SetImmutable();
  90. aRuleWalker->Forward(declaration->AsGecko());
  91. }
  92. }
  93. /* virtual */ void
  94. nsHTMLCSSStyleSheet::RulesMatching(PseudoElementRuleProcessorData* aData)
  95. {
  96. if (nsCSSPseudoElements::PseudoElementSupportsStyleAttribute(aData->mPseudoType) &&
  97. aData->mPseudoElement) {
  98. PseudoElementRulesMatching(aData->mPseudoElement, aData->mPseudoType,
  99. aData->mRuleWalker);
  100. }
  101. }
  102. /* virtual */ void
  103. nsHTMLCSSStyleSheet::RulesMatching(AnonBoxRuleProcessorData* aData)
  104. {
  105. }
  106. #ifdef MOZ_XUL
  107. /* virtual */ void
  108. nsHTMLCSSStyleSheet::RulesMatching(XULTreeRuleProcessorData* aData)
  109. {
  110. }
  111. #endif
  112. // Test if style is dependent on content state
  113. /* virtual */ nsRestyleHint
  114. nsHTMLCSSStyleSheet::HasStateDependentStyle(StateRuleProcessorData* aData)
  115. {
  116. return nsRestyleHint(0);
  117. }
  118. /* virtual */ nsRestyleHint
  119. nsHTMLCSSStyleSheet::HasStateDependentStyle(PseudoElementStateRuleProcessorData* aData)
  120. {
  121. return nsRestyleHint(0);
  122. }
  123. /* virtual */ bool
  124. nsHTMLCSSStyleSheet::HasDocumentStateDependentStyle(StateRuleProcessorData* aData)
  125. {
  126. return false;
  127. }
  128. // Test if style is dependent on attribute
  129. /* virtual */ nsRestyleHint
  130. nsHTMLCSSStyleSheet::HasAttributeDependentStyle(
  131. AttributeRuleProcessorData* aData,
  132. RestyleHintData& aRestyleHintDataResult)
  133. {
  134. // Perhaps should check that it's XUL, SVG, (or HTML) namespace, but
  135. // it doesn't really matter.
  136. if (aData->mAttrHasChanged && aData->mAttribute == nsGkAtoms::style) {
  137. return eRestyle_StyleAttribute;
  138. }
  139. return nsRestyleHint(0);
  140. }
  141. /* virtual */ bool
  142. nsHTMLCSSStyleSheet::MediumFeaturesChanged(nsPresContext* aPresContext)
  143. {
  144. return false;
  145. }
  146. /* virtual */ size_t
  147. nsHTMLCSSStyleSheet::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
  148. {
  149. // The size of mCachedStyleAttrs's mTable member (a PLDHashTable) is
  150. // significant in itself, but more significant is the size of the nsString
  151. // members of the nsStringHashKeys.
  152. size_t n = 0;
  153. n += mCachedStyleAttrs.ShallowSizeOfExcludingThis(aMallocSizeOf);
  154. for (auto iter = mCachedStyleAttrs.ConstIter(); !iter.Done(); iter.Next()) {
  155. // We don't own the MiscContainers (the hash table values) so we don't
  156. // count them. We do care about the size of the nsString members in the
  157. // keys though.
  158. n += iter.Key().SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  159. }
  160. return n;
  161. }
  162. /* virtual */ size_t
  163. nsHTMLCSSStyleSheet::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
  164. {
  165. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  166. }
  167. void
  168. nsHTMLCSSStyleSheet::CacheStyleAttr(const nsAString& aSerialized,
  169. MiscContainer* aValue)
  170. {
  171. mCachedStyleAttrs.Put(aSerialized, aValue);
  172. }
  173. void
  174. nsHTMLCSSStyleSheet::EvictStyleAttr(const nsAString& aSerialized,
  175. MiscContainer* aValue)
  176. {
  177. #ifdef DEBUG
  178. {
  179. NS_ASSERTION(aValue == mCachedStyleAttrs.Get(aSerialized),
  180. "Cached value does not match?!");
  181. }
  182. #endif
  183. mCachedStyleAttrs.Remove(aSerialized);
  184. }
  185. MiscContainer*
  186. nsHTMLCSSStyleSheet::LookupStyleAttr(const nsAString& aSerialized)
  187. {
  188. return mCachedStyleAttrs.Get(aSerialized);
  189. }