Back to index

lightning-sunbird  0.9+nobinonly
nsTreeStyleCache.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Dave Hyatt <hyatt@mozilla.org> (Original Author)
00024  *   Jan Varga <varga@ku.sk>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsTreeStyleCache_h__
00041 #define nsTreeStyleCache_h__
00042 
00043 #include "nsHashtable.h"
00044 #include "nsIAtom.h"
00045 #include "nsICSSPseudoComparator.h"
00046 #include "nsStyleContext.h"
00047 
00048 class nsDFAState : public nsHashKey
00049 {
00050 public:
00051   PRUint32 mStateID;
00052 
00053   nsDFAState(PRUint32 aID) :mStateID(aID) {};
00054 
00055   PRUint32 GetStateID() { return mStateID; };
00056 
00057   PRUint32 HashCode(void) const {
00058     return mStateID;
00059   }
00060 
00061   PRBool Equals(const nsHashKey *aKey) const {
00062     nsDFAState* key = (nsDFAState*)aKey;
00063     return key->mStateID == mStateID;
00064   }
00065 
00066   nsHashKey *Clone(void) const {
00067     return new nsDFAState(mStateID);
00068   }
00069 };
00070 
00071 class nsTransitionKey : public nsHashKey
00072 {
00073 public:
00074   PRUint32 mState;
00075   nsCOMPtr<nsIAtom> mInputSymbol;
00076 
00077   nsTransitionKey(PRUint32 aState, nsIAtom* aSymbol) :mState(aState), mInputSymbol(aSymbol) {};
00078 
00079   PRUint32 HashCode(void) const {
00080     // Make a 32-bit integer that combines the low-order 16 bits of the state and the input symbol.
00081     PRInt32 hb = mState << 16;
00082     PRInt32 lb = (NS_PTR_TO_INT32(mInputSymbol.get()) << 16) >> 16;
00083     return hb+lb;
00084   }
00085 
00086   PRBool Equals(const nsHashKey *aKey) const {
00087     nsTransitionKey* key = (nsTransitionKey*)aKey;
00088     return key->mState == mState && key->mInputSymbol == mInputSymbol;
00089   }
00090 
00091   nsHashKey *Clone(void) const {
00092     return new nsTransitionKey(mState, mInputSymbol);
00093   }
00094 };
00095 
00096 class nsTreeStyleCache 
00097 {
00098 public:
00099   nsTreeStyleCache() :mTransitionTable(nsnull), mCache(nsnull), mNextState(0) {};
00100   ~nsTreeStyleCache() { Clear(); };
00101 
00102   void Clear() { delete mTransitionTable; mTransitionTable = nsnull; delete mCache; mCache = nsnull; mNextState = 0; };
00103 
00104   nsStyleContext* GetStyleContext(nsICSSPseudoComparator* aComparator,
00105                                   nsPresContext* aPresContext, 
00106                                   nsIContent* aContent, 
00107                                   nsStyleContext* aContext,
00108                                   nsIAtom* aPseudoElement,
00109                                   nsISupportsArray* aInputWord);
00110 
00111   static PRBool PR_CALLBACK DeleteDFAState(nsHashKey *aKey, void *aData, void *closure);
00112 
00113   static PRBool PR_CALLBACK ReleaseStyleContext(nsHashKey *aKey, void *aData, void *closure);
00114 
00115 protected:
00116   // A transition table for a deterministic finite automaton.  The DFA
00117   // takes as its input a single pseudoelement and an ordered set of properties.  
00118   // It transitions on an input word that is the concatenation of the pseudoelement supplied
00119   // with the properties in the array.
00120   // 
00121   // It transitions from state to state by looking up entries in the transition table (which is
00122   // a mapping from (S,i)->S', where S is the current state, i is the next
00123   // property in the input word, and S' is the state to transition to.
00124   //
00125   // If S' is not found, it is constructed and entered into the hashtable
00126   // under the key (S,i).
00127   //
00128   // Once the entire word has been consumed, the final state is used
00129   // to reference the cache table to locate the style context.
00130   nsObjectHashtable* mTransitionTable;
00131 
00132   // The cache of all active style contexts.  This is a hash from 
00133   // a final state in the DFA, Sf, to the resultant style context.
00134   nsObjectHashtable* mCache;
00135 
00136   // An integer counter that is used when we need to make new states in the
00137   // DFA.
00138   PRUint32 mNextState;
00139 };
00140 
00141 #endif // nsTreeStyleCache_h__