1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Boris Zbarsky <bzbarsky@mit.edu> (original author)
24 : * L. David Baron <dbaron@dbaron.org>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 : #ifndef nsContentIndexCache_h__
40 : #define nsContentIndexCache_h__
41 :
42 : #include "nscore.h"
43 : #include "js/HashTable.h"
44 : #include "mozilla/dom/Element.h"
45 :
46 : /*
47 : * A class that computes and caches the indices used for :nth-* pseudo-class
48 : * matching.
49 : */
50 :
51 : class nsNthIndexCache {
52 : private:
53 : typedef mozilla::dom::Element Element;
54 :
55 : public:
56 : /**
57 : * Constructor and destructor out of line so that we don't try to
58 : * instantiate the hashtable template all over the place.
59 : */
60 : nsNthIndexCache();
61 : ~nsNthIndexCache();
62 :
63 : // Returns a 1-based index of the child in its parent. If the child
64 : // is not in its parent's child list (i.e., it is anonymous content),
65 : // returns 0.
66 : // If aCheckEdgeOnly is true, the function will return 1 if the result
67 : // is 1, and something other than 1 (maybe or maybe not a valid
68 : // result) otherwise.
69 : // This must only be called on nodes which have a non-null parent.
70 : PRInt32 GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd,
71 : bool aCheckEdgeOnly);
72 :
73 : void Reset();
74 :
75 : private:
76 : /**
77 : * Returns true if aSibling and aElement should be considered in the same
78 : * list for nth-index purposes, taking aIsOfType into account.
79 : */
80 : inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
81 : bool aIsOfType);
82 :
83 : // This node's index for this cache.
84 : // If -2, needs to be computed.
85 : // If -1, needs to be computed but known not to be 1.
86 : // If 0, the node is not at any index in its parent.
87 : typedef PRInt32 CacheEntry;
88 :
89 : class SystemAllocPolicy {
90 : public:
91 0 : void *malloc_(size_t bytes) { return ::malloc(bytes); }
92 : void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); }
93 0 : void free_(void *p) { ::free(p); }
94 0 : void reportAllocOverflow() const {}
95 : };
96 :
97 : typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
98 : SystemAllocPolicy> Cache;
99 :
100 : /**
101 : * Returns true if aResult has been set to the correct value for aChild and
102 : * no more work needs to be done. Returns false otherwise.
103 : *
104 : * aResult is an inout parameter. The in value is the number of elements
105 : * that are in the half-open range (aSibling, aChild] (so including aChild
106 : * but not including aSibling) that match aChild. The out value is the
107 : * correct index for aChild if this function returns true and the number of
108 : * elements in the closed range [aSibling, aChild] that match aChild
109 : * otherwise.
110 : */
111 : inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
112 : Element* aChild,
113 : bool aIsOfType,
114 : bool aIsFromEnd,
115 : const Cache& aCache,
116 : PRInt32& aResult);
117 :
118 : // Caches of indices for :nth-child(), :nth-last-child(),
119 : // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
120 : //
121 : // The first subscript is 0 for -child and 1 for -of-type, the second
122 : // subscript is 0 for nth- and 1 for nth-last-.
123 : Cache mCaches[2][2];
124 : };
125 :
126 : #endif /* nsContentIndexCache_h__ */
|