1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 01:17:34 +00:00

LibWeb: Parse repeating-conic-gradient()s

This commit is contained in:
MacDue 2022-11-06 14:51:10 +00:00 committed by Sam Atkins
parent bee4df7bfb
commit 2c2b9fb1d7
3 changed files with 38 additions and 25 deletions

View file

@ -2388,15 +2388,8 @@ static Optional<Vector<TElement>> parse_color_stop_list(auto& tokens, auto is_po
return color_stops;
}
RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const& component_value)
static StringView consume_if_starts_with(StringView str, StringView start, auto found_callback)
{
using GradientType = LinearGradientStyleValue::GradientType;
using Repeating = LinearGradientStyleValue::Repeating;
if (!component_value.is_function())
return {};
auto consume_if_starts_with = [](StringView str, StringView start, auto found_callback) {
if (str.starts_with(start, CaseSensitivity::CaseInsensitive)) {
found_callback();
return str.substring_view(start.length());
@ -2404,7 +2397,14 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
return str;
};
Repeating repeating_gradient = Repeating::No;
RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const& component_value)
{
using GradientType = LinearGradientStyleValue::GradientType;
if (!component_value.is_function())
return {};
GradientRepeating repeating_gradient = GradientRepeating::No;
GradientType gradient_type { GradientType::Standard };
auto function_name = component_value.function().name();
@ -2414,7 +2414,7 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
});
function_name = consume_if_starts_with(function_name, "repeating-"sv, [&] {
repeating_gradient = Repeating::Yes;
repeating_gradient = GradientRepeating::Yes;
});
if (!function_name.equals_ignoring_case("linear-gradient"sv))
@ -2541,8 +2541,15 @@ RefPtr<StyleValue> Parser::parse_conic_gradient_function(ComponentValue const& c
{
if (!component_value.is_function())
return {};
GradientRepeating repeating_gradient = GradientRepeating::No;
auto function_name = component_value.function().name();
function_name = consume_if_starts_with(function_name, "repeating-"sv, [&] {
repeating_gradient = GradientRepeating::Yes;
});
if (!function_name.equals_ignoring_case("conic-gradient"sv))
return {};
@ -2636,7 +2643,7 @@ RefPtr<StyleValue> Parser::parse_conic_gradient_function(ComponentValue const& c
if (!color_stops.has_value())
return {};
return ConicGradientStyleValue::create(from_angle, at_position, move(*color_stops));
return ConicGradientStyleValue::create(from_angle, at_position, move(*color_stops), repeating_gradient);
}
Optional<PositionValue> Parser::parse_position(TokenStream<ComponentValue>& tokens)

View file

@ -1751,7 +1751,7 @@ String LinearGradientStyleValue::to_string() const
if (m_gradient_type == GradientType::WebKit)
builder.append("-webkit-"sv);
if (m_repeating == Repeating::Yes)
if (is_repeating())
builder.append("repeating-"sv);
builder.append("linear-gradient("sv);
m_direction.visit(
@ -1958,6 +1958,8 @@ bool PositionValue::operator==(PositionValue const& other) const
String ConicGradientStyleValue::to_string() const
{
StringBuilder builder;
if (is_repeating())
builder.append("repeating-"sv);
builder.append("conic-gradient("sv);
bool has_from_angle = false;
bool has_at_position = false;

View file

@ -1190,12 +1190,17 @@ private:
RefPtr<Gfx::Bitmap> m_bitmap;
};
enum class GradientRepeating {
Yes,
No
};
class ConicGradientStyleValue final : public AbstractImageStyleValue {
public:
static NonnullRefPtr<ConicGradientStyleValue> create(Angle from_angle, PositionValue position, Vector<AngularColorStopListElement> color_stop_list)
static NonnullRefPtr<ConicGradientStyleValue> create(Angle from_angle, PositionValue position, Vector<AngularColorStopListElement> color_stop_list, GradientRepeating repeating)
{
VERIFY(color_stop_list.size() >= 2);
return adopt_ref(*new ConicGradientStyleValue(from_angle, position, move(color_stop_list)));
return adopt_ref(*new ConicGradientStyleValue(from_angle, position, move(color_stop_list), repeating));
}
virtual String to_string() const override;
@ -1219,12 +1224,15 @@ public:
Gfx::FloatPoint resolve_position(Layout::Node const&, Gfx::FloatRect const&) const;
bool is_repeating() const { return m_repeating == GradientRepeating::Yes; }
private:
ConicGradientStyleValue(Angle from_angle, PositionValue position, Vector<AngularColorStopListElement> color_stop_list)
ConicGradientStyleValue(Angle from_angle, PositionValue position, Vector<AngularColorStopListElement> color_stop_list, GradientRepeating repeating)
: AbstractImageStyleValue(Type::ConicGradient)
, m_from_angle(from_angle)
, m_position(position)
, m_color_stop_list(move(color_stop_list))
, m_repeating(repeating)
{
}
@ -1232,6 +1240,7 @@ private:
Angle m_from_angle;
PositionValue m_position;
Vector<AngularColorStopListElement> m_color_stop_list;
GradientRepeating m_repeating;
struct ResolvedData {
Painting::ConicGradientData data;
@ -1250,12 +1259,7 @@ public:
WebKit
};
enum class Repeating {
Yes,
No
};
static NonnullRefPtr<LinearGradientStyleValue> create(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, Repeating repeating)
static NonnullRefPtr<LinearGradientStyleValue> create(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
{
VERIFY(color_stop_list.size() >= 2);
return adopt_ref(*new LinearGradientStyleValue(direction, move(color_stop_list), type, repeating));
@ -1270,7 +1274,7 @@ public:
return m_color_stop_list;
}
bool is_repeating() const { return m_repeating == Repeating::Yes; }
bool is_repeating() const { return m_repeating == GradientRepeating::Yes; }
float angle_degrees(Gfx::FloatSize const& gradient_size) const;
@ -1280,7 +1284,7 @@ public:
void paint(PaintContext& context, Gfx::IntRect const& dest_rect, CSS::ImageRendering image_rendering) const override;
private:
LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, Repeating repeating)
LinearGradientStyleValue(GradientDirection direction, Vector<LinearColorStopListElement> color_stop_list, GradientType type, GradientRepeating repeating)
: AbstractImageStyleValue(Type::LinearGradient)
, m_direction(direction)
, m_color_stop_list(move(color_stop_list))
@ -1292,7 +1296,7 @@ private:
GradientDirection m_direction;
Vector<LinearColorStopListElement> m_color_stop_list;
GradientType m_gradient_type;
Repeating m_repeating;
GradientRepeating m_repeating;
struct ResolvedData {
Painting::LinearGradientData data;