RuleSet.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
  3. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #ifndef RuleSet_h
  22. #define RuleSet_h
  23. #include "RuleFeature.h"
  24. #include "StyleRule.h"
  25. #include <wtf/Forward.h>
  26. #include <wtf/HashMap.h>
  27. #include <wtf/HashSet.h>
  28. #include <wtf/text/AtomicString.h>
  29. namespace WebCore {
  30. enum AddRuleFlags {
  31. RuleHasNoSpecialState = 0,
  32. RuleHasDocumentSecurityOrigin = 1,
  33. RuleCanUseFastCheckSelector = 1 << 1,
  34. RuleIsInRegionRule = 1 << 2,
  35. };
  36. enum PropertyWhitelistType {
  37. PropertyWhitelistNone = 0,
  38. PropertyWhitelistRegion,
  39. #if ENABLE(VIDEO_TRACK)
  40. PropertyWhitelistCue
  41. #endif
  42. };
  43. class CSSSelector;
  44. class ContainerNode;
  45. class MediaQueryEvaluator;
  46. class StyleResolver;
  47. class StyleRuleRegion;
  48. class StyleSheetContents;
  49. class RuleData {
  50. public:
  51. static const unsigned maximumSelectorComponentCount = 8192;
  52. RuleData(StyleRule*, unsigned selectorIndex, unsigned position, AddRuleFlags);
  53. unsigned position() const { return m_position; }
  54. StyleRule* rule() const { return m_rule; }
  55. const CSSSelector* selector() const { return m_rule->selectorList().selectorAt(m_selectorIndex); }
  56. unsigned selectorIndex() const { return m_selectorIndex; }
  57. bool hasFastCheckableSelector() const { return m_hasFastCheckableSelector; }
  58. bool hasMultipartSelector() const { return m_hasMultipartSelector; }
  59. bool hasRightmostSelectorMatchingHTMLBasedOnRuleHash() const { return m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash; }
  60. bool containsUncommonAttributeSelector() const { return m_containsUncommonAttributeSelector; }
  61. unsigned specificity() const { return m_specificity; }
  62. unsigned linkMatchType() const { return m_linkMatchType; }
  63. bool hasDocumentSecurityOrigin() const { return m_hasDocumentSecurityOrigin; }
  64. PropertyWhitelistType propertyWhitelistType(bool isMatchingUARules = false) const { return isMatchingUARules ? PropertyWhitelistNone : static_cast<PropertyWhitelistType>(m_propertyWhitelistType); }
  65. // Try to balance between memory usage (there can be lots of RuleData objects) and good filtering performance.
  66. static const unsigned maximumIdentifierCount = 4;
  67. const unsigned* descendantSelectorIdentifierHashes() const { return m_descendantSelectorIdentifierHashes; }
  68. private:
  69. StyleRule* m_rule;
  70. unsigned m_selectorIndex : 13;
  71. // This number was picked fairly arbitrarily. We can probably lower it if we need to.
  72. // Some simple testing showed <100,000 RuleData's on large sites.
  73. unsigned m_position : 18;
  74. unsigned m_hasFastCheckableSelector : 1;
  75. unsigned m_specificity : 24;
  76. unsigned m_hasMultipartSelector : 1;
  77. unsigned m_hasRightmostSelectorMatchingHTMLBasedOnRuleHash : 1;
  78. unsigned m_containsUncommonAttributeSelector : 1;
  79. unsigned m_linkMatchType : 2; // SelectorChecker::LinkMatchMask
  80. unsigned m_hasDocumentSecurityOrigin : 1;
  81. unsigned m_propertyWhitelistType : 2;
  82. // Use plain array instead of a Vector to minimize memory overhead.
  83. unsigned m_descendantSelectorIdentifierHashes[maximumIdentifierCount];
  84. };
  85. struct SameSizeAsRuleData {
  86. void* a;
  87. unsigned b;
  88. unsigned c;
  89. unsigned d[4];
  90. };
  91. COMPILE_ASSERT(sizeof(RuleData) == sizeof(SameSizeAsRuleData), RuleData_should_stay_small);
  92. class RuleSet {
  93. WTF_MAKE_NONCOPYABLE(RuleSet); WTF_MAKE_FAST_ALLOCATED;
  94. public:
  95. static PassOwnPtr<RuleSet> create() { return adoptPtr(new RuleSet); }
  96. typedef HashMap<AtomicStringImpl*, OwnPtr<Vector<RuleData> > > AtomRuleMap;
  97. void addRulesFromSheet(StyleSheetContents*, const MediaQueryEvaluator&, StyleResolver* = 0, const ContainerNode* = 0);
  98. void addStyleRule(StyleRule*, AddRuleFlags);
  99. void addRule(StyleRule*, unsigned selectorIndex, AddRuleFlags);
  100. void addPageRule(StyleRulePage*);
  101. void addToRuleSet(AtomicStringImpl* key, AtomRuleMap&, const RuleData&);
  102. void addRegionRule(StyleRuleRegion*, bool hasDocumentSecurityOrigin);
  103. void shrinkToFit();
  104. void disableAutoShrinkToFit() { m_autoShrinkToFitEnabled = false; }
  105. const RuleFeatureSet& features() const { return m_features; }
  106. const Vector<RuleData>* idRules(AtomicStringImpl* key) const { return m_idRules.get(key); }
  107. const Vector<RuleData>* classRules(AtomicStringImpl* key) const { return m_classRules.get(key); }
  108. const Vector<RuleData>* tagRules(AtomicStringImpl* key) const { return m_tagRules.get(key); }
  109. const Vector<RuleData>* shadowPseudoElementRules(AtomicStringImpl* key) const { return m_shadowPseudoElementRules.get(key); }
  110. const Vector<RuleData>* linkPseudoClassRules() const { return &m_linkPseudoClassRules; }
  111. #if ENABLE(VIDEO_TRACK)
  112. const Vector<RuleData>* cuePseudoRules() const { return &m_cuePseudoRules; }
  113. #endif
  114. const Vector<RuleData>* focusPseudoClassRules() const { return &m_focusPseudoClassRules; }
  115. const Vector<RuleData>* universalRules() const { return &m_universalRules; }
  116. const Vector<StyleRulePage*>& pageRules() const { return m_pageRules; }
  117. private:
  118. void addChildRules(const Vector<RefPtr<StyleRuleBase> >&, const MediaQueryEvaluator& medium, StyleResolver*, const ContainerNode* scope, bool hasDocumentSecurityOrigin, AddRuleFlags);
  119. bool findBestRuleSetAndAdd(const CSSSelector*, RuleData&);
  120. public:
  121. RuleSet();
  122. AtomRuleMap m_idRules;
  123. AtomRuleMap m_classRules;
  124. AtomRuleMap m_tagRules;
  125. AtomRuleMap m_shadowPseudoElementRules;
  126. Vector<RuleData> m_linkPseudoClassRules;
  127. #if ENABLE(VIDEO_TRACK)
  128. Vector<RuleData> m_cuePseudoRules;
  129. #endif
  130. Vector<RuleData> m_focusPseudoClassRules;
  131. Vector<RuleData> m_universalRules;
  132. Vector<StyleRulePage*> m_pageRules;
  133. unsigned m_ruleCount;
  134. bool m_autoShrinkToFitEnabled;
  135. RuleFeatureSet m_features;
  136. struct RuleSetSelectorPair {
  137. RuleSetSelectorPair(const CSSSelector* selector, PassOwnPtr<RuleSet> ruleSet) : selector(selector), ruleSet(ruleSet) { }
  138. RuleSetSelectorPair(const RuleSetSelectorPair& rs) : selector(rs.selector), ruleSet(const_cast<RuleSetSelectorPair*>(&rs)->ruleSet.release()) { }
  139. const CSSSelector* selector;
  140. OwnPtr<RuleSet> ruleSet;
  141. };
  142. Vector<RuleSetSelectorPair> m_regionSelectorsAndRuleSets;
  143. };
  144. inline RuleSet::RuleSet()
  145. : m_ruleCount(0)
  146. , m_autoShrinkToFitEnabled(true)
  147. {
  148. }
  149. } // namespace WebCore
  150. #endif // RuleSet_h