layout/style/nsNthIndexCache.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #ifndef nsContentIndexCache_h__
     6 #define nsContentIndexCache_h__
     8 #include "js/HashTable.h"
    10 class nsIContent;
    12 namespace mozilla {
    13 namespace dom {
    14 class Element;
    15 } // namespace dom
    16 } // namespace mozilla
    18 /*
    19  * A class that computes and caches the indices used for :nth-* pseudo-class
    20  * matching.
    21  */
    23 class nsNthIndexCache {
    24 private:
    25   typedef mozilla::dom::Element Element;
    27 public:
    28   /**
    29    * Constructor and destructor out of line so that we don't try to
    30    * instantiate the hashtable template all over the place.
    31    */
    32   nsNthIndexCache();
    33   ~nsNthIndexCache();
    35   // Returns a 1-based index of the child in its parent.  If the child
    36   // is not in its parent's child list (i.e., it is anonymous content),
    37   // returns 0.
    38   // If aCheckEdgeOnly is true, the function will return 1 if the result
    39   // is 1, and something other than 1 (maybe or maybe not a valid
    40   // result) otherwise.
    41   // This must only be called on nodes which have a non-null parent.
    42   int32_t GetNthIndex(Element* aChild, bool aIsOfType, bool aIsFromEnd,
    43                       bool aCheckEdgeOnly);
    45   void Reset();
    47 private:
    48   /**
    49    * Returns true if aSibling and aElement should be considered in the same
    50    * list for nth-index purposes, taking aIsOfType into account.
    51    */
    52   inline bool SiblingMatchesElement(nsIContent* aSibling, Element* aElement,
    53                                     bool aIsOfType);
    55   // This node's index for this cache.
    56   // If -2, needs to be computed.
    57   // If -1, needs to be computed but known not to be 1.
    58   // If 0, the node is not at any index in its parent.
    59   typedef int32_t CacheEntry;
    61   class SystemAllocPolicy {
    62   public:
    63     void *malloc_(size_t bytes) { return ::malloc(bytes); }
    64     void *calloc_(size_t bytes) { return ::calloc(bytes, 1); }
    65     void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); }
    66     void free_(void *p) { ::free(p); }
    67     void reportAllocOverflow() const {}
    68   };
    70   typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
    71                       SystemAllocPolicy> Cache;
    73   /**
    74    * Returns true if aResult has been set to the correct value for aChild and
    75    * no more work needs to be done.  Returns false otherwise.
    76    *
    77    * aResult is an inout parameter.  The in value is the number of elements
    78    * that are in the half-open range (aSibling, aChild] (so including aChild
    79    * but not including aSibling) that match aChild.  The out value is the
    80    * correct index for aChild if this function returns true and the number of
    81    * elements in the closed range [aSibling, aChild] that match aChild
    82    * otherwise.
    83    */
    84   inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
    85                                                  Element* aChild,
    86                                                  bool aIsOfType,
    87                                                  bool aIsFromEnd,
    88                                                  const Cache& aCache,
    89                                                  int32_t& aResult);
    91   // Caches of indices for :nth-child(), :nth-last-child(),
    92   // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
    93   //
    94   // The first subscript is 0 for -child and 1 for -of-type, the second
    95   // subscript is 0 for nth- and 1 for nth-last-.
    96   Cache mCaches[2][2];
    97 };
    99 #endif /* nsContentIndexCache_h__ */

mercurial