LibWeb: Add RatioStyleValue and parsing

This commit is contained in:
Sam Atkins 2023-06-06 15:42:43 +01:00 committed by Andreas Kling
parent b9f9d87bd0
commit 5e3da93f1a
Notes: sideshowbarker 2024-07-16 23:38:54 +09:00
8 changed files with 88 additions and 2 deletions

View File

@ -20,7 +20,7 @@ void generate_bounds_checking_function(JsonObject& properties, SourceGenerator&
static bool type_name_is_enum(StringView type_name)
{
return !AK::first_is_one_of(type_name, "angle"sv, "color"sv, "custom-ident"sv, "frequency"sv, "image"sv, "integer"sv, "length"sv, "number"sv, "percentage"sv, "rect"sv, "resolution"sv, "string"sv, "time"sv, "url"sv);
return !AK::first_is_one_of(type_name, "angle"sv, "color"sv, "custom-ident"sv, "frequency"sv, "image"sv, "integer"sv, "length"sv, "number"sv, "percentage"sv, "ratio"sv, "rect"sv, "resolution"sv, "string"sv, "time"sv, "url"sv);
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
@ -167,6 +167,7 @@ enum class ValueType {
Paint,
Percentage,
Position,
Ratio,
Rect,
Resolution,
String,
@ -612,6 +613,8 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type)
property_generator.appendln(" case ValueType::Number:");
} else if (type_name == "percentage") {
property_generator.appendln(" case ValueType::Percentage:");
} else if (type_name == "ratio") {
property_generator.appendln(" case ValueType::Ratio:");
} else if (type_name == "rect") {
property_generator.appendln(" case ValueType::Rect:");
} else if (type_name == "resolution") {

View File

@ -72,6 +72,7 @@
#include <LibWeb/CSS/StyleValues/PlaceContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
@ -4387,6 +4388,13 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_color_value(ComponentValue const& comp
return nullptr;
}
ErrorOr<RefPtr<StyleValue>> Parser::parse_ratio_value(TokenStream<ComponentValue>& tokens)
{
if (auto ratio = parse_ratio(tokens); ratio.has_value())
return RatioStyleValue::create(ratio.release_value());
return nullptr;
}
ErrorOr<RefPtr<StyleValue>> Parser::parse_string_value(ComponentValue const& component_value)
{
if (component_value.is(Token::Type::String))
@ -7626,6 +7634,11 @@ ErrorOr<Parser::PropertyAndValue> Parser::parse_css_value_for_properties(Readonl
}
}
if (auto property = any_property_accepts_type(property_ids, ValueType::Ratio); property.has_value()) {
if (auto maybe_ratio = TRY(parse_ratio_value(tokens)))
return PropertyAndValue { *property, maybe_ratio };
}
auto property_accepting_integer = any_property_accepts_type(property_ids, ValueType::Integer);
auto property_accepting_number = any_property_accepts_type(property_ids, ValueType::Number);
bool property_accepts_numeric = property_accepting_integer.has_value() || property_accepting_number.has_value();

View File

@ -299,6 +299,7 @@ private:
ErrorOr<RefPtr<StyleValue>> parse_identifier_value(ComponentValue const&);
ErrorOr<RefPtr<StyleValue>> parse_color_value(ComponentValue const&);
ErrorOr<RefPtr<StyleValue>> parse_rect_value(ComponentValue const&);
ErrorOr<RefPtr<StyleValue>> parse_ratio_value(TokenStream<ComponentValue>&);
ErrorOr<RefPtr<StyleValue>> parse_string_value(ComponentValue const&);
ErrorOr<RefPtr<StyleValue>> parse_image_value(ComponentValue const&);
template<typename ParseFunction>

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -19,6 +19,23 @@ public:
ErrorOr<String> to_string() const;
bool operator==(Ratio const& other) const
{
return value() == other.value();
}
int operator<=>(Ratio const& other) const
{
auto this_value = value();
auto other_value = other.value();
if (this_value < other_value)
return -1;
if (this_value > other_value)
return 1;
return 0;
}
private:
float m_first_value { 0 };
float m_second_value { 1 };

View File

@ -50,6 +50,7 @@
#include <LibWeb/CSS/StyleValues/PlaceContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
@ -321,6 +322,12 @@ RadialGradientStyleValue const& StyleValue::as_radial_gradient() const
return static_cast<RadialGradientStyleValue const&>(*this);
}
RatioStyleValue const& StyleValue::as_ratio() const
{
VERIFY(is_ratio());
return static_cast<RatioStyleValue const&>(*this);
}
RectStyleValue const& StyleValue::as_rect() const
{
VERIFY(is_rect());

View File

@ -127,6 +127,7 @@ public:
PlaceContent,
Position,
RadialGradient,
Ratio,
Rect,
Resolution,
Shadow,
@ -183,6 +184,7 @@ public:
bool is_place_content() const { return type() == Type::PlaceContent; }
bool is_position() const { return type() == Type::Position; }
bool is_radial_gradient() const { return type() == Type::RadialGradient; }
bool is_ratio() const { return type() == Type::Ratio; }
bool is_rect() const { return type() == Type::Rect; }
bool is_resolution() const { return type() == Type::Resolution; }
bool is_shadow() const { return type() == Type::Shadow; }
@ -238,6 +240,7 @@ public:
PlaceContentStyleValue const& as_place_content() const;
PositionStyleValue const& as_position() const;
RadialGradientStyleValue const& as_radial_gradient() const;
RatioStyleValue const& as_ratio() const;
RectStyleValue const& as_rect() const;
ResolutionStyleValue const& as_resolution() const;
ShadowStyleValue const& as_shadow() const;
@ -290,6 +293,7 @@ public:
PlaceContentStyleValue& as_place_content() { return const_cast<PlaceContentStyleValue&>(const_cast<StyleValue const&>(*this).as_place_content()); }
PositionStyleValue& as_position() { return const_cast<PositionStyleValue&>(const_cast<StyleValue const&>(*this).as_position()); }
RadialGradientStyleValue& as_radial_gradient() { return const_cast<RadialGradientStyleValue&>(const_cast<StyleValue const&>(*this).as_radial_gradient()); }
RatioStyleValue& as_ratio() { return const_cast<RatioStyleValue&>(const_cast<StyleValue const&>(*this).as_ratio()); }
RectStyleValue& as_rect() { return const_cast<RectStyleValue&>(const_cast<StyleValue const&>(*this).as_rect()); }
ResolutionStyleValue& as_resolution() { return const_cast<ResolutionStyleValue&>(const_cast<StyleValue const&>(*this).as_resolution()); }
ShadowStyleValue& as_shadow() { return const_cast<ShadowStyleValue&>(const_cast<StyleValue const&>(*this).as_shadow()); }

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/Ratio.h>
#include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS {
class RatioStyleValue final : public StyleValueWithDefaultOperators<RatioStyleValue> {
public:
static ErrorOr<ValueComparingNonnullRefPtr<RatioStyleValue>> create(Ratio ratio)
{
return adopt_nonnull_ref_or_enomem(new (nothrow) RatioStyleValue(move(ratio)));
}
virtual ~RatioStyleValue() override = default;
Ratio const& ratio() const { return m_ratio; }
Ratio& ratio() { return m_ratio; }
virtual ErrorOr<String> to_string() const override { return m_ratio.to_string(); }
bool properties_equal(RatioStyleValue const& other) const { return m_ratio == other.m_ratio; }
private:
RatioStyleValue(Ratio&& ratio)
: StyleValueWithDefaultOperators(Type::Ratio)
, m_ratio(ratio)
{
}
Ratio m_ratio;
};
}

View File

@ -140,6 +140,8 @@ class PlaceContentStyleValue;
class PositionStyleValue;
class PropertyOwningCSSStyleDeclaration;
class RadialGradientStyleValue;
class Ratio;
class RatioStyleValue;
class RectStyleValue;
class Resolution;
class ResolutionStyleValue;