1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Original Author: David W. Hyatt (hyatt@netscape.com)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /*
40 : * a class that walks the lexicographic tree of rule nodes as style
41 : * rules are matched
42 : */
43 :
44 : #ifndef nsRuleWalker_h_
45 : #define nsRuleWalker_h_
46 :
47 : #include "nsRuleNode.h"
48 : #include "nsIStyleRule.h"
49 : #include "StyleRule.h"
50 :
51 : class nsRuleWalker {
52 : public:
53 0 : nsRuleNode* CurrentNode() { return mCurrent; }
54 0 : void SetCurrentNode(nsRuleNode* aNode) {
55 0 : NS_ASSERTION(aNode, "Must have node here!");
56 0 : mCurrent = aNode;
57 0 : }
58 :
59 : protected:
60 0 : void DoForward(nsIStyleRule* aRule) {
61 0 : mCurrent = mCurrent->Transition(aRule, mLevel, mImportance);
62 0 : NS_POSTCONDITION(mCurrent, "Transition messed up");
63 0 : }
64 :
65 : public:
66 0 : void Forward(nsIStyleRule* aRule) {
67 0 : NS_PRECONDITION(!nsRefPtr<mozilla::css::StyleRule>(do_QueryObject(aRule)),
68 : "Calling the wrong Forward() overload");
69 0 : DoForward(aRule);
70 0 : }
71 0 : void Forward(mozilla::css::StyleRule* aRule) {
72 0 : DoForward(aRule);
73 : mCheckForImportantRules =
74 0 : mCheckForImportantRules && !aRule->GetImportantRule();
75 0 : }
76 : // ForwardOnPossiblyCSSRule should only be used by callers that have
77 : // an explicit list of rules they need to walk, with the list
78 : // already containing any important rules they care about.
79 0 : void ForwardOnPossiblyCSSRule(nsIStyleRule* aRule) {
80 0 : DoForward(aRule);
81 0 : }
82 :
83 0 : void Reset() { mCurrent = mRoot; }
84 :
85 : bool AtRoot() { return mCurrent == mRoot; }
86 :
87 0 : void SetLevel(PRUint8 aLevel, bool aImportance,
88 : bool aCheckForImportantRules) {
89 0 : NS_ASSERTION(!aCheckForImportantRules || !aImportance,
90 : "Shouldn't be checking for important rules while walking "
91 : "important rules");
92 0 : mLevel = aLevel;
93 0 : mImportance = aImportance;
94 0 : mCheckForImportantRules = aCheckForImportantRules;
95 0 : }
96 : PRUint8 GetLevel() const { return mLevel; }
97 : bool GetImportance() const { return mImportance; }
98 0 : bool GetCheckForImportantRules() const { return mCheckForImportantRules; }
99 :
100 : // We define the visited-relevant link to be the link that is the
101 : // nearest self-or-ancestor to the node being matched.
102 : enum VisitedHandlingType {
103 : // Do rule matching as though all links are unvisited.
104 : eRelevantLinkUnvisited,
105 : // Do rule matching as though the relevant link is visited and all
106 : // other links are unvisited.
107 : eRelevantLinkVisited,
108 : // Do rule matching as though a rule should match if it would match
109 : // given any set of visitedness states. (used by users other than
110 : // nsRuleWalker)
111 : eLinksVisitedOrUnvisited
112 : };
113 :
114 : private:
115 : nsRuleNode* mCurrent; // Our current position. Never null.
116 : nsRuleNode* mRoot; // The root of the tree we're walking.
117 : PRUint8 mLevel; // an nsStyleSet::sheetType
118 : bool mImportance;
119 : bool mCheckForImportantRules; // If true, check for important rules as
120 : // we walk and set to false if we find
121 : // one.
122 :
123 : public:
124 0 : nsRuleWalker(nsRuleNode* aRoot)
125 : : mCurrent(aRoot)
126 0 : , mRoot(aRoot)
127 : {
128 0 : NS_ASSERTION(mCurrent, "Caller screwed up and gave us null node");
129 0 : MOZ_COUNT_CTOR(nsRuleWalker);
130 0 : }
131 0 : ~nsRuleWalker() { MOZ_COUNT_DTOR(nsRuleWalker); }
132 : };
133 :
134 : #endif /* !defined(nsRuleWalker_h_) */
|