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 :
40 : /*
41 : * A class that computes and caches the indices used for :nth-* pseudo-class
42 : * matching.
43 : */
44 :
45 : #include "nsNthIndexCache.h"
46 : #include "nsIContent.h"
47 :
48 110 : nsNthIndexCache::nsNthIndexCache()
49 : {
50 110 : }
51 :
52 660 : nsNthIndexCache::~nsNthIndexCache()
53 : {
54 660 : }
55 :
56 : void
57 0 : nsNthIndexCache::Reset()
58 : {
59 0 : mCaches[0][0].clear();
60 0 : mCaches[0][1].clear();
61 0 : mCaches[1][0].clear();
62 0 : mCaches[1][1].clear();
63 0 : }
64 :
65 : inline bool
66 0 : nsNthIndexCache::SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
67 : bool aIsOfType)
68 : {
69 0 : return aSibling->IsElement() &&
70 0 : (!aIsOfType ||
71 0 : aSibling->NodeInfo()->NameAndNamespaceEquals(aElement->NodeInfo()));
72 : }
73 :
74 : inline bool
75 0 : nsNthIndexCache::IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
76 : Element* aChild,
77 : bool aIsOfType,
78 : bool aIsFromEnd,
79 : const Cache& aCache,
80 : PRInt32& aResult)
81 : {
82 0 : if (SiblingMatchesElement(aSibling, aChild, aIsOfType)) {
83 0 : Cache::Ptr siblingEntry = aCache.lookup(aSibling);
84 0 : if (siblingEntry) {
85 0 : PRInt32 siblingIndex = siblingEntry->value;
86 0 : NS_ASSERTION(siblingIndex != 0,
87 : "How can a non-anonymous node have an anonymous sibling?");
88 0 : if (siblingIndex > 0) {
89 : // At this point, aResult is a count of how many elements matching
90 : // aChild we have seen after aSibling, including aChild itself.
91 : // |siblingIndex| is the index of aSibling.
92 : // So if aIsFromEnd, we want |aResult = siblingIndex - aResult| and
93 : // otherwise we want |aResult = siblingIndex + aResult|.
94 : NS_ABORT_IF_FALSE(aIsFromEnd == 0 || aIsFromEnd == 1,
95 : "Bogus bool value");
96 0 : aResult = siblingIndex + aResult * (1 - 2 * aIsFromEnd);
97 0 : return true;
98 : }
99 : }
100 :
101 0 : ++aResult;
102 : }
103 :
104 0 : return false;
105 : }
106 :
107 : PRInt32
108 0 : nsNthIndexCache::GetNthIndex(Element* aChild, bool aIsOfType,
109 : bool aIsFromEnd, bool aCheckEdgeOnly)
110 : {
111 0 : NS_ASSERTION(aChild->GetParent(), "caller should check GetParent()");
112 :
113 0 : if (aChild->IsRootOfAnonymousSubtree()) {
114 0 : return 0;
115 : }
116 :
117 0 : Cache &cache = mCaches[aIsOfType][aIsFromEnd];
118 :
119 0 : if (!cache.initialized() && !cache.init()) {
120 : // Give up and just don't match.
121 0 : return 0;
122 : }
123 :
124 0 : Cache::AddPtr entry = cache.lookupForAdd(aChild);
125 :
126 : // Default the value to -2 when adding
127 0 : if (!entry && !cache.add(entry, aChild, -2)) {
128 : // No good; don't match.
129 0 : return 0;
130 : }
131 :
132 0 : PRInt32 &slot = entry->value;
133 0 : if (slot != -2 && (slot != -1 || aCheckEdgeOnly)) {
134 0 : return slot;
135 : }
136 :
137 0 : PRInt32 result = 1;
138 0 : if (aCheckEdgeOnly) {
139 : // The caller only cares whether or not the result is 1, so we can
140 : // stop as soon as we see any other elements that match us.
141 0 : if (aIsFromEnd) {
142 0 : for (nsIContent *cur = aChild->GetNextSibling();
143 : cur;
144 0 : cur = cur->GetNextSibling()) {
145 0 : if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
146 0 : result = -1;
147 0 : break;
148 : }
149 : }
150 : } else {
151 0 : for (nsIContent *cur = aChild->GetPreviousSibling();
152 : cur;
153 0 : cur = cur->GetPreviousSibling()) {
154 0 : if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
155 0 : result = -1;
156 0 : break;
157 : }
158 : }
159 : }
160 : } else {
161 : // In the common case, we already have a cached index for one of
162 : // our previous siblings, so check that first.
163 0 : for (nsIContent *cur = aChild->GetPreviousSibling();
164 : cur;
165 0 : cur = cur->GetPreviousSibling()) {
166 0 : if (IndexDeterminedFromPreviousSibling(cur, aChild, aIsOfType,
167 0 : aIsFromEnd, cache, result)) {
168 0 : slot = result;
169 0 : return result;
170 : }
171 : }
172 :
173 : // Now if aIsFromEnd we lose: need to actually compute our index,
174 : // since looking at previous siblings wouldn't have told us
175 : // anything about it. Note that it doesn't make sense to do cache
176 : // lookups on our following siblings, since chances are the cache
177 : // is not primed for them.
178 0 : if (aIsFromEnd) {
179 0 : result = 1;
180 0 : for (nsIContent *cur = aChild->GetNextSibling();
181 : cur;
182 0 : cur = cur->GetNextSibling()) {
183 0 : if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
184 0 : ++result;
185 : }
186 : }
187 : }
188 : }
189 :
190 0 : slot = result;
191 0 : return result;
192 : }
|