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 #include "Grid.h"
8 
9 #include "GridArea.h"
10 #include "GridDimension.h"
11 #include "mozilla/dom/GridBinding.h"
12 #include "nsGridContainerFrame.h"
13 
14 namespace mozilla {
15 namespace dom {
16 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Grid,mParent,mRows,mCols,mAreas)17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Grid, mParent, mRows, mCols, mAreas)
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(Grid)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(Grid)
20 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Grid)
21   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
22   NS_INTERFACE_MAP_ENTRY(nsISupports)
23 NS_INTERFACE_MAP_END
24 
25 Grid::Grid(nsISupports* aParent,
26            nsGridContainerFrame* aFrame)
27   : mParent(do_QueryInterface(aParent))
28   , mRows(new GridDimension(this))
29   , mCols(new GridDimension(this))
30 {
31   MOZ_ASSERT(aFrame,
32     "Should never be instantiated with a null nsGridContainerFrame");
33 
34   // Construct areas first, because lines may need to reference them
35   // to extract additional names for boundary lines.
36 
37   // Add implicit areas first. Track the names that we add here, because
38   // we will ignore future explicit areas with the same name.
39   nsTHashtable<nsStringHashKey> namesSeen;
40   nsGridContainerFrame::ImplicitNamedAreas* implicitAreas =
41     aFrame->GetImplicitNamedAreas();
42   if (implicitAreas) {
43     for (auto iter = implicitAreas->Iter(); !iter.Done(); iter.Next()) {
44       auto& areaInfo = iter.Data();
45       namesSeen.PutEntry(areaInfo.mName);
46       GridArea* area = new GridArea(this,
47                                     areaInfo.mName,
48                                     GridDeclaration::Implicit,
49                                     areaInfo.mRowStart,
50                                     areaInfo.mRowEnd,
51                                     areaInfo.mColumnStart,
52                                     areaInfo.mColumnEnd);
53       mAreas.AppendElement(area);
54     }
55   }
56 
57   // Add explicit areas next, as long as they don't have the same name
58   // as the implicit areas, because the implicit values override what was
59   // initially available in the explicit areas.
60   nsGridContainerFrame::ExplicitNamedAreas* explicitAreas =
61     aFrame->GetExplicitNamedAreas();
62   if (explicitAreas) {
63     for (auto& areaInfo : *explicitAreas) {
64       if (!namesSeen.Contains(areaInfo.mName)) {
65         GridArea* area = new GridArea(this,
66                                       areaInfo.mName,
67                                       GridDeclaration::Explicit,
68                                       areaInfo.mRowStart,
69                                       areaInfo.mRowEnd,
70                                       areaInfo.mColumnStart,
71                                       areaInfo.mColumnEnd);
72         mAreas.AppendElement(area);
73       }
74     }
75   }
76 
77   // Now construct the tracks and lines.
78   const ComputedGridTrackInfo* rowTrackInfo =
79     aFrame->GetComputedTemplateRows();
80   const ComputedGridLineInfo* rowLineInfo =
81     aFrame->GetComputedTemplateRowLines();
82   mRows->SetTrackInfo(rowTrackInfo);
83   mRows->SetLineInfo(rowTrackInfo, rowLineInfo, mAreas, true);
84 
85   const ComputedGridTrackInfo* columnTrackInfo =
86     aFrame->GetComputedTemplateColumns();
87   const ComputedGridLineInfo* columnLineInfo =
88     aFrame->GetComputedTemplateColumnLines();
89   mCols->SetTrackInfo(columnTrackInfo);
90   mCols->SetLineInfo(columnTrackInfo, columnLineInfo, mAreas, false);
91 }
92 
~Grid()93 Grid::~Grid()
94 {
95 }
96 
97 JSObject*
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)98 Grid::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
99 {
100   return GridBinding::Wrap(aCx, this, aGivenProto);
101 }
102 
103 GridDimension*
Rows() const104 Grid::Rows() const
105 {
106   return mRows;
107 }
108 
109 GridDimension*
Cols() const110 Grid::Cols() const
111 {
112   return mCols;
113 }
114 
115 void
GetAreas(nsTArray<RefPtr<GridArea>> & aAreas) const116 Grid::GetAreas(nsTArray<RefPtr<GridArea>>& aAreas) const
117 {
118   aAreas = mAreas;
119 }
120 
121 } // namespace dom
122 } // namespace mozilla
123