Skip to content

Commit 7de17dc

Browse files
AtkinsSJtcl3
authored andcommitted
LibWeb/CSS: Absolutize grid-related StyleValues
1 parent 597fe82 commit 7de17dc

12 files changed

+161
-26
lines changed

Libraries/LibWeb/CSS/GridTrackPlacement.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/*
22
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
33
* Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
4+
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
78

89
#include "GridTrackPlacement.h"
910
#include <AK/StringBuilder.h>
11+
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
1012

1113
namespace Web::CSS {
1214

@@ -38,4 +40,34 @@ String GridTrackPlacement::to_string(SerializationMode mode) const
3840
return MUST(builder.to_string());
3941
}
4042

43+
GridTrackPlacement GridTrackPlacement::absolutized(ComputationContext const& context) const
44+
{
45+
auto absolutize_integer_or_calculated = [&context](IntegerOrCalculated const& integer_or_calculated) {
46+
if (!integer_or_calculated.is_calculated())
47+
return integer_or_calculated;
48+
auto absolutized = integer_or_calculated.calculated()->absolutized(context);
49+
if (absolutized->is_calculated())
50+
return IntegerOrCalculated { absolutized->as_calculated() };
51+
VERIFY(absolutized->is_integer());
52+
return IntegerOrCalculated { absolutized->as_integer().integer() };
53+
};
54+
55+
return m_value.visit(
56+
[this](Auto const&) {
57+
return *this;
58+
},
59+
[&](AreaOrLine const& area_or_line) -> GridTrackPlacement {
60+
return AreaOrLine {
61+
.line_number = area_or_line.line_number.map(absolutize_integer_or_calculated),
62+
.name = area_or_line.name,
63+
};
64+
},
65+
[&](Span const& span) -> GridTrackPlacement {
66+
return Span {
67+
.value = absolutize_integer_or_calculated(span.value),
68+
.name = span.name,
69+
};
70+
});
71+
}
72+
4173
}

Libraries/LibWeb/CSS/GridTrackPlacement.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
33
* Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
4+
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -55,6 +56,8 @@ class GridTrackPlacement {
5556

5657
String to_string(SerializationMode mode) const;
5758

59+
GridTrackPlacement absolutized(ComputationContext const&) const;
60+
5861
bool operator==(GridTrackPlacement const& other) const = default;
5962

6063
private:

Libraries/LibWeb/CSS/GridTrackSize.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
33
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
4+
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -110,6 +111,41 @@ String GridSize::to_string(SerializationMode mode) const
110111
return m_value.visit([mode](auto const& it) { return it.to_string(mode); });
111112
}
112113

114+
GridSize GridSize::absolutized(ComputationContext const& context) const
115+
{
116+
auto absolutize_length_percentage = [&context](LengthPercentage const& length_percentage) -> Optional<LengthPercentage> {
117+
if (length_percentage.is_length()) {
118+
auto length = length_percentage.length().absolutize(context.length_resolution_context.viewport_rect, context.length_resolution_context.font_metrics, context.length_resolution_context.root_font_metrics);
119+
if (length.has_value())
120+
return length.release_value();
121+
return {};
122+
}
123+
124+
if (length_percentage.is_calculated())
125+
return LengthPercentage::from_style_value(length_percentage.calculated()->absolutized(context));
126+
127+
return {};
128+
};
129+
return m_value.visit(
130+
[&](Size const& size) -> GridSize {
131+
if (size.is_length_percentage()) {
132+
if (auto result = absolutize_length_percentage(size.length_percentage()); result.has_value())
133+
return Size::make_length_percentage(result.release_value());
134+
}
135+
136+
if (size.is_fit_content() && size.fit_content_available_space().has_value()) {
137+
if (auto result = absolutize_length_percentage(size.fit_content_available_space().value()); result.has_value()) {
138+
return Size::make_fit_content(result.release_value());
139+
}
140+
}
141+
142+
return GridSize { size };
143+
},
144+
[](Flex const& flex) {
145+
return GridSize { flex };
146+
});
147+
}
148+
113149
GridMinMax::GridMinMax(GridSize min_grid_size, GridSize max_grid_size)
114150
: m_min_grid_size(move(min_grid_size))
115151
, m_max_grid_size(move(max_grid_size))
@@ -127,6 +163,14 @@ String GridMinMax::to_string(SerializationMode mode) const
127163
return MUST(builder.to_string());
128164
}
129165

166+
GridMinMax GridMinMax::absolutized(ComputationContext const& context) const
167+
{
168+
return GridMinMax {
169+
m_min_grid_size.absolutized(context),
170+
m_max_grid_size.absolutized(context),
171+
};
172+
}
173+
130174
GridRepeat::GridRepeat(GridRepeatType grid_repeat_type, GridTrackSizeList&& grid_track_size_list, size_t repeat_count)
131175
: m_type(grid_repeat_type)
132176
, m_grid_track_size_list(move(grid_track_size_list))
@@ -162,6 +206,15 @@ String GridRepeat::to_string(SerializationMode mode) const
162206
return MUST(builder.to_string());
163207
}
164208

209+
GridRepeat GridRepeat::absolutized(ComputationContext const& context) const
210+
{
211+
return GridRepeat {
212+
m_type,
213+
m_grid_track_size_list.absolutized(context),
214+
m_repeat_count,
215+
};
216+
}
217+
165218
ExplicitGridTrack::ExplicitGridTrack(Variant<GridRepeat, GridMinMax, GridSize>&& value)
166219
: m_value(move(value))
167220
{
@@ -174,6 +227,13 @@ String ExplicitGridTrack::to_string(SerializationMode mode) const
174227
});
175228
}
176229

230+
ExplicitGridTrack ExplicitGridTrack::absolutized(ComputationContext const& context) const
231+
{
232+
return m_value.visit([&](auto const& it) {
233+
return ExplicitGridTrack { it.absolutized(context) };
234+
});
235+
}
236+
177237
String GridLineNames::to_string() const
178238
{
179239
StringBuilder builder;
@@ -239,4 +299,19 @@ void GridTrackSizeList::append(ExplicitGridTrack&& explicit_track)
239299
m_list.append(move(explicit_track));
240300
}
241301

302+
GridTrackSizeList GridTrackSizeList::absolutized(ComputationContext const& context) const
303+
{
304+
GridTrackSizeList result;
305+
for (auto const& item : m_list) {
306+
item.visit(
307+
[&result, &context](ExplicitGridTrack const& track) {
308+
result.append(track.absolutized(context));
309+
},
310+
[&result](GridLineNames names) {
311+
result.append(move(names));
312+
});
313+
}
314+
return result;
315+
}
316+
242317
}

Libraries/LibWeb/CSS/GridTrackSize.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
33
* Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
4+
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
45
*
56
* SPDX-License-Identifier: BSD-2-Clause
67
*/
@@ -40,6 +41,7 @@ class GridSize {
4041
bool is_definite() const;
4142

4243
String to_string(SerializationMode) const;
44+
GridSize absolutized(ComputationContext const&) const;
4345
bool operator==(GridSize const& other) const = default;
4446

4547
private:
@@ -54,6 +56,7 @@ class GridMinMax {
5456
GridSize const& max_grid_size() const& { return m_max_grid_size; }
5557

5658
String to_string(SerializationMode) const;
59+
GridMinMax absolutized(ComputationContext const&) const;
5760
bool operator==(GridMinMax const& other) const = default;
5861

5962
private:
@@ -97,6 +100,8 @@ class GridTrackSizeList {
97100
void append(GridLineNames&&);
98101
void append(ExplicitGridTrack&&);
99102

103+
GridTrackSizeList absolutized(ComputationContext const&) const;
104+
100105
private:
101106
Vector<Variant<ExplicitGridTrack, GridLineNames>> m_list;
102107
};
@@ -129,6 +134,7 @@ class GridRepeat {
129134
GridRepeatType type() const& { return m_type; }
130135

131136
String to_string(SerializationMode) const;
137+
GridRepeat absolutized(ComputationContext const&) const;
132138
bool operator==(GridRepeat const& other) const = default;
133139

134140
private:
@@ -151,6 +157,7 @@ class ExplicitGridTrack {
151157
GridSize const& grid_size() const { return m_value.get<GridSize>(); }
152158

153159
String to_string(SerializationMode) const;
160+
ExplicitGridTrack absolutized(ComputationContext const&) const;
154161
bool operator==(ExplicitGridTrack const& other) const = default;
155162

156163
private:

Libraries/LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
33
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
4-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
4+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
55
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
66
*
77
* SPDX-License-Identifier: BSD-2-Clause
@@ -21,4 +21,12 @@ String GridTrackPlacementStyleValue::to_string(SerializationMode mode) const
2121
return m_grid_track_placement.to_string(mode);
2222
}
2323

24+
ValueComparingNonnullRefPtr<StyleValue const> GridTrackPlacementStyleValue::absolutized(ComputationContext const& context) const
25+
{
26+
auto absolutized_placement = m_grid_track_placement.absolutized(context);
27+
if (absolutized_placement == m_grid_track_placement)
28+
return *this;
29+
return create(move(absolutized_placement));
30+
}
31+
2432
}

Libraries/LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
33
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
4-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
4+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
55
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
66
*
77
* SPDX-License-Identifier: BSD-2-Clause
@@ -22,6 +22,8 @@ class GridTrackPlacementStyleValue final : public StyleValueWithDefaultOperators
2222
GridTrackPlacement const& grid_track_placement() const { return m_grid_track_placement; }
2323
virtual String to_string(SerializationMode) const override;
2424

25+
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
26+
2527
bool properties_equal(GridTrackPlacementStyleValue const& other) const { return m_grid_track_placement == other.m_grid_track_placement; }
2628

2729
private:

Libraries/LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
33
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
4-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
4+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
55
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
66
*
77
* SPDX-License-Identifier: BSD-2-Clause
@@ -31,4 +31,12 @@ ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> GridTrackSizeList
3131
return adopt_ref(*new (nothrow) GridTrackSizeListStyleValue(CSS::GridTrackSizeList()));
3232
}
3333

34+
ValueComparingNonnullRefPtr<StyleValue const> GridTrackSizeListStyleValue::absolutized(ComputationContext const& context) const
35+
{
36+
auto absolutized = m_grid_track_size_list.absolutized(context);
37+
if (absolutized == m_grid_track_size_list)
38+
return *this;
39+
return create(move(absolutized));
40+
}
41+
3442
}

