nsTreeStyleCache.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef nsTreeStyleCache_h__
  6. #define nsTreeStyleCache_h__
  7. #include "mozilla/Attributes.h"
  8. #include "nsAutoPtr.h"
  9. #include "nsIAtom.h"
  10. #include "nsCOMArray.h"
  11. #include "nsICSSPseudoComparator.h"
  12. #include "nsRefPtrHashtable.h"
  13. #include "nsStyleContext.h"
  14. typedef nsCOMArray<nsIAtom> AtomArray;
  15. class nsTreeStyleCache
  16. {
  17. public:
  18. nsTreeStyleCache()
  19. : mNextState(0)
  20. {
  21. }
  22. ~nsTreeStyleCache()
  23. {
  24. Clear();
  25. }
  26. void Clear()
  27. {
  28. mTransitionTable = nullptr;
  29. mCache = nullptr;
  30. mNextState = 0;
  31. }
  32. nsStyleContext* GetStyleContext(nsICSSPseudoComparator* aComparator,
  33. nsPresContext* aPresContext,
  34. nsIContent* aContent,
  35. nsStyleContext* aContext,
  36. nsIAtom* aPseudoElement,
  37. const AtomArray & aInputWord);
  38. protected:
  39. typedef uint32_t DFAState;
  40. class Transition final
  41. {
  42. public:
  43. Transition(DFAState aState, nsIAtom* aSymbol);
  44. bool operator==(const Transition& aOther) const;
  45. uint32_t Hash() const;
  46. private:
  47. DFAState mState;
  48. nsCOMPtr<nsIAtom> mInputSymbol;
  49. };
  50. typedef nsDataHashtable<nsGenericHashKey<Transition>, DFAState> TransitionTable;
  51. // A transition table for a deterministic finite automaton. The DFA
  52. // takes as its input a single pseudoelement and an ordered set of properties.
  53. // It transitions on an input word that is the concatenation of the pseudoelement supplied
  54. // with the properties in the array.
  55. //
  56. // It transitions from state to state by looking up entries in the transition table (which is
  57. // a mapping from (S,i)->S', where S is the current state, i is the next
  58. // property in the input word, and S' is the state to transition to.
  59. //
  60. // If S' is not found, it is constructed and entered into the hashtable
  61. // under the key (S,i).
  62. //
  63. // Once the entire word has been consumed, the final state is used
  64. // to reference the cache table to locate the style context.
  65. nsAutoPtr<TransitionTable> mTransitionTable;
  66. // The cache of all active style contexts. This is a hash from
  67. // a final state in the DFA, Sf, to the resultant style context.
  68. typedef nsRefPtrHashtable<nsUint32HashKey, nsStyleContext> StyleContextCache;
  69. nsAutoPtr<StyleContextCache> mCache;
  70. // An integer counter that is used when we need to make new states in the
  71. // DFA.
  72. DFAState mNextState;
  73. };
  74. #endif // nsTreeStyleCache_h__