LibWeb: Parse the polygon() basic shape function

This commit is contained in:
MacDue 2024-05-26 00:20:09 +01:00 committed by Andreas Kling
parent 5f17d9b34a
commit 120915048c
Notes: sideshowbarker 2024-07-17 03:27:40 +09:00
2 changed files with 53 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BasicShapeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
@ -1204,6 +1205,55 @@ RefPtr<StyleValue> Parser::parse_url_value(TokenStream<ComponentValue>& tokens)
return URLStyleValue::create(*url);
}
RefPtr<StyleValue> Parser::parse_basic_shape_function(ComponentValue const& component_value)
{
if (!component_value.is_function())
return nullptr;
auto function_name = component_value.function().name().bytes_as_string_view();
// FIXME: Implement other shapes. See: https://www.w3.org/TR/css-shapes-1/#basic-shape-functions
if (!function_name.equals_ignoring_ascii_case("polygon"sv))
return nullptr;
// polygon() = polygon( <'fill-rule'>? , [<length-percentage> <length-percentage>]# )
// FIXME: Parse the fill-rule.
auto arguments_tokens = TokenStream { component_value.function().values() };
auto arguments = parse_a_comma_separated_list_of_component_values(arguments_tokens);
Vector<Polygon::Point> points;
for (auto& argument : arguments) {
TokenStream argument_tokens { argument };
argument_tokens.skip_whitespace();
auto x_pos = parse_length_percentage(argument_tokens);
if (!x_pos.has_value())
return nullptr;
argument_tokens.skip_whitespace();
auto y_pos = parse_length_percentage(argument_tokens);
if (!y_pos.has_value())
return nullptr;
argument_tokens.skip_whitespace();
if (argument_tokens.has_next_token())
return nullptr;
points.append(Polygon::Point { *x_pos, *y_pos });
}
return BasicShapeStyleValue::create(Polygon { FillRule::Nonzero, move(points) });
}
RefPtr<StyleValue> Parser::parse_basic_shape_value(TokenStream<ComponentValue>& tokens)
{
auto basic_shape = parse_basic_shape_function(tokens.peek_token());
if (!basic_shape)
return nullptr;
(void)tokens.next_token();
return basic_shape;
}
CSSRule* Parser::convert_to_rule(NonnullRefPtr<Rule> rule)
{
if (rule->is_at_rule()) {

View File

@ -203,6 +203,9 @@ private:
Optional<URL::URL> parse_url_function(ComponentValue const&);
RefPtr<StyleValue> parse_url_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_basic_shape_function(ComponentValue const&);
RefPtr<StyleValue> parse_basic_shape_value(TokenStream<ComponentValue>&);
template<typename TElement>
Optional<Vector<TElement>> parse_color_stop_list(TokenStream<ComponentValue>& tokens, auto is_position, auto get_position);
Optional<Vector<LinearColorStopListElement>> parse_linear_color_stop_list(TokenStream<ComponentValue>&);