michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #ifndef nsContentIndexCache_h__ michael@0: #define nsContentIndexCache_h__ michael@0: michael@0: #include "js/HashTable.h" michael@0: michael@0: class nsIContent; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: class Element; michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: /* michael@0: * A class that computes and caches the indices used for :nth-* pseudo-class michael@0: * matching. michael@0: */ michael@0: michael@0: class nsNthIndexCache { michael@0: private: michael@0: typedef mozilla::dom::Element Element; michael@0: michael@0: public: michael@0: /** michael@0: * Constructor and destructor out of line so that we don't try to michael@0: * instantiate the hashtable template all over the place. michael@0: */ michael@0: nsNthIndexCache(); michael@0: ~nsNthIndexCache(); michael@0: michael@0: // Returns a 1-based index of the child in its parent. If the child michael@0: // is not in its parent's child list (i.e., it is anonymous content), michael@0: // returns 0. michael@0: // If aCheckEdgeOnly is true, the function will return 1 if the result michael@0: // is 1, and something other than 1 (maybe or maybe not a valid michael@0: // result) otherwise. michael@0: // This must only be called on nodes which have a non-null parent. michael@0: int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd, michael@0: bool aCheckEdgeOnly); michael@0: michael@0: void Reset(); michael@0: michael@0: private: michael@0: /** michael@0: * Returns true if aSibling and aElement should be considered in the same michael@0: * list for nth-index purposes, taking aIsOfType into account. michael@0: */ michael@0: inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement, michael@0: bool aIsOfType); michael@0: michael@0: // This node's index for this cache. michael@0: // If -2, needs to be computed. michael@0: // If -1, needs to be computed but known not to be 1. michael@0: // If 0, the node is not at any index in its parent. michael@0: typedef int32_t CacheEntry; michael@0: michael@0: class SystemAllocPolicy { michael@0: public: michael@0: void *malloc_(size_t bytes) { return ::malloc(bytes); } michael@0: void *calloc_(size_t bytes) { return ::calloc(bytes, 1); } michael@0: void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); } michael@0: void free_(void *p) { ::free(p); } michael@0: void reportAllocOverflow() const {} michael@0: }; michael@0: michael@0: typedef js::HashMap, michael@0: SystemAllocPolicy> Cache; michael@0: michael@0: /** michael@0: * Returns true if aResult has been set to the correct value for aChild and michael@0: * no more work needs to be done. Returns false otherwise. michael@0: * michael@0: * aResult is an inout parameter. The in value is the number of elements michael@0: * that are in the half-open range (aSibling, aChild] (so including aChild michael@0: * but not including aSibling) that match aChild. The out value is the michael@0: * correct index for aChild if this function returns true and the number of michael@0: * elements in the closed range [aSibling, aChild] that match aChild michael@0: * otherwise. michael@0: */ michael@0: inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling, michael@0: Element* aChild, michael@0: bool aIsOfType, michael@0: bool aIsFromEnd, michael@0: const Cache& aCache, michael@0: int32_t& aResult); michael@0: michael@0: // Caches of indices for :nth-child(), :nth-last-child(), michael@0: // :nth-of-type(), :nth-last-of-type(), keyed by Element*. michael@0: // michael@0: // The first subscript is 0 for -child and 1 for -of-type, the second michael@0: // subscript is 0 for nth- and 1 for nth-last-. michael@0: Cache mCaches[2][2]; michael@0: }; michael@0: michael@0: #endif /* nsContentIndexCache_h__ */