layout/style/nsNthIndexCache.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/nsNthIndexCache.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     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 +#ifndef nsContentIndexCache_h__
     1.9 +#define nsContentIndexCache_h__
    1.10 +
    1.11 +#include "js/HashTable.h"
    1.12 +
    1.13 +class nsIContent;
    1.14 +
    1.15 +namespace mozilla {
    1.16 +namespace dom {
    1.17 +class Element;
    1.18 +} // namespace dom
    1.19 +} // namespace mozilla
    1.20 +
    1.21 +/*
    1.22 + * A class that computes and caches the indices used for :nth-* pseudo-class
    1.23 + * matching.
    1.24 + */
    1.25 +
    1.26 +class nsNthIndexCache {
    1.27 +private:
    1.28 +  typedef mozilla::dom::Element Element;
    1.29 +
    1.30 +public:
    1.31 +  /**
    1.32 +   * Constructor and destructor out of line so that we don't try to
    1.33 +   * instantiate the hashtable template all over the place.
    1.34 +   */
    1.35 +  nsNthIndexCache();
    1.36 +  ~nsNthIndexCache();
    1.37 +
    1.38 +  // Returns a 1-based index of the child in its parent.  If the child
    1.39 +  // is not in its parent's child list (i.e., it is anonymous content),
    1.40 +  // returns 0.
    1.41 +  // If aCheckEdgeOnly is true, the function will return 1 if the result
    1.42 +  // is 1, and something other than 1 (maybe or maybe not a valid
    1.43 +  // result) otherwise.
    1.44 +  // This must only be called on nodes which have a non-null parent.
    1.45 +  int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd,
    1.46 +                      bool aCheckEdgeOnly);
    1.47 +
    1.48 +  void Reset();
    1.49 +
    1.50 +private:
    1.51 +  /**
    1.52 +   * Returns true if aSibling and aElement should be considered in the same
    1.53 +   * list for nth-index purposes, taking aIsOfType into account.
    1.54 +   */
    1.55 +  inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
    1.56 +                                    bool aIsOfType);
    1.57 +
    1.58 +  // This node's index for this cache.
    1.59 +  // If -2, needs to be computed.
    1.60 +  // If -1, needs to be computed but known not to be 1.
    1.61 +  // If 0, the node is not at any index in its parent.
    1.62 +  typedef int32_t CacheEntry;
    1.63 +
    1.64 +  class SystemAllocPolicy {
    1.65 +  public:
    1.66 +    void *malloc_(size_t bytes) { return ::malloc(bytes); }
    1.67 +    void *calloc_(size_t bytes) { return ::calloc(bytes, 1); }
    1.68 +    void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); }
    1.69 +    void free_(void *p) { ::free(p); }
    1.70 +    void reportAllocOverflow() const {}
    1.71 +  };
    1.72 +
    1.73 +  typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
    1.74 +                      SystemAllocPolicy> Cache;
    1.75 +
    1.76 +  /**
    1.77 +   * Returns true if aResult has been set to the correct value for aChild and
    1.78 +   * no more work needs to be done.  Returns false otherwise.
    1.79 +   *
    1.80 +   * aResult is an inout parameter.  The in value is the number of elements
    1.81 +   * that are in the half-open range (aSibling, aChild] (so including aChild
    1.82 +   * but not including aSibling) that match aChild.  The out value is the
    1.83 +   * correct index for aChild if this function returns true and the number of
    1.84 +   * elements in the closed range [aSibling, aChild] that match aChild
    1.85 +   * otherwise.
    1.86 +   */
    1.87 +  inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
    1.88 +                                                 Element* aChild,
    1.89 +                                                 bool aIsOfType,
    1.90 +                                                 bool aIsFromEnd,
    1.91 +                                                 const Cache& aCache,
    1.92 +                                                 int32_t& aResult);
    1.93 +
    1.94 +  // Caches of indices for :nth-child(), :nth-last-child(),
    1.95 +  // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
    1.96 +  //
    1.97 +  // The first subscript is 0 for -child and 1 for -of-type, the second
    1.98 +  // subscript is 0 for nth- and 1 for nth-last-.
    1.99 +  Cache mCaches[2][2];
   1.100 +};
   1.101 +
   1.102 +#endif /* nsContentIndexCache_h__ */

mercurial