LibWeb: Parse auto-fill and auto-fit attributes

Parse auto-fill and auto-fit attributes for the CSS Grid.
This commit is contained in:
martinfalisse 2022-10-15 13:02:45 +02:00 committed by Andreas Kling
parent 3cba4b6e41
commit a5f042b424
Notes: sideshowbarker 2024-07-17 18:13:59 +09:00
3 changed files with 31 additions and 2 deletions

View file

@ -84,6 +84,13 @@ ExplicitTrackSizing::ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize> meta_gri
{
}
ExplicitTrackSizing::ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize> meta_grid_track_sizes, Type type)
: m_meta_grid_track_sizes(meta_grid_track_sizes)
, m_is_auto_fill(type == Type::AutoFill)
, m_is_auto_fit(type == Type::AutoFit)
{
}
String ExplicitTrackSizing::to_string() const
{
StringBuilder builder;

View file

@ -87,12 +87,21 @@ private:
class ExplicitTrackSizing {
public:
enum class Type {
AutoFit,
AutoFill,
};
ExplicitTrackSizing();
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>);
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, int repeat_count);
ExplicitTrackSizing(Vector<CSS::MetaGridTrackSize>, Type);
static ExplicitTrackSizing make_auto() { return ExplicitTrackSizing(); };
bool is_repeat() const { return m_is_repeat; }
bool is_auto_fill() const { return m_is_auto_fill; }
bool is_auto_fit() const { return m_is_auto_fit; }
int repeat_count() const { return m_repeat_count; }
Vector<CSS::MetaGridTrackSize> meta_grid_track_sizes() const& { return m_meta_grid_track_sizes; }
@ -106,6 +115,8 @@ public:
private:
Vector<CSS::MetaGridTrackSize> m_meta_grid_track_sizes;
bool m_is_repeat { false };
bool m_is_auto_fill { false };
bool m_is_auto_fit { false };
int m_repeat_count { 0 };
};

View file

@ -5440,6 +5440,8 @@ RefPtr<StyleValue> Parser::parse_grid_track_sizes(Vector<ComponentValue> const&
// The generic form of the repeat() syntax is, approximately,
// repeat( [ <integer [1,∞]> | auto-fill | auto-fit ] , <track-list> )
if (function_token.name().equals_ignoring_case("repeat"sv)) {
auto is_auto_fill = false;
auto is_auto_fit = false;
auto function_tokens = TokenStream(function_token.values());
auto comma_separated_list = parse_a_comma_separated_list_of_component_values(function_tokens);
if (comma_separated_list.size() != 2)
@ -5454,6 +5456,10 @@ RefPtr<StyleValue> Parser::parse_grid_track_sizes(Vector<ComponentValue> const&
auto repeat_count = 0;
if (current_token.is(Token::Type::Number) && current_token.number().is_integer() && current_token.number_value() > 0)
repeat_count = current_token.number_value();
else if (current_token.is(Token::Type::Ident) && current_token.ident().equals_ignoring_case("auto-fill"sv))
is_auto_fill = true;
else if (current_token.is(Token::Type::Ident) && current_token.ident().equals_ignoring_case("auto-fit"sv))
is_auto_fit = true;
// The second argument is a track list, which is repeated that number of times.
TokenStream part_two_tokens { comma_separated_list[1] };
@ -5478,6 +5484,9 @@ RefPtr<StyleValue> Parser::parse_grid_track_sizes(Vector<ComponentValue> const&
auto grid_track_size = parse_grid_track_size(current_component_value);
if (!grid_track_size.has_value())
return GridTrackSizeStyleValue::create({});
// Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes.
if (grid_track_size.value().is_flexible_length() && (is_auto_fill || is_auto_fit))
return GridTrackSizeStyleValue::create({});
repeat_params.append(grid_track_size.value());
}
part_two_tokens.skip_whitespace();
@ -5485,8 +5494,6 @@ RefPtr<StyleValue> Parser::parse_grid_track_sizes(Vector<ComponentValue> const&
break;
}
// Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes.
// Thus the precise syntax of the repeat() notation has several forms:
// <track-repeat> = repeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )
// <auto-repeat> = repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
@ -5506,6 +5513,10 @@ RefPtr<StyleValue> Parser::parse_grid_track_sizes(Vector<ComponentValue> const&
// If a repeat() function that is not a <name-repeat> ends up placing two <line-names> adjacent to
// each other, the name lists are merged. For example, repeat(2, [a] 1fr [b]) is equivalent to [a]
// 1fr [b a] 1fr [b].
if (is_auto_fill)
return GridTrackSizeStyleValue::create(CSS::ExplicitTrackSizing(repeat_params, CSS::ExplicitTrackSizing::Type::AutoFill));
if (is_auto_fit)
return GridTrackSizeStyleValue::create(CSS::ExplicitTrackSizing(repeat_params, CSS::ExplicitTrackSizing::Type::AutoFit));
return GridTrackSizeStyleValue::create(CSS::ExplicitTrackSizing(repeat_params, repeat_count));
} else {
// FIXME: Implement MinMax, etc.