1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:17:44 +00:00

LibWeb: Disambiguate GridTrackPlacement API

- Ambiguous `raw_value()` method is replaced with `line_number()` and
  `span()`.
- `line_name()` that before returned either line name or area name is
  replaced with `line_name()` and `area_name()`.
- `Position` type is replaced with `Line` and `Area` type so we don't
   have to guess while doing layout.

Affected test expectations:
- `template-lines-and-areas` - improvement over what we had before.
- `named-tracks` - rebaseline a giant test. will have to split it into
  smaller tests in the future.
This commit is contained in:
Aliaksandr Kalenik 2023-08-25 23:29:13 +02:00 committed by Andreas Kling
parent 29352f570a
commit c03e025a32
7 changed files with 292 additions and 205 deletions

View file

@ -1,4 +1,5 @@
/*
* Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
* Copyright (c) 2022, Martin Falisse <mfalisse@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -12,40 +13,79 @@ namespace Web::CSS {
class GridTrackPlacement {
public:
enum class Type {
Span,
Position,
Auto
};
GridTrackPlacement(String line_name, int span_count_or_position, bool has_span = false);
GridTrackPlacement(int span_count_or_position, bool has_span = false);
GridTrackPlacement(String line_name, bool has_span = false);
GridTrackPlacement();
static GridTrackPlacement make_auto() { return GridTrackPlacement(); }
bool is_span() const { return m_type == Type::Span; }
bool is_position() const { return m_type == Type::Position; }
bool is_auto() const { return m_type == Type::Auto; }
bool is_auto_positioned() const { return m_type == Type::Auto || (m_type == Type::Span && !has_line_name()); }
bool has_line_name() const { return !m_line_name.is_empty(); }
int raw_value() const { return m_span_count_or_position; }
Type type() const { return m_type; }
String line_name() const { return m_line_name; }
String to_string() const;
bool operator==(GridTrackPlacement const& other) const
static GridTrackPlacement make_auto()
{
return m_type == other.type() && m_span_count_or_position == other.raw_value();
return GridTrackPlacement();
}
static GridTrackPlacement make_area(String name)
{
return GridTrackPlacement(Area { .name = name });
}
static GridTrackPlacement make_line(int value, Optional<String> name)
{
return GridTrackPlacement(Line { .value = value, .name = name });
}
static GridTrackPlacement make_span(int value)
{
return GridTrackPlacement(Span { .value = value });
}
bool is_auto() const { return m_value.has<Auto>(); }
bool is_area() const { return m_value.has<Area>(); }
bool is_line() const { return m_value.has<Line>(); }
bool is_span() const { return m_value.has<Span>(); }
bool is_auto_positioned() const { return is_auto() || is_span(); }
bool is_positioned() const { return !is_auto_positioned(); }
bool has_line_name() const
{
return is_line() && m_value.get<Line>().name.has_value();
}
String area_name() const { return m_value.get<Area>().name; }
String line_name() const { return m_value.get<Line>().name.value(); }
int line_number() const { return m_value.get<Line>().value; }
int span() const { return m_value.get<Span>().value; }
String to_string() const;
bool operator==(GridTrackPlacement const& other) const = default;
private:
Type m_type;
int m_span_count_or_position { 0 };
String m_line_name;
struct Auto {
bool operator==(Auto const&) const = default;
};
struct Area {
String name;
bool operator==(Area const& other) const = default;
};
struct Line {
int value;
Optional<String> name;
bool operator==(Line const& other) const = default;
};
struct Span {
int value;
bool operator==(Span const& other) const = default;
};
GridTrackPlacement()
: m_value(Auto {}) {};
GridTrackPlacement(Area value)
: m_value(value) {};
GridTrackPlacement(Line value)
: m_value(value) {};
GridTrackPlacement(Span value)
: m_value(value) {};
Variant<Auto, Area, Line, Span> m_value;
};
}