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/. */
10 #include "GridDimension.h"
11 #include "mozilla/dom/Element.h"
12 #include "mozilla/dom/GridBinding.h"
13 #include "nsGridContainerFrame.h"
14 #include "nsTHashSet.h"
16 namespace mozilla::dom
{
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Grid
)
20 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Grid
)
22 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent
, mRows
, mCols
, mAreas
)
23 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
24 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
26 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Grid
)
27 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent
, mRows
, mCols
, mAreas
)
28 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
30 NS_IMPL_CYCLE_COLLECTING_ADDREF(Grid
)
31 NS_IMPL_CYCLE_COLLECTING_RELEASE(Grid
)
33 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Grid
)
34 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
35 NS_INTERFACE_MAP_ENTRY(nsISupports
)
38 Grid::Grid(nsISupports
* aParent
, nsGridContainerFrame
* aFrame
)
39 : mParent(do_QueryInterface(aParent
)),
41 mRows(new GridDimension(this)),
42 mCols(new GridDimension(this)) {
44 "Should never be instantiated with a null nsGridContainerFrame");
46 aFrame
->SetProperty(nsGridContainerFrame::GridFragmentInfo(), this);
48 // Construct areas first, because lines may need to reference them
49 // to extract additional names for boundary lines.
51 // Add implicit areas first. Track the names that we add here, because
52 // we will ignore future explicit areas with the same name.
53 nsTHashSet
<RefPtr
<nsAtom
>> namesSeen
;
54 nsGridContainerFrame::ImplicitNamedAreas
* implicitAreas
=
55 aFrame
->GetImplicitNamedAreas();
57 for (auto iter
= implicitAreas
->iter(); !iter
.done(); iter
.next()) {
58 auto& areaInfo
= iter
.get().value();
59 namesSeen
.Insert(areaInfo
.name
.AsAtom());
61 new GridArea(this, areaInfo
.name
.AsAtom(), GridDeclaration::Implicit
,
62 areaInfo
.rows
.start
, areaInfo
.rows
.end
,
63 areaInfo
.columns
.start
, areaInfo
.columns
.end
);
64 mAreas
.AppendElement(area
);
68 // Add explicit areas next, as long as they don't have the same name
69 // as the implicit areas, because the implicit values override what was
70 // initially available in the explicit areas.
71 if (auto* explicitAreas
= aFrame
->GetExplicitNamedAreas()) {
72 for (auto& areaInfo
: explicitAreas
->AsSpan()) {
73 if (!namesSeen
.Contains(areaInfo
.name
.AsAtom())) {
74 GridArea
* area
= new GridArea(
75 this, areaInfo
.name
.AsAtom(), GridDeclaration::Explicit
,
76 areaInfo
.rows
.start
, areaInfo
.rows
.end
, areaInfo
.columns
.start
,
77 areaInfo
.columns
.end
);
78 mAreas
.AppendElement(area
);
83 // Now construct the tracks and lines.
84 const ComputedGridTrackInfo
* rowTrackInfo
= aFrame
->GetComputedTemplateRows();
85 const ComputedGridLineInfo
* rowLineInfo
=
86 aFrame
->GetComputedTemplateRowLines();
87 mRows
->SetTrackInfo(rowTrackInfo
);
88 mRows
->SetLineInfo(rowTrackInfo
, rowLineInfo
, mAreas
, true);
90 const ComputedGridTrackInfo
* columnTrackInfo
=
91 aFrame
->GetComputedTemplateColumns();
92 const ComputedGridLineInfo
* columnLineInfo
=
93 aFrame
->GetComputedTemplateColumnLines();
94 mCols
->SetTrackInfo(columnTrackInfo
);
95 mCols
->SetLineInfo(columnTrackInfo
, columnLineInfo
, mAreas
, false);
98 Grid::~Grid() = default;
100 JSObject
* Grid::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
101 return Grid_Binding::Wrap(aCx
, this, aGivenProto
);
104 void Grid::ForgetFrame() {
105 if (mFrame
.IsAlive()) {
106 mFrame
->RemoveProperty(nsGridContainerFrame::GridFragmentInfo());
107 mFrame
.Clear(mFrame
->PresContext()->GetPresShell());
111 GridDimension
* Grid::Rows() const { return mRows
; }
113 GridDimension
* Grid::Cols() const { return mCols
; }
115 void Grid::GetAreas(nsTArray
<RefPtr
<GridArea
>>& aAreas
) const {
116 aAreas
= mAreas
.Clone();
119 } // namespace mozilla::dom