Libraries/LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
33
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
4-
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
4+
* Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
55
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
66
*
77
* SPDX-License-Identifier: BSD-2-Clause
@@ -26,6 +26,8 @@ class GridTrackSizeListStyleValue final : public StyleValueWithDefaultOperators<
2626

2727
virtual String to_string(SerializationMode) const override;
2828

29+
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
30+
2931
bool properties_equal(GridTrackSizeListStyleValue const& other) const { return m_grid_track_size_list == other.m_grid_track_size_list; }
3032

3133
private:

Tests/LibWeb/Text/expected/wpt-import/css/css-grid/parsing/grid-auto-columns-computed.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ Harness status: OK
22

33
Found 25 tests
44

5-
18 Pass
6-
7 Fail
5+
22 Pass
6+
3 Fail
77
Pass Property grid-auto-columns value '1px'
8-
Fail Property grid-auto-columns value 'calc(10px + 0.5em)'
8+
Pass Property grid-auto-columns value 'calc(10px + 0.5em)'
99
Fail Property grid-auto-columns value 'calc(10px - 0.5em)'
1010
Pass Property grid-auto-columns value '4%'
1111
Pass Property grid-auto-columns value '5fr'
1212
Pass Property grid-auto-columns value 'min-content'
1313
Pass Property grid-auto-columns value 'max-content'
1414
Pass Property grid-auto-columns value 'auto'
1515
Pass Property grid-auto-columns value 'minmax(1px, 5fr)'
16-
Fail Property grid-auto-columns value 'minmax(calc(10px + 0.5em), max-content)'
16+
Pass Property grid-auto-columns value 'minmax(calc(10px + 0.5em), max-content)'
1717
Fail Property grid-auto-columns value 'minmax(calc(10px - 0.5em), max-content)'
1818
Pass Property grid-auto-columns value 'minmax(4%, auto)'
19-
Fail Property grid-auto-columns value 'minmax(min-content, calc(10px + 0.5em))'
19+
Pass Property grid-auto-columns value 'minmax(min-content, calc(10px + 0.5em))'
2020
Pass Property grid-auto-columns value 'minmax(auto, 4%)'
2121
Pass Property grid-auto-columns value 'fit-content(1px)'
22-
Fail Property grid-auto-columns value 'fit-content(calc(10px + 0.5em))'
22+
Pass Property grid-auto-columns value 'fit-content(calc(10px + 0.5em))'
2323
Fail Property grid-auto-columns value 'fit-content(calc(10px - 0.5em))'
2424
Pass Property grid-auto-columns value 'fit-content(4%)'
2525
Pass Property grid-auto-columns value '0px'

