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__
8 #include "js/HashTable.h"
16 } // namespace mozilla
19 * A class that computes and caches the indices used for :nth-* pseudo-class
23 class nsNthIndexCache
{
25 typedef mozilla::dom::Element Element
;
29 * Constructor and destructor out of line so that we don't try to
30 * instantiate the hashtable template all over the place.
35 // Returns a 1-based index of the child in its parent. If the child
36 // is not in its parent's child list (i.e., it is anonymous content),
38 // If aCheckEdgeOnly is true, the function will return 1 if the result
39 // is 1, and something other than 1 (maybe or maybe not a valid
41 // This must only be called on nodes which have a non-null parent.
42 int32_t GetNthIndex(Element
* aChild
, bool aIsOfType
, bool aIsFromEnd
,
49 * Returns true if aSibling and aElement should be considered in the same
50 * list for nth-index purposes, taking aIsOfType into account.
52 inline bool SiblingMatchesElement(nsIContent
* aSibling
, Element
* aElement
,
55 // This node's index for this cache.
56 // If -2, needs to be computed.
57 // If -1, needs to be computed but known not to be 1.
58 // If 0, the node is not at any index in its parent.
59 typedef int32_t CacheEntry
;
61 class SystemAllocPolicy
{
63 void *malloc_(size_t bytes
) { return ::malloc(bytes
); }
66 T
*pod_calloc(size_t numElems
) {
67 return static_cast<T
*>(::calloc(numElems
, sizeof(T
)));
70 void *realloc_(void *p
, size_t bytes
) { return ::realloc(p
, bytes
); }
71 void free_(void *p
) { ::free(p
); }
72 void reportAllocOverflow() const {}
75 typedef js::HashMap
<nsIContent
*, CacheEntry
, js::DefaultHasher
<nsIContent
*>,
76 SystemAllocPolicy
> Cache
;
79 * Returns true if aResult has been set to the correct value for aChild and
80 * no more work needs to be done. Returns false otherwise.
82 * aResult is an inout parameter. The in value is the number of elements
83 * that are in the half-open range (aSibling, aChild] (so including aChild
84 * but not including aSibling) that match aChild. The out value is the
85 * correct index for aChild if this function returns true and the number of
86 * elements in the closed range [aSibling, aChild] that match aChild
89 inline bool IndexDeterminedFromPreviousSibling(nsIContent
* aSibling
,
96 // Caches of indices for :nth-child(), :nth-last-child(),
97 // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
99 // The first subscript is 0 for -child and 1 for -of-type, the second
100 // subscript is 0 for nth- and 1 for nth-last-.
104 #endif /* nsContentIndexCache_h__ */