Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #ifndef nsContentIndexCache_h__ |
michael@0 | 6 | #define nsContentIndexCache_h__ |
michael@0 | 7 | |
michael@0 | 8 | #include "js/HashTable.h" |
michael@0 | 9 | |
michael@0 | 10 | class nsIContent; |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | namespace dom { |
michael@0 | 14 | class Element; |
michael@0 | 15 | } // namespace dom |
michael@0 | 16 | } // namespace mozilla |
michael@0 | 17 | |
michael@0 | 18 | /* |
michael@0 | 19 | * A class that computes and caches the indices used for :nth-* pseudo-class |
michael@0 | 20 | * matching. |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | class nsNthIndexCache { |
michael@0 | 24 | private: |
michael@0 | 25 | typedef mozilla::dom::Element Element; |
michael@0 | 26 | |
michael@0 | 27 | public: |
michael@0 | 28 | /** |
michael@0 | 29 | * Constructor and destructor out of line so that we don't try to |
michael@0 | 30 | * instantiate the hashtable template all over the place. |
michael@0 | 31 | */ |
michael@0 | 32 | nsNthIndexCache(); |
michael@0 | 33 | ~nsNthIndexCache(); |
michael@0 | 34 | |
michael@0 | 35 | // Returns a 1-based index of the child in its parent. If the child |
michael@0 | 36 | // is not in its parent's child list (i.e., it is anonymous content), |
michael@0 | 37 | // returns 0. |
michael@0 | 38 | // If aCheckEdgeOnly is true, the function will return 1 if the result |
michael@0 | 39 | // is 1, and something other than 1 (maybe or maybe not a valid |
michael@0 | 40 | // result) otherwise. |
michael@0 | 41 | // This must only be called on nodes which have a non-null parent. |
michael@0 | 42 | int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd, |
michael@0 | 43 | bool aCheckEdgeOnly); |
michael@0 | 44 | |
michael@0 | 45 | void Reset(); |
michael@0 | 46 | |
michael@0 | 47 | private: |
michael@0 | 48 | /** |
michael@0 | 49 | * Returns true if aSibling and aElement should be considered in the same |
michael@0 | 50 | * list for nth-index purposes, taking aIsOfType into account. |
michael@0 | 51 | */ |
michael@0 | 52 | inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement, |
michael@0 | 53 | bool aIsOfType); |
michael@0 | 54 | |
michael@0 | 55 | // This node's index for this cache. |
michael@0 | 56 | // If -2, needs to be computed. |
michael@0 | 57 | // If -1, needs to be computed but known not to be 1. |
michael@0 | 58 | // If 0, the node is not at any index in its parent. |
michael@0 | 59 | typedef int32_t CacheEntry; |
michael@0 | 60 | |
michael@0 | 61 | class SystemAllocPolicy { |
michael@0 | 62 | public: |
michael@0 | 63 | void *malloc_(size_t bytes) { return ::malloc(bytes); } |
michael@0 | 64 | void *calloc_(size_t bytes) { return ::calloc(bytes, 1); } |
michael@0 | 65 | void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); } |
michael@0 | 66 | void free_(void *p) { ::free(p); } |
michael@0 | 67 | void reportAllocOverflow() const {} |
michael@0 | 68 | }; |
michael@0 | 69 | |
michael@0 | 70 | typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>, |
michael@0 | 71 | SystemAllocPolicy> Cache; |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Returns true if aResult has been set to the correct value for aChild and |
michael@0 | 75 | * no more work needs to be done. Returns false otherwise. |
michael@0 | 76 | * |
michael@0 | 77 | * aResult is an inout parameter. The in value is the number of elements |
michael@0 | 78 | * that are in the half-open range (aSibling, aChild] (so including aChild |
michael@0 | 79 | * but not including aSibling) that match aChild. The out value is the |
michael@0 | 80 | * correct index for aChild if this function returns true and the number of |
michael@0 | 81 | * elements in the closed range [aSibling, aChild] that match aChild |
michael@0 | 82 | * otherwise. |
michael@0 | 83 | */ |
michael@0 | 84 | inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling, |
michael@0 | 85 | Element* aChild, |
michael@0 | 86 | bool aIsOfType, |
michael@0 | 87 | bool aIsFromEnd, |
michael@0 | 88 | const Cache& aCache, |
michael@0 | 89 | int32_t& aResult); |
michael@0 | 90 | |
michael@0 | 91 | // Caches of indices for :nth-child(), :nth-last-child(), |
michael@0 | 92 | // :nth-of-type(), :nth-last-of-type(), keyed by Element*. |
michael@0 | 93 | // |
michael@0 | 94 | // The first subscript is 0 for -child and 1 for -of-type, the second |
michael@0 | 95 | // subscript is 0 for nth- and 1 for nth-last-. |
michael@0 | 96 | Cache mCaches[2][2]; |
michael@0 | 97 | }; |
michael@0 | 98 | |
michael@0 | 99 | #endif /* nsContentIndexCache_h__ */ |