Tests/LibWeb/Text/expected/wpt-import/css/css-grid/parsing/grid-auto-rows-computed.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ Harness status: OK
22

33
Found 25 tests
44

5-
18 Pass
6-
7 Fail
5+
22 Pass
6+
3 Fail
77
Pass Property grid-auto-rows value '1px'
8-
Fail Property grid-auto-rows value 'calc(10px + 0.5em)'
8+
Pass Property grid-auto-rows value 'calc(10px + 0.5em)'
99
Fail Property grid-auto-rows value 'calc(10px - 0.5em)'
1010
Pass Property grid-auto-rows value '4%'
1111
Pass Property grid-auto-rows value '5fr'
1212
Pass Property grid-auto-rows value 'min-content'
1313
Pass Property grid-auto-rows value 'max-content'
1414
Pass Property grid-auto-rows value 'auto'
1515
Pass Property grid-auto-rows value 'minmax(1px, 5fr)'
16-
Fail Property grid-auto-rows value 'minmax(calc(10px + 0.5em), max-content)'
16+
Pass Property grid-auto-rows value 'minmax(calc(10px + 0.5em), max-content)'
1717
Fail Property grid-auto-rows value 'minmax(calc(10px - 0.5em), max-content)'
1818
Pass Property grid-auto-rows value 'minmax(4%, auto)'
19-
Fail Property grid-auto-rows value 'minmax(min-content, calc(10px + 0.5em))'
19+
Pass Property grid-auto-rows value 'minmax(min-content, calc(10px + 0.5em))'
2020
Pass Property grid-auto-rows value 'minmax(auto, 4%)'
2121
Pass Property grid-auto-rows value 'fit-content(1px)'
22-
Fail Property grid-auto-rows value 'fit-content(calc(10px + 0.5em))'
22+
Pass Property grid-auto-rows value 'fit-content(calc(10px + 0.5em))'
2323
Fail Property grid-auto-rows value 'fit-content(calc(10px - 0.5em))'
2424
Pass Property grid-auto-rows value 'fit-content(4%)'
2525
Pass Property grid-auto-rows value '0px'

0 commit comments

Comments
 (0)