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.org 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) 2003
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Dave Hyatt <hyatt@mozilla.org> (Original Author)
24 : * Jan Varga <varga@ku.sk>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsTreeStyleCache_h__
41 : #define nsTreeStyleCache_h__
42 :
43 : #include "nsHashtable.h"
44 : #include "nsIAtom.h"
45 : #include "nsICSSPseudoComparator.h"
46 : #include "nsStyleContext.h"
47 :
48 : class nsISupportsArray;
49 :
50 : class nsDFAState : public nsHashKey
51 0 : {
52 : public:
53 : PRUint32 mStateID;
54 :
55 0 : nsDFAState(PRUint32 aID) :mStateID(aID) {}
56 :
57 0 : PRUint32 GetStateID() { return mStateID; }
58 :
59 0 : PRUint32 HashCode(void) const {
60 0 : return mStateID;
61 : }
62 :
63 0 : bool Equals(const nsHashKey *aKey) const {
64 0 : nsDFAState* key = (nsDFAState*)aKey;
65 0 : return key->mStateID == mStateID;
66 : }
67 :
68 0 : nsHashKey *Clone(void) const {
69 0 : return new nsDFAState(mStateID);
70 : }
71 : };
72 :
73 : class nsTransitionKey : public nsHashKey
74 0 : {
75 : public:
76 : PRUint32 mState;
77 : nsCOMPtr<nsIAtom> mInputSymbol;
78 :
79 0 : nsTransitionKey(PRUint32 aState, nsIAtom* aSymbol) :mState(aState), mInputSymbol(aSymbol) {}
80 :
81 0 : PRUint32 HashCode(void) const {
82 : // Make a 32-bit integer that combines the low-order 16 bits of the state and the input symbol.
83 0 : PRInt32 hb = mState << 16;
84 0 : PRInt32 lb = (NS_PTR_TO_INT32(mInputSymbol.get()) << 16) >> 16;
85 0 : return hb+lb;
86 : }
87 :
88 0 : bool Equals(const nsHashKey *aKey) const {
89 0 : nsTransitionKey* key = (nsTransitionKey*)aKey;
90 0 : return key->mState == mState && key->mInputSymbol == mInputSymbol;
91 : }
92 :
93 0 : nsHashKey *Clone(void) const {
94 0 : return new nsTransitionKey(mState, mInputSymbol);
95 : }
96 : };
97 :
98 : class nsTreeStyleCache
99 : {
100 : public:
101 0 : nsTreeStyleCache() :mTransitionTable(nsnull), mCache(nsnull), mNextState(0) {}
102 0 : ~nsTreeStyleCache() { Clear(); }
103 :
104 0 : void Clear() { delete mTransitionTable; mTransitionTable = nsnull; delete mCache; mCache = nsnull; mNextState = 0; }
105 :
106 : nsStyleContext* GetStyleContext(nsICSSPseudoComparator* aComparator,
107 : nsPresContext* aPresContext,
108 : nsIContent* aContent,
109 : nsStyleContext* aContext,
110 : nsIAtom* aPseudoElement,
111 : nsISupportsArray* aInputWord);
112 :
113 : static bool DeleteDFAState(nsHashKey *aKey, void *aData, void *closure);
114 :
115 : static bool ReleaseStyleContext(nsHashKey *aKey, void *aData, void *closure);
116 :
117 : protected:
118 : // A transition table for a deterministic finite automaton. The DFA
119 : // takes as its input a single pseudoelement and an ordered set of properties.
120 : // It transitions on an input word that is the concatenation of the pseudoelement supplied
121 : // with the properties in the array.
122 : //
123 : // It transitions from state to state by looking up entries in the transition table (which is
124 : // a mapping from (S,i)->S', where S is the current state, i is the next
125 : // property in the input word, and S' is the state to transition to.
126 : //
127 : // If S' is not found, it is constructed and entered into the hashtable
128 : // under the key (S,i).
129 : //
130 : // Once the entire word has been consumed, the final state is used
131 : // to reference the cache table to locate the style context.
132 : nsObjectHashtable* mTransitionTable;
133 :
134 : // The cache of all active style contexts. This is a hash from
135 : // a final state in the DFA, Sf, to the resultant style context.
136 : nsObjectHashtable* mCache;
137 :
138 : // An integer counter that is used when we need to make new states in the
139 : // DFA.
140 : PRUint32 mNextState;
141 : };
142 :
143 : #endif // nsTreeStyleCache_h__
|