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, nsGridContainerFrame* aFrame)
26     : mParent(do_QueryInterface(aParent)),
27       mRows(new GridDimension(this)),
28       mCols(new GridDimension(this)) {
29   MOZ_ASSERT(aFrame,
30              "Should never be instantiated with a null nsGridContainerFrame");
31 
32   // Construct areas first, because lines may need to reference them
33   // to extract additional names for boundary lines.
34 
35   // Add implicit areas first. Track the names that we add here, because
36   // we will ignore future explicit areas with the same name.
37   nsTHashtable<nsRefPtrHashKey<nsAtom>> namesSeen;
38   nsGridContainerFrame::ImplicitNamedAreas* implicitAreas =
39       aFrame->GetImplicitNamedAreas();
40   if (implicitAreas) {
41     for (auto iter = implicitAreas->iter(); !iter.done(); iter.next()) {
42       auto& areaInfo = iter.get().value();
43       namesSeen.PutEntry(areaInfo.name.AsAtom());
44       GridArea* area =
45           new GridArea(this, areaInfo.name.AsAtom(), GridDeclaration::Implicit,
46                        areaInfo.rows.start, areaInfo.rows.end,
47                        areaInfo.columns.start, areaInfo.columns.end);
48       mAreas.AppendElement(area);
49     }
50   }
51 
52   // Add explicit areas next, as long as they don't have the same name
53   // as the implicit areas, because the implicit values override what was
54   // initially available in the explicit areas.
55   if (auto* explicitAreas = aFrame->GetExplicitNamedAreas()) {
56     for (auto& areaInfo : explicitAreas->AsSpan()) {
57       if (!namesSeen.Contains(areaInfo.name.AsAtom())) {
58         GridArea* area = new GridArea(
59             this, areaInfo.name.AsAtom(), GridDeclaration::Explicit,
60             areaInfo.rows.start, areaInfo.rows.end, areaInfo.columns.start,
61             areaInfo.columns.end);
62         mAreas.AppendElement(area);
63       }
64     }
65   }
66 
67   // Now construct the tracks and lines.
68   const ComputedGridTrackInfo* rowTrackInfo = aFrame->GetComputedTemplateRows();
69   const ComputedGridLineInfo* rowLineInfo =
70       aFrame->GetComputedTemplateRowLines();
71   mRows->SetTrackInfo(rowTrackInfo);
72   mRows->SetLineInfo(rowTrackInfo, rowLineInfo, mAreas, true);
73 
74   const ComputedGridTrackInfo* columnTrackInfo =
75       aFrame->GetComputedTemplateColumns();
76   const ComputedGridLineInfo* columnLineInfo =
77       aFrame->GetComputedTemplateColumnLines();
78   mCols->SetTrackInfo(columnTrackInfo);
79   mCols->SetLineInfo(columnTrackInfo, columnLineInfo, mAreas, false);
80 }
81 
82 Grid::~Grid() = default;
83 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)84 JSObject* Grid::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
85   return Grid_Binding::Wrap(aCx, this, aGivenProto);
86 }
87 
Rows() const88 GridDimension* Grid::Rows() const { return mRows; }
89 
Cols() const90 GridDimension* Grid::Cols() const { return mCols; }
91 
GetAreas(nsTArray<RefPtr<GridArea>> & aAreas) const92 void Grid::GetAreas(nsTArray<RefPtr<GridArea>>& aAreas) const {
93   aAreas = mAreas.Clone();
94 }
95 
96 }  // namespace dom
97 }  // namespace mozilla
98