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