LCOV - code coverage report
Current view: top level - dom/base - DOMRect.h (source / functions) Hit Total Coverage
Test: output.info Lines: 39 64 60.9 %
Date: 2017-07-14 16:53:18 Functions: 23 41 56.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
       2             : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
       3             : /* This Source Code Form is subject to the terms of the Mozilla Public
       4             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       5             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
       6             : 
       7             : #ifndef MOZILLA_DOMRECT_H_
       8             : #define MOZILLA_DOMRECT_H_
       9             : 
      10             : #include "nsIDOMClientRect.h"
      11             : #include "nsIDOMClientRectList.h"
      12             : #include "nsTArray.h"
      13             : #include "nsCOMPtr.h"
      14             : #include "nsWrapperCache.h"
      15             : #include "nsCycleCollectionParticipant.h"
      16             : #include "mozilla/Attributes.h"
      17             : #include "mozilla/dom/BindingDeclarations.h"
      18             : #include "mozilla/ErrorResult.h"
      19             : #include <algorithm>
      20             : 
      21             : struct nsRect;
      22             : 
      23             : namespace mozilla {
      24             : namespace dom {
      25             : 
      26             : class DOMRectReadOnly : public nsISupports
      27             :                       , public nsWrapperCache
      28             : {
      29             : protected:
      30           0 :   virtual ~DOMRectReadOnly() {}
      31             : 
      32             : public:
      33             :   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
      34          68 :   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
      35             : 
      36           8 :   explicit DOMRectReadOnly(nsISupports* aParent)
      37           8 :     : mParent(aParent)
      38             :   {
      39           8 :   }
      40             : 
      41           7 :   nsISupports* GetParentObject() const
      42             :   {
      43           7 :     MOZ_ASSERT(mParent);
      44           7 :     return mParent;
      45             :   }
      46             :   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
      47             : 
      48             :   virtual double X() const = 0;
      49             :   virtual double Y() const = 0;
      50             :   virtual double Width() const = 0;
      51             :   virtual double Height() const = 0;
      52             : 
      53           2 :   double Left() const
      54             :   {
      55           2 :     double x = X(), w = Width();
      56           2 :     return std::min(x, x + w);
      57             :   }
      58           1 :   double Top() const
      59             :   {
      60           1 :     double y = Y(), h = Height();
      61           1 :     return std::min(y, y + h);
      62             :   }
      63           0 :   double Right() const
      64             :   {
      65           0 :     double x = X(), w = Width();
      66           0 :     return std::max(x, x + w);
      67             :   }
      68           1 :   double Bottom() const
      69             :   {
      70           1 :     double y = Y(), h = Height();
      71           1 :     return std::max(y, y + h);
      72             :   }
      73             : 
      74             : protected:
      75             :   nsCOMPtr<nsISupports> mParent;
      76             : };
      77             : 
      78             : class DOMRect final : public DOMRectReadOnly
      79             :                     , public nsIDOMClientRect
      80             : {
      81             : public:
      82           8 :   explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
      83             :                    double aWidth = 0, double aHeight = 0)
      84           8 :     : DOMRectReadOnly(aParent)
      85             :     , mX(aX)
      86             :     , mY(aY)
      87             :     , mWidth(aWidth)
      88           8 :     , mHeight(aHeight)
      89             :   {
      90           8 :   }
      91             : 
      92             :   NS_DECL_ISUPPORTS_INHERITED
      93             :   NS_DECL_NSIDOMCLIENTRECT
      94             : 
      95             :   static already_AddRefed<DOMRect>
      96             :   Constructor(const GlobalObject& aGlobal, ErrorResult& aRV);
      97             :   static already_AddRefed<DOMRect>
      98             :   Constructor(const GlobalObject& aGlobal, double aX, double aY,
      99             :               double aWidth, double aHeight, ErrorResult& aRV);
     100             : 
     101             :   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
     102             : 
     103           8 :   void SetRect(float aX, float aY, float aWidth, float aHeight) {
     104           8 :     mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
     105           8 :   }
     106             :   void SetLayoutRect(const nsRect& aLayoutRect);
     107             : 
     108           2 :   virtual double X() const override
     109             :   {
     110           2 :     return mX;
     111             :   }
     112           2 :   virtual double Y() const override
     113             :   {
     114           2 :     return mY;
     115             :   }
     116           8 :   virtual double Width() const override
     117             :   {
     118           8 :     return mWidth;
     119             :   }
     120           2 :   virtual double Height() const override
     121             :   {
     122           2 :     return mHeight;
     123             :   }
     124             : 
     125           0 :   void SetX(double aX)
     126             :   {
     127           0 :     mX = aX;
     128           0 :   }
     129           0 :   void SetY(double aY)
     130             :   {
     131           0 :     mY = aY;
     132           0 :   }
     133           0 :   void SetWidth(double aWidth)
     134             :   {
     135           0 :     mWidth = aWidth;
     136           0 :   }
     137           0 :   void SetHeight(double aHeight)
     138             :   {
     139           0 :     mHeight = aHeight;
     140           0 :   }
     141             : 
     142             : protected:
     143             :   double mX, mY, mWidth, mHeight;
     144             : 
     145             : private:
     146           0 :   ~DOMRect() {};
     147             : };
     148             : 
     149             : class DOMRectList final : public nsIDOMClientRectList,
     150             :                           public nsWrapperCache
     151             : {
     152           0 :   ~DOMRectList() {}
     153             : 
     154             : public:
     155           1 :   explicit DOMRectList(nsISupports *aParent) : mParent(aParent)
     156             :   {
     157           1 :   }
     158             : 
     159             :   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     160          19 :   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
     161             : 
     162             :   NS_DECL_NSIDOMCLIENTRECTLIST
     163             : 
     164             :   virtual JSObject* WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
     165             : 
     166           1 :   nsISupports* GetParentObject()
     167             :   {
     168           1 :     return mParent;
     169             :   }
     170             : 
     171           1 :   void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
     172             : 
     173             :   static DOMRectList* FromSupports(nsISupports* aSupports)
     174             :   {
     175             : #ifdef DEBUG
     176             :     {
     177             :       nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
     178             : 
     179             :       // If this assertion fires the QI implementation for the object in
     180             :       // question doesn't use the nsIDOMClientRectList pointer as the nsISupports
     181             :       // pointer. That must be fixed, or we'll crash...
     182             :       NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
     183             :                    "Uh, fix QI!");
     184             :     }
     185             : #endif
     186             : 
     187             :     return static_cast<DOMRectList*>(aSupports);
     188             :   }
     189             : 
     190           1 :   uint32_t Length()
     191             :   {
     192           1 :     return mArray.Length();
     193             :   }
     194           0 :   DOMRect* Item(uint32_t aIndex)
     195             :   {
     196           0 :     return mArray.SafeElementAt(aIndex);
     197             :   }
     198           0 :   DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
     199             :   {
     200           0 :     aFound = aIndex < mArray.Length();
     201           0 :     if (!aFound) {
     202           0 :       return nullptr;
     203             :     }
     204           0 :     return mArray[aIndex];
     205             :   }
     206             : 
     207             : protected:
     208             :   nsTArray<RefPtr<DOMRect> > mArray;
     209             :   nsCOMPtr<nsISupports> mParent;
     210             : };
     211             : 
     212             : } // namespace dom
     213             : } // namespace mozilla
     214             : 
     215             : #endif /*MOZILLA_DOMRECT_H_*/

Generated by: LCOV version 1.13