1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 03:08:13 +00:00

LibWeb: Parse line names in grid track position properties

Parse line names when passed to the grid-column/row-start/end CSS
properties.
This commit is contained in:
martinfalisse 2022-10-30 13:46:50 +01:00 committed by Andreas Kling
parent 78a573d678
commit 829f56572d
3 changed files with 42 additions and 1 deletions

View file

@ -15,6 +15,19 @@ GridTrackPlacement::GridTrackPlacement(int span_count_or_position, bool has_span
{
}
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)
{
@ -35,6 +48,9 @@ String GridTrackPlacement::to_string() const
builder.append(String::number(m_span_count_or_position));
builder.append(" "sv);
}
if (has_line_name()) {
builder.append(m_line_name);
}
return builder.to_string();
}

View file

@ -18,7 +18,9 @@ public:
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(); };
@ -26,10 +28,13 @@ public:
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; }
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
@ -40,6 +45,7 @@ public:
private:
Type m_type;
int m_span_count_or_position { 0 };
String m_line_name;
};
}

View file

@ -5655,6 +5655,13 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
return true;
return false;
};
auto is_line_name = [](Token token) -> bool {
// The <custom-ident> additionally excludes the keywords span and auto.
if (token.is(Token::Type::Ident) && !token.ident().equals_ignoring_case("span"sv) && !token.ident().equals_ignoring_case("auto"sv))
return true;
return false;
};
auto tokens = TokenStream { component_values };
tokens.skip_whitespace();
auto current_token = tokens.next_token().token();
@ -5666,11 +5673,14 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
return GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement(1, true));
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(CSS::GridTrackPlacement(current_token.ident()));
return {};
}
auto span_value = false;
auto span_or_position_value = 0;
String line_name_value;
while (true) {
if (is_auto(current_token))
return {};
@ -5686,6 +5696,12 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
else
return {};
}
if (is_line_name(current_token)) {
if (line_name_value.is_empty())
line_name_value = current_token.ident();
else
return {};
}
tokens.skip_whitespace();
if (tokens.has_next_token())
current_token = tokens.next_token().token();
@ -5700,6 +5716,9 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
// If the <integer> is omitted, it defaults to 1.
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));
}