mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 02:37:36 +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:
parent
29352f570a
commit
c03e025a32
7 changed files with 292 additions and 205 deletions
|
@ -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
|
||||
|
@ -9,48 +10,24 @@
|
|||
|
||||
namespace Web::CSS {
|
||||
|
||||
GridTrackPlacement::GridTrackPlacement(int span_count_or_position, bool has_span)
|
||||
: m_type(has_span ? Type::Span : Type::Position)
|
||||
, m_span_count_or_position(span_count_or_position)
|
||||
{
|
||||
}
|
||||
|
||||
GridTrackPlacement::GridTrackPlacement(String line_name, int span_count_or_position, bool has_span)
|
||||
: m_type(has_span ? Type::Span : Type::Position)
|
||||
, m_span_count_or_position(span_count_or_position)
|
||||
, m_line_name(line_name)
|
||||
{
|
||||
}
|
||||
|
||||
GridTrackPlacement::GridTrackPlacement(String line_name, bool has_span)
|
||||
: m_type(has_span ? Type::Span : Type::Position)
|
||||
, m_line_name(line_name)
|
||||
{
|
||||
}
|
||||
|
||||
GridTrackPlacement::GridTrackPlacement()
|
||||
: m_type(Type::Auto)
|
||||
{
|
||||
}
|
||||
|
||||
String GridTrackPlacement::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
if (is_auto()) {
|
||||
builder.append("auto"sv);
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
if (is_span()) {
|
||||
builder.append("span"sv);
|
||||
builder.append(" "sv);
|
||||
}
|
||||
if (m_span_count_or_position != 0) {
|
||||
builder.append(MUST(String::number(m_span_count_or_position)));
|
||||
builder.append(" "sv);
|
||||
}
|
||||
if (has_line_name()) {
|
||||
builder.append(m_line_name);
|
||||
}
|
||||
m_value.visit(
|
||||
[&](Auto const&) {
|
||||
builder.append("auto"sv);
|
||||
},
|
||||
[&](Area const& area) {
|
||||
builder.append(area.name);
|
||||
},
|
||||
[&](Line const& line) {
|
||||
builder.appendff("{}", line.value);
|
||||
if (line.name.has_value())
|
||||
builder.appendff(" {}", line.name.value());
|
||||
},
|
||||
[&](Span const& span) {
|
||||
builder.appendff("span {}", span.value);
|
||||
});
|
||||
return MUST(builder.to_string());
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -5350,7 +5350,7 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
|||
return true;
|
||||
return false;
|
||||
};
|
||||
auto is_line_name = [](Token token) -> bool {
|
||||
auto is_identifier = [](Token token) -> bool {
|
||||
// The <custom-ident> additionally excludes the keywords span and auto.
|
||||
if (token.is(Token::Type::Ident) && !token.ident().equals_ignoring_ascii_case("span"sv) && !token.ident().equals_ignoring_ascii_case("auto"sv))
|
||||
return true;
|
||||
|
@ -5363,22 +5363,22 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
|||
|
||||
if (!tokens.has_next_token()) {
|
||||
if (is_auto(current_token))
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement());
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto());
|
||||
if (is_span(current_token))
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(1, true));
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(1));
|
||||
if (is_valid_integer(current_token))
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(static_cast<int>(current_token.number_value())));
|
||||
if (is_line_name(current_token)) {
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast<int>(current_token.number_value()), {}));
|
||||
if (is_identifier(current_token)) {
|
||||
auto maybe_string = String::from_utf8(current_token.ident());
|
||||
if (!maybe_string.is_error())
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(maybe_string.value()));
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_area(maybe_string.value()));
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto span_value = false;
|
||||
auto span_or_position_value = 0;
|
||||
String line_name_value;
|
||||
String identifier_value;
|
||||
while (true) {
|
||||
if (is_auto(current_token))
|
||||
return nullptr;
|
||||
|
@ -5394,12 +5394,12 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
|||
else
|
||||
return nullptr;
|
||||
}
|
||||
if (is_line_name(current_token)) {
|
||||
if (line_name_value.is_empty()) {
|
||||
if (is_identifier(current_token)) {
|
||||
if (identifier_value.is_empty()) {
|
||||
auto maybe_string = String::from_utf8(current_token.ident());
|
||||
if (maybe_string.is_error())
|
||||
return nullptr;
|
||||
line_name_value = maybe_string.release_value();
|
||||
identifier_value = maybe_string.release_value();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -5419,9 +5419,9 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
|||
if (span_or_position_value == 0)
|
||||
span_or_position_value = 1;
|
||||
|
||||
if (!line_name_value.is_empty())
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(line_name_value, span_or_position_value, span_value));
|
||||
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(span_or_position_value, span_value));
|
||||
if (!identifier_value.is_empty())
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(span_or_position_value, identifier_value));
|
||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(span_or_position_value));
|
||||
}
|
||||
|
||||
RefPtr<StyleValue> Parser::parse_grid_track_placement_shorthand_value(Vector<ComponentValue> const& component_values)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue