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<nsStringHashKey> 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.Data();
43 namesSeen.PutEntry(areaInfo.mName);
44 GridArea* area = new GridArea(
45 this, areaInfo.mName, GridDeclaration::Implicit, areaInfo.mRowStart,
46 areaInfo.mRowEnd, areaInfo.mColumnStart, areaInfo.mColumnEnd);
47 mAreas.AppendElement(area);
48 }
49 }
50
51 // Add explicit areas next, as long as they don't have the same name
52 // as the implicit areas, because the implicit values override what was
53 // initially available in the explicit areas.
54 nsGridContainerFrame::ExplicitNamedAreas* explicitAreas =
55 aFrame->GetExplicitNamedAreas();
56 if (explicitAreas) {
57 for (auto& areaInfo : *explicitAreas) {
58 if (!namesSeen.Contains(areaInfo.mName)) {
59 GridArea* area = new GridArea(
60 this, areaInfo.mName, GridDeclaration::Explicit, areaInfo.mRowStart,
61 areaInfo.mRowEnd, areaInfo.mColumnStart, areaInfo.mColumnEnd);
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
~Grid()82 Grid::~Grid() {}
83
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)84 JSObject* Grid::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
85 return GridBinding::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;
94 }
95
96 } // namespace dom
97 } // namespace mozilla
98