layout/style/nsNthIndexCache.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/nsNthIndexCache.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * A class that computes and caches the indices used for :nth-* pseudo-class
    1.11 + * matching.
    1.12 + */
    1.13 +
    1.14 +#include "nsNthIndexCache.h"
    1.15 +#include "mozilla/dom/Element.h"
    1.16 +
    1.17 +nsNthIndexCache::nsNthIndexCache()
    1.18 +{
    1.19 +}
    1.20 +
    1.21 +nsNthIndexCache::~nsNthIndexCache()
    1.22 +{
    1.23 +}
    1.24 +
    1.25 +void
    1.26 +nsNthIndexCache::Reset()
    1.27 +{
    1.28 +  mCaches[0][0].clear();
    1.29 +  mCaches[0][1].clear();
    1.30 +  mCaches[1][0].clear();
    1.31 +  mCaches[1][1].clear();
    1.32 +}
    1.33 +
    1.34 +inline bool
    1.35 +nsNthIndexCache::SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
    1.36 +                                       bool aIsOfType)
    1.37 +{
    1.38 +  return aSibling->IsElement() &&
    1.39 +    (!aIsOfType ||
    1.40 +     aSibling->NodeInfo()->NameAndNamespaceEquals(aElement->NodeInfo()));
    1.41 +}
    1.42 +
    1.43 +inline bool
    1.44 +nsNthIndexCache::IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
    1.45 +                                                    Element* aChild,
    1.46 +                                                    bool aIsOfType,
    1.47 +                                                    bool aIsFromEnd,
    1.48 +                                                    const Cache& aCache,
    1.49 +                                                    int32_t& aResult)
    1.50 +{
    1.51 +  if (SiblingMatchesElement(aSibling, aChild, aIsOfType)) {
    1.52 +    Cache::Ptr siblingEntry = aCache.lookup(aSibling);
    1.53 +    if (siblingEntry) {
    1.54 +      int32_t siblingIndex = siblingEntry->value();
    1.55 +      NS_ASSERTION(siblingIndex != 0,
    1.56 +                   "How can a non-anonymous node have an anonymous sibling?");
    1.57 +      if (siblingIndex > 0) {
    1.58 +        // At this point, aResult is a count of how many elements matching
    1.59 +        // aChild we have seen after aSibling, including aChild itself.
    1.60 +        // |siblingIndex| is the index of aSibling.
    1.61 +        // So if aIsFromEnd, we want |aResult = siblingIndex - aResult| and
    1.62 +        // otherwise we want |aResult = siblingIndex + aResult|.
    1.63 +        aResult = siblingIndex + aResult * (1 - 2 * aIsFromEnd);
    1.64 +        return true;
    1.65 +      }
    1.66 +    }
    1.67 +
    1.68 +    ++aResult;
    1.69 +  }
    1.70 +
    1.71 +  return false;
    1.72 +}
    1.73 +
    1.74 +int32_t
    1.75 +nsNthIndexCache::GetNthIndex(Element* aChild, bool aIsOfType,
    1.76 +                             bool aIsFromEnd, bool aCheckEdgeOnly)
    1.77 +{
    1.78 +  NS_ASSERTION(aChild->GetParent(), "caller should check GetParent()");
    1.79 +
    1.80 +  if (aChild->IsRootOfAnonymousSubtree()) {
    1.81 +    return 0;
    1.82 +  }
    1.83 +
    1.84 +  Cache &cache = mCaches[aIsOfType][aIsFromEnd];
    1.85 +
    1.86 +  if (!cache.initialized() && !cache.init()) {
    1.87 +    // Give up and just don't match.
    1.88 +    return 0;
    1.89 +  }
    1.90 +
    1.91 +  Cache::AddPtr entry = cache.lookupForAdd(aChild);
    1.92 +
    1.93 +  // Default the value to -2 when adding
    1.94 +  if (!entry && !cache.add(entry, aChild, -2)) {
    1.95 +    // No good; don't match.
    1.96 +    return 0;
    1.97 +  }
    1.98 +
    1.99 +  int32_t &slot = entry->value();
   1.100 +  if (slot != -2 && (slot != -1 || aCheckEdgeOnly)) {
   1.101 +    return slot;
   1.102 +  }
   1.103 +  
   1.104 +  int32_t result = 1;
   1.105 +  if (aCheckEdgeOnly) {
   1.106 +    // The caller only cares whether or not the result is 1, so we can
   1.107 +    // stop as soon as we see any other elements that match us.
   1.108 +    if (aIsFromEnd) {
   1.109 +      for (nsIContent *cur = aChild->GetNextSibling();
   1.110 +           cur;
   1.111 +           cur = cur->GetNextSibling()) {
   1.112 +        if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
   1.113 +          result = -1;
   1.114 +          break;
   1.115 +        }
   1.116 +      }
   1.117 +    } else {
   1.118 +      for (nsIContent *cur = aChild->GetPreviousSibling();
   1.119 +           cur;
   1.120 +           cur = cur->GetPreviousSibling()) {
   1.121 +        if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
   1.122 +          result = -1;
   1.123 +          break;
   1.124 +        }
   1.125 +      }
   1.126 +    }
   1.127 +  } else {
   1.128 +    // In the common case, we already have a cached index for one of
   1.129 +    // our previous siblings, so check that first.
   1.130 +    for (nsIContent *cur = aChild->GetPreviousSibling();
   1.131 +         cur;
   1.132 +         cur = cur->GetPreviousSibling()) {
   1.133 +      if (IndexDeterminedFromPreviousSibling(cur, aChild, aIsOfType,
   1.134 +                                             aIsFromEnd, cache, result)) {
   1.135 +        slot = result;
   1.136 +        return result;
   1.137 +      }
   1.138 +    }
   1.139 +
   1.140 +    // Now if aIsFromEnd we lose: need to actually compute our index,
   1.141 +    // since looking at previous siblings wouldn't have told us
   1.142 +    // anything about it.  Note that it doesn't make sense to do cache
   1.143 +    // lookups on our following siblings, since chances are the cache
   1.144 +    // is not primed for them.
   1.145 +    if (aIsFromEnd) {
   1.146 +      result = 1;
   1.147 +      for (nsIContent *cur = aChild->GetNextSibling();
   1.148 +           cur;
   1.149 +           cur = cur->GetNextSibling()) {
   1.150 +        if (SiblingMatchesElement(cur, aChild, aIsOfType)) {
   1.151 +          ++result;
   1.152 +        }
   1.153 +      }
   1.154 +    }
   1.155 +  }
   1.156 +
   1.157 +  slot = result;
   1.158 +  return result;
   1.159 +}

mercurial