Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / layout / style / nsNthIndexCache.h
blob23a1c8f3082ab9c531333e52e22cbe4b68858700
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"
10 class nsIContent;
12 namespace mozilla {
13 namespace dom {
14 class Element;
15 } // namespace dom
16 } // namespace mozilla
19 * A class that computes and caches the indices used for :nth-* pseudo-class
20 * matching.
23 class nsNthIndexCache {
24 private:
25 typedef mozilla::dom::Element Element;
27 public:
28 /**
29 * Constructor and destructor out of line so that we don't try to
30 * instantiate the hashtable template all over the place.
32 nsNthIndexCache();
33 ~nsNthIndexCache();
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),
37 // returns 0.
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
40 // result) otherwise.
41 // This must only be called on nodes which have a non-null parent.
42 int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd,
43 bool aCheckEdgeOnly);
45 void Reset();
47 private:
48 /**
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,
53 bool aIsOfType);
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 {
62 public:
63 void *malloc_(size_t bytes) { return ::malloc(bytes); }
65 template <typename T>
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;
78 /**
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
87 * otherwise.
89 inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
90 Element* aChild,
91 bool aIsOfType,
92 bool aIsFromEnd,
93 const Cache& aCache,
94 int32_t& aResult);
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-.
101 Cache mCaches[2][2];
104 #endif /* nsContentIndexCache_h__ */