mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 09:27:34 +00:00
LibWeb: Parse grid track placements using TokenStream
I'm not entirely happy with this, but I barely understand grid, and this does still work.
This commit is contained in:
parent
b172c29d9a
commit
14e2e2bac4
2 changed files with 74 additions and 50 deletions
|
@ -5376,8 +5376,12 @@ RefPtr<StyleValue> Parser::parse_grid_auto_track_sizes(Vector<ComponentValue> co
|
||||||
return GridTrackSizeListStyleValue::create(CSS::GridTrackSizeList(track_list, {}));
|
return GridTrackSizeListStyleValue::create(CSS::GridTrackSizeList(track_list, {}));
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> const& component_values)
|
RefPtr<StyleValue> Parser::parse_grid_track_placement(TokenStream<ComponentValue>& tokens)
|
||||||
{
|
{
|
||||||
|
// FIXME: This shouldn't be needed. Right now, the below code returns a StyleValue even if no tokens are consumed!
|
||||||
|
if (!tokens.has_next_token())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
// https://www.w3.org/TR/css-grid-2/#line-placement
|
// https://www.w3.org/TR/css-grid-2/#line-placement
|
||||||
// Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
|
// Line-based Placement: the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties
|
||||||
// <grid-line> =
|
// <grid-line> =
|
||||||
|
@ -5391,59 +5395,65 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
auto is_identifier = [](auto& token) -> bool {
|
auto is_custom_ident = [](auto& token) -> bool {
|
||||||
// The <custom-ident> additionally excludes the keywords span and auto.
|
// The <custom-ident> additionally excludes the keywords span and auto.
|
||||||
if (token.is(Token::Type::Ident) && !token.token().ident().equals_ignoring_ascii_case("span"sv) && !token.token().ident().equals_ignoring_ascii_case("auto"sv))
|
if (token.is(Token::Type::Ident) && !token.is_ident("span"sv) && !token.is_ident("auto"sv))
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
auto tokens = TokenStream { component_values };
|
auto transaction = tokens.begin_transaction();
|
||||||
tokens.skip_whitespace();
|
|
||||||
auto current_token = tokens.next_token();
|
|
||||||
|
|
||||||
if (!tokens.has_next_token()) {
|
// FIXME: Handle the single-token case inside the loop instead, so that we can more easily call this from
|
||||||
if (current_token.is_ident("auto"sv))
|
// `parse_grid_area_shorthand_value()` using a single TokenStream.
|
||||||
|
if (tokens.remaining_token_count() == 1) {
|
||||||
|
auto& token = tokens.next_token();
|
||||||
|
if (token.is_ident("auto"sv)) {
|
||||||
|
transaction.commit();
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto());
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto());
|
||||||
if (current_token.is_ident("span"sv))
|
}
|
||||||
|
if (token.is_ident("span"sv)) {
|
||||||
|
transaction.commit();
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(1));
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(1));
|
||||||
if (is_valid_integer(current_token))
|
}
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast<int>(current_token.token().number_value()), {}));
|
if (is_valid_integer(token)) {
|
||||||
if (is_identifier(current_token))
|
transaction.commit();
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line({}, current_token.token().to_string()));
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(static_cast<int>(token.token().number_value()), {}));
|
||||||
|
}
|
||||||
|
if (is_custom_ident(token)) {
|
||||||
|
transaction.commit();
|
||||||
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line({}, token.token().ident().to_string()));
|
||||||
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto span_value = false;
|
auto span_value = false;
|
||||||
auto span_or_position_value = 0;
|
auto span_or_position_value = 0;
|
||||||
String identifier_value;
|
String identifier_value;
|
||||||
while (true) {
|
while (tokens.has_next_token()) {
|
||||||
if (current_token.is_ident("auto"sv))
|
auto& token = tokens.peek_token();
|
||||||
|
if (token.is_ident("auto"sv))
|
||||||
return nullptr;
|
return nullptr;
|
||||||
if (current_token.is_ident("span"sv)) {
|
if (token.is_ident("span"sv)) {
|
||||||
if (span_value == false)
|
if (span_value)
|
||||||
span_value = true;
|
|
||||||
else
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
(void)tokens.next_token(); // span
|
||||||
|
span_value = true;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (is_valid_integer(current_token)) {
|
if (is_valid_integer(token)) {
|
||||||
if (span_or_position_value == 0)
|
if (span_or_position_value != 0)
|
||||||
span_or_position_value = static_cast<int>(current_token.token().number_value());
|
|
||||||
else
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
span_or_position_value = static_cast<int>(tokens.next_token().token().to_integer());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (is_identifier(current_token)) {
|
if (is_custom_ident(token)) {
|
||||||
if (identifier_value.is_empty()) {
|
if (!identifier_value.is_empty())
|
||||||
identifier_value = current_token.token().ident().to_string();
|
|
||||||
} else {
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
identifier_value = tokens.next_token().token().ident().to_string();
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
tokens.skip_whitespace();
|
break;
|
||||||
if (tokens.has_next_token())
|
|
||||||
current_token = tokens.next_token();
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Negative integers or zero are invalid.
|
// Negative integers or zero are invalid.
|
||||||
|
@ -5454,6 +5464,7 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement(Vector<ComponentValue> con
|
||||||
if (span_or_position_value == 0)
|
if (span_or_position_value == 0)
|
||||||
span_or_position_value = 1;
|
span_or_position_value = 1;
|
||||||
|
|
||||||
|
transaction.commit();
|
||||||
if (!identifier_value.is_empty())
|
if (!identifier_value.is_empty())
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(span_or_position_value, identifier_value));
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_line(span_or_position_value, identifier_value));
|
||||||
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(span_or_position_value));
|
return GridTrackPlacementStyleValue::create(GridTrackPlacement::make_span(span_or_position_value));
|
||||||
|
@ -5488,7 +5499,8 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement_shorthand_value(PropertyID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parsed_start_value = parse_grid_track_placement(track_start_placement_tokens);
|
TokenStream track_start_placement_token_stream { track_start_placement_tokens };
|
||||||
|
auto parsed_start_value = parse_grid_track_placement(track_start_placement_token_stream);
|
||||||
if (parsed_start_value && track_end_placement_tokens.is_empty()) {
|
if (parsed_start_value && track_end_placement_tokens.is_empty()) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return ShorthandStyleValue::create(property_id,
|
return ShorthandStyleValue::create(property_id,
|
||||||
|
@ -5496,7 +5508,8 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement_shorthand_value(PropertyID
|
||||||
{ parsed_start_value.release_nonnull(), GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto()) });
|
{ parsed_start_value.release_nonnull(), GridTrackPlacementStyleValue::create(GridTrackPlacement::make_auto()) });
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parsed_end_value = parse_grid_track_placement(track_end_placement_tokens);
|
TokenStream track_end_placement_token_stream { track_end_placement_tokens };
|
||||||
|
auto parsed_end_value = parse_grid_track_placement(track_end_placement_token_stream);
|
||||||
if (parsed_start_value && parsed_end_value) {
|
if (parsed_start_value && parsed_end_value) {
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
return ShorthandStyleValue::create(property_id,
|
return ShorthandStyleValue::create(property_id,
|
||||||
|
@ -5559,15 +5572,11 @@ RefPtr<StyleValue> Parser::parse_grid_area_shorthand_value(TokenStream<Component
|
||||||
auto transaction = tokens.begin_transaction();
|
auto transaction = tokens.begin_transaction();
|
||||||
|
|
||||||
auto parse_placement_tokens = [&](Vector<ComponentValue>& placement_tokens, bool check_for_delimiter = true) -> void {
|
auto parse_placement_tokens = [&](Vector<ComponentValue>& placement_tokens, bool check_for_delimiter = true) -> void {
|
||||||
auto current_token = tokens.next_token();
|
while (tokens.has_next_token()) {
|
||||||
while (true) {
|
auto& current_token = tokens.next_token();
|
||||||
if (check_for_delimiter && current_token.is_delim('/'))
|
if (check_for_delimiter && current_token.is_delim('/'))
|
||||||
break;
|
break;
|
||||||
placement_tokens.append(current_token);
|
placement_tokens.append(current_token);
|
||||||
tokens.skip_whitespace();
|
|
||||||
if (!tokens.has_next_token())
|
|
||||||
break;
|
|
||||||
current_token = tokens.next_token();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -5589,10 +5598,25 @@ RefPtr<StyleValue> Parser::parse_grid_area_shorthand_value(TokenStream<Component
|
||||||
// https://www.w3.org/TR/css-grid-2/#placement-shorthands
|
// https://www.w3.org/TR/css-grid-2/#placement-shorthands
|
||||||
// The grid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end and
|
// The grid-area property is a shorthand for grid-row-start, grid-column-start, grid-row-end and
|
||||||
// grid-column-end.
|
// grid-column-end.
|
||||||
auto row_start_style_value = parse_grid_track_placement(row_start_placement_tokens);
|
TokenStream row_start_placement_token_stream { row_start_placement_tokens };
|
||||||
auto column_start_style_value = parse_grid_track_placement(column_start_placement_tokens);
|
auto row_start_style_value = parse_grid_track_placement(row_start_placement_token_stream);
|
||||||
auto row_end_style_value = parse_grid_track_placement(row_end_placement_tokens);
|
if (row_start_placement_token_stream.has_next_token())
|
||||||
auto column_end_style_value = parse_grid_track_placement(column_end_placement_tokens);
|
return nullptr;
|
||||||
|
|
||||||
|
TokenStream column_start_placement_token_stream { column_start_placement_tokens };
|
||||||
|
auto column_start_style_value = parse_grid_track_placement(column_start_placement_token_stream);
|
||||||
|
if (column_start_placement_token_stream.has_next_token())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
TokenStream row_end_placement_token_stream { row_end_placement_tokens };
|
||||||
|
auto row_end_style_value = parse_grid_track_placement(row_end_placement_token_stream);
|
||||||
|
if (row_end_placement_token_stream.has_next_token())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
TokenStream column_end_placement_token_stream { column_end_placement_tokens };
|
||||||
|
auto column_end_style_value = parse_grid_track_placement(column_end_placement_token_stream);
|
||||||
|
if (column_end_placement_token_stream.has_next_token())
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
// If four <grid-line> values are specified, grid-row-start is set to the first value, grid-column-start
|
// If four <grid-line> values are specified, grid-row-start is set to the first value, grid-column-start
|
||||||
// is set to the second value, grid-row-end is set to the third value, and grid-column-end is set to the
|
// is set to the second value, grid-row-end is set to the third value, and grid-column-end is set to the
|
||||||
|
@ -5833,11 +5857,11 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::GridColumnEnd:
|
case PropertyID::GridColumnEnd:
|
||||||
if (auto parsed_value = parse_grid_track_placement(component_values))
|
if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::GridColumnStart:
|
case PropertyID::GridColumnStart:
|
||||||
if (auto parsed_value = parse_grid_track_placement(component_values))
|
if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::GridRow:
|
case PropertyID::GridRow:
|
||||||
|
@ -5845,11 +5869,11 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::GridRowEnd:
|
case PropertyID::GridRowEnd:
|
||||||
if (auto parsed_value = parse_grid_track_placement(component_values))
|
if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::GridRowStart:
|
case PropertyID::GridRowStart:
|
||||||
if (auto parsed_value = parse_grid_track_placement(component_values))
|
if (auto parsed_value = parse_grid_track_placement(tokens); parsed_value && !tokens.has_next_token())
|
||||||
return parsed_value.release_nonnull();
|
return parsed_value.release_nonnull();
|
||||||
return ParseError::SyntaxError;
|
return ParseError::SyntaxError;
|
||||||
case PropertyID::Grid:
|
case PropertyID::Grid:
|
||||||
|
|
|
@ -262,7 +262,7 @@ private:
|
||||||
RefPtr<StyleValue> parse_grid_auto_track_sizes(Vector<ComponentValue> const&);
|
RefPtr<StyleValue> parse_grid_auto_track_sizes(Vector<ComponentValue> const&);
|
||||||
RefPtr<GridAutoFlowStyleValue> parse_grid_auto_flow_value(TokenStream<ComponentValue>&);
|
RefPtr<GridAutoFlowStyleValue> parse_grid_auto_flow_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_grid_track_size_list_shorthand_value(PropertyID, Vector<ComponentValue> const&);
|
RefPtr<StyleValue> parse_grid_track_size_list_shorthand_value(PropertyID, Vector<ComponentValue> const&);
|
||||||
RefPtr<StyleValue> parse_grid_track_placement(Vector<ComponentValue> const&);
|
RefPtr<StyleValue> parse_grid_track_placement(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_grid_track_placement_shorthand_value(PropertyID, TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_grid_track_placement_shorthand_value(PropertyID, TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_grid_template_areas_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_grid_template_areas_value(TokenStream<ComponentValue>&);
|
||||||
RefPtr<StyleValue> parse_grid_area_shorthand_value(TokenStream<ComponentValue>&);
|
RefPtr<StyleValue> parse_grid_area_shorthand_value(TokenStream<ComponentValue>&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue