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: 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: #include "nsNthIndexCache.h" michael@0: #include "mozilla/dom/Element.h" michael@0: michael@0: nsNthIndexCache::nsNthIndexCache() michael@0: { michael@0: } michael@0: michael@0: nsNthIndexCache::~nsNthIndexCache() michael@0: { michael@0: } michael@0: michael@0: void michael@0: nsNthIndexCache::Reset() michael@0: { michael@0: mCaches[0][0].clear(); michael@0: mCaches[0][1].clear(); michael@0: mCaches[1][0].clear(); michael@0: mCaches[1][1].clear(); michael@0: } michael@0: michael@0: inline bool michael@0: nsNthIndexCache::SiblingMatchesElement(nsIContent* aSibling, Element* aElement, michael@0: bool aIsOfType) michael@0: { michael@0: return aSibling->IsElement() && michael@0: (!aIsOfType || michael@0: aSibling->NodeInfo()->NameAndNamespaceEquals(aElement->NodeInfo())); michael@0: } michael@0: michael@0: inline bool michael@0: nsNthIndexCache::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: if (SiblingMatchesElement(aSibling, aChild, aIsOfType)) { michael@0: Cache::Ptr siblingEntry = aCache.lookup(aSibling); michael@0: if (siblingEntry) { michael@0: int32_t siblingIndex = siblingEntry->value(); michael@0: NS_ASSERTION(siblingIndex != 0, michael@0: "How can a non-anonymous node have an anonymous sibling?"); michael@0: if (siblingIndex > 0) { michael@0: // At this point, aResult is a count of how many elements matching michael@0: // aChild we have seen after aSibling, including aChild itself. michael@0: // |siblingIndex| is the index of aSibling. michael@0: // So if aIsFromEnd, we want |aResult = siblingIndex - aResult| and michael@0: // otherwise we want |aResult = siblingIndex + aResult|. michael@0: aResult = siblingIndex + aResult * (1 - 2 * aIsFromEnd); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: ++aResult; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: int32_t michael@0: nsNthIndexCache::GetNthIndex(Element* aChild, bool aIsOfType, michael@0: bool aIsFromEnd, bool aCheckEdgeOnly) michael@0: { michael@0: NS_ASSERTION(aChild->GetParent(), "caller should check GetParent()"); michael@0: michael@0: if (aChild->IsRootOfAnonymousSubtree()) { michael@0: return 0; michael@0: } michael@0: michael@0: Cache &cache = mCaches[aIsOfType][aIsFromEnd]; michael@0: michael@0: if (!cache.initialized() && !cache.init()) { michael@0: // Give up and just don't match. michael@0: return 0; michael@0: } michael@0: michael@0: Cache::AddPtr entry = cache.lookupForAdd(aChild); michael@0: michael@0: // Default the value to -2 when adding michael@0: if (!entry && !cache.add(entry, aChild, -2)) { michael@0: // No good; don't match. michael@0: return 0; michael@0: } michael@0: michael@0: int32_t &slot = entry->value(); michael@0: if (slot != -2 && (slot != -1 || aCheckEdgeOnly)) { michael@0: return slot; michael@0: } michael@0: michael@0: int32_t result = 1; michael@0: if (aCheckEdgeOnly) { michael@0: // The caller only cares whether or not the result is 1, so we can michael@0: // stop as soon as we see any other elements that match us. michael@0: if (aIsFromEnd) { michael@0: for (nsIContent *cur = aChild->GetNextSibling(); michael@0: cur; michael@0: cur = cur->GetNextSibling()) { michael@0: if (SiblingMatchesElement(cur, aChild, aIsOfType)) { michael@0: result = -1; michael@0: break; michael@0: } michael@0: } michael@0: } else { michael@0: for (nsIContent *cur = aChild->GetPreviousSibling(); michael@0: cur; michael@0: cur = cur->GetPreviousSibling()) { michael@0: if (SiblingMatchesElement(cur, aChild, aIsOfType)) { michael@0: result = -1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: // In the common case, we already have a cached index for one of michael@0: // our previous siblings, so check that first. michael@0: for (nsIContent *cur = aChild->GetPreviousSibling(); michael@0: cur; michael@0: cur = cur->GetPreviousSibling()) { michael@0: if (IndexDeterminedFromPreviousSibling(cur, aChild, aIsOfType, michael@0: aIsFromEnd, cache, result)) { michael@0: slot = result; michael@0: return result; michael@0: } michael@0: } michael@0: michael@0: // Now if aIsFromEnd we lose: need to actually compute our index, michael@0: // since looking at previous siblings wouldn't have told us michael@0: // anything about it. Note that it doesn't make sense to do cache michael@0: // lookups on our following siblings, since chances are the cache michael@0: // is not primed for them. michael@0: if (aIsFromEnd) { michael@0: result = 1; michael@0: for (nsIContent *cur = aChild->GetNextSibling(); michael@0: cur; michael@0: cur = cur->GetNextSibling()) { michael@0: if (SiblingMatchesElement(cur, aChild, aIsOfType)) { michael@0: ++result; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: slot = result; michael@0: return result; michael@0: }