nsNthIndexCache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 nsContentIndexCache_h__
  6. #define nsContentIndexCache_h__
  7. #include "js/HashTable.h"
  8. class nsIContent;
  9. namespace mozilla {
  10. namespace dom {
  11. class Element;
  12. } // namespace dom
  13. } // namespace mozilla
  14. /*
  15. * A class that computes and caches the indices used for :nth-* pseudo-class
  16. * matching.
  17. */
  18. class nsNthIndexCache {
  19. private:
  20. typedef mozilla::dom::Element Element;
  21. public:
  22. /**
  23. * Constructor and destructor out of line so that we don't try to
  24. * instantiate the hashtable template all over the place.
  25. */
  26. nsNthIndexCache();
  27. ~nsNthIndexCache();
  28. // Returns a 1-based index of the child in its parent. If the child
  29. // is not in its parent's child list (i.e., it is anonymous content),
  30. // returns 0.
  31. // If aCheckEdgeOnly is true, the function will return 1 if the result
  32. // is 1, and something other than 1 (maybe or maybe not a valid
  33. // result) otherwise.
  34. // This must only be called on nodes which have a non-null parent.
  35. int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd,
  36. bool aCheckEdgeOnly);
  37. void Reset();
  38. private:
  39. /**
  40. * Returns true if aSibling and aElement should be considered in the same
  41. * list for nth-index purposes, taking aIsOfType into account.
  42. */
  43. inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
  44. bool aIsOfType);
  45. // This node's index for this cache.
  46. // If -2, needs to be computed.
  47. // If -1, needs to be computed but known not to be 1.
  48. // If 0, the node is not at any index in its parent.
  49. typedef int32_t CacheEntry;
  50. class SystemAllocPolicy {
  51. public:
  52. void *malloc_(size_t bytes) { return ::malloc(bytes); }
  53. template <typename T>
  54. T *maybe_pod_calloc(size_t numElems) {
  55. return static_cast<T *>(::calloc(numElems, sizeof(T)));
  56. }
  57. template <typename T>
  58. T *pod_calloc(size_t numElems) {
  59. return maybe_pod_calloc<T>(numElems);
  60. }
  61. void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); }
  62. void free_(void *p) { ::free(p); }
  63. void reportAllocOverflow() const {}
  64. bool checkSimulatedOOM() const { return true; }
  65. };
  66. typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
  67. SystemAllocPolicy> Cache;
  68. /**
  69. * Returns true if aResult has been set to the correct value for aChild and
  70. * no more work needs to be done. Returns false otherwise.
  71. *
  72. * aResult is an inout parameter. The in value is the number of elements
  73. * that are in the half-open range (aSibling, aChild] (so including aChild
  74. * but not including aSibling) that match aChild. The out value is the
  75. * correct index for aChild if this function returns true and the number of
  76. * elements in the closed range [aSibling, aChild] that match aChild
  77. * otherwise.
  78. */
  79. inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
  80. Element* aChild,
  81. bool aIsOfType,
  82. bool aIsFromEnd,
  83. const Cache& aCache,
  84. int32_t& aResult);
  85. // Caches of indices for :nth-child(), :nth-last-child(),
  86. // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
  87. //
  88. // The first subscript is 0 for -child and 1 for -of-type, the second
  89. // subscript is 0 for nth- and 1 for nth-last-.
  90. Cache mCaches[2][2];
  91. };
  92. #endif /* nsContentIndexCache_h__ */