LCOV - code coverage report
Current view: top level - layout/style - nsNthIndexCache.h (source / functions) Hit Total Coverage
Test: output.info Lines: 6 7 85.7 %
Date: 2017-07-14 16:53:18 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source 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__
       7             : 
       8             : #include "js/HashTable.h"
       9             : 
      10             : class nsIContent;
      11             : 
      12             : namespace mozilla {
      13             : namespace dom {
      14             : class Element;
      15             : } // namespace dom
      16             : } // namespace mozilla
      17             : 
      18             : /*
      19             :  * A class that computes and caches the indices used for :nth-* pseudo-class
      20             :  * matching.
      21             :  */
      22             : 
      23             : class nsNthIndexCache {
      24             : private:
      25             :   typedef mozilla::dom::Element Element;
      26             : 
      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();
      34             : 
      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);
      44             : 
      45             :   void Reset();
      46             : 
      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);
      54             : 
      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;
      60             : 
      61             :   class SystemAllocPolicy {
      62             :   public:
      63             :     void *malloc_(size_t bytes) { return ::malloc(bytes); }
      64             : 
      65             :     template <typename T>
      66           6 :     T *maybe_pod_calloc(size_t numElems) {
      67           6 :       return static_cast<T *>(::calloc(numElems, sizeof(T)));
      68             :     }
      69             : 
      70             :     template <typename T>
      71           6 :     T *pod_calloc(size_t numElems) {
      72           6 :       return maybe_pod_calloc<T>(numElems);
      73             :     }
      74             : 
      75             :     void *realloc_(void *p, size_t bytes) { return ::realloc(p, bytes); }
      76           6 :     void free_(void *p) { ::free(p); }
      77           0 :     void reportAllocOverflow() const {}
      78           6 :     bool checkSimulatedOOM() const { return true; }
      79             :   };
      80             : 
      81             :   typedef js::HashMap<nsIContent*, CacheEntry, js::DefaultHasher<nsIContent*>,
      82             :                       SystemAllocPolicy> Cache;
      83             : 
      84             :   /**
      85             :    * Returns true if aResult has been set to the correct value for aChild and
      86             :    * no more work needs to be done.  Returns false otherwise.
      87             :    *
      88             :    * aResult is an inout parameter.  The in value is the number of elements
      89             :    * that are in the half-open range (aSibling, aChild] (so including aChild
      90             :    * but not including aSibling) that match aChild.  The out value is the
      91             :    * correct index for aChild if this function returns true and the number of
      92             :    * elements in the closed range [aSibling, aChild] that match aChild
      93             :    * otherwise.
      94             :    */
      95             :   inline bool IndexDeterminedFromPreviousSibling(nsIContent* aSibling,
      96             :                                                  Element* aChild,
      97             :                                                  bool aIsOfType,
      98             :                                                  bool aIsFromEnd,
      99             :                                                  const Cache& aCache,
     100             :                                                  int32_t& aResult);
     101             : 
     102             :   // Caches of indices for :nth-child(), :nth-last-child(),
     103             :   // :nth-of-type(), :nth-last-of-type(), keyed by Element*.
     104             :   //
     105             :   // The first subscript is 0 for -child and 1 for -of-type, the second
     106             :   // subscript is 0 for nth- and 1 for nth-last-.
     107             :   Cache mCaches[2][2];
     108             : };
     109             : 
     110             : #endif /* nsContentIndexCache_h__ */

Generated by: LCOV version 1.13