nsRuleWalker.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/. */
  5. /*
  6. * a class that walks the lexicographic tree of rule nodes as style
  7. * rules are matched
  8. */
  9. #ifndef nsRuleWalker_h_
  10. #define nsRuleWalker_h_
  11. #include "nsRuleNode.h"
  12. #include "nsIStyleRule.h"
  13. #include "Declaration.h"
  14. #include "nsQueryObject.h"
  15. class nsRuleWalker {
  16. public:
  17. nsRuleNode* CurrentNode() { return mCurrent; }
  18. void SetCurrentNode(nsRuleNode* aNode) {
  19. NS_ASSERTION(aNode, "Must have node here!");
  20. mCurrent = aNode;
  21. }
  22. nsPresContext* PresContext() const { return mRoot->PresContext(); }
  23. protected:
  24. void DoForward(nsIStyleRule* aRule) {
  25. mCurrent = mCurrent->Transition(aRule, mLevel, mImportance);
  26. NS_POSTCONDITION(mCurrent, "Transition messed up");
  27. }
  28. public:
  29. void Forward(nsIStyleRule* aRule) {
  30. NS_PRECONDITION(!RefPtr<mozilla::css::Declaration>(do_QueryObject(aRule)),
  31. "Calling the wrong Forward() overload");
  32. DoForward(aRule);
  33. }
  34. void Forward(mozilla::css::Declaration* aRule) {
  35. DoForward(aRule);
  36. mCheckForImportantRules =
  37. mCheckForImportantRules && !aRule->HasImportantData();
  38. }
  39. // ForwardOnPossiblyCSSRule should only be used by callers that have
  40. // an explicit list of rules they need to walk, with the list
  41. // already containing any important rules they care about.
  42. void ForwardOnPossiblyCSSRule(nsIStyleRule* aRule) {
  43. DoForward(aRule);
  44. }
  45. void Reset() { mCurrent = mRoot; }
  46. bool AtRoot() { return mCurrent == mRoot; }
  47. void SetLevel(mozilla::SheetType aLevel, bool aImportance,
  48. bool aCheckForImportantRules) {
  49. NS_ASSERTION(!aCheckForImportantRules || !aImportance,
  50. "Shouldn't be checking for important rules while walking "
  51. "important rules");
  52. mLevel = aLevel;
  53. mImportance = aImportance;
  54. mCheckForImportantRules = aCheckForImportantRules;
  55. }
  56. mozilla::SheetType GetLevel() const { return mLevel; }
  57. bool GetImportance() const { return mImportance; }
  58. bool GetCheckForImportantRules() const { return mCheckForImportantRules; }
  59. bool AuthorStyleDisabled() const { return mAuthorStyleDisabled; }
  60. // We define the visited-relevant link to be the link that is the
  61. // nearest self-or-ancestor to the node being matched.
  62. enum VisitedHandlingType {
  63. // Do rule matching as though all links are unvisited.
  64. eRelevantLinkUnvisited,
  65. // Do rule matching as though the relevant link is visited and all
  66. // other links are unvisited.
  67. eRelevantLinkVisited,
  68. // Do rule matching as though a rule should match if it would match
  69. // given any set of visitedness states. (used by users other than
  70. // nsRuleWalker)
  71. eLinksVisitedOrUnvisited
  72. };
  73. private:
  74. nsRuleNode* mCurrent; // Our current position. Never null.
  75. nsRuleNode* mRoot; // The root of the tree we're walking.
  76. mozilla::SheetType mLevel;
  77. bool mImportance;
  78. bool mCheckForImportantRules; // If true, check for important rules as
  79. // we walk and set to false if we find
  80. // one.
  81. bool mAuthorStyleDisabled;
  82. public:
  83. nsRuleWalker(nsRuleNode* aRoot, bool aAuthorStyleDisabled)
  84. : mCurrent(aRoot)
  85. , mRoot(aRoot)
  86. , mAuthorStyleDisabled(aAuthorStyleDisabled)
  87. {
  88. NS_ASSERTION(mCurrent, "Caller screwed up and gave us null node");
  89. MOZ_COUNT_CTOR(nsRuleWalker);
  90. }
  91. ~nsRuleWalker() { MOZ_COUNT_DTOR(nsRuleWalker); }
  92. };
  93. #endif /* !defined(nsRuleWalker_h_) */