From f6fae315e31fb543817c6980a6182a589dbe3bda Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 25 May 2023 12:43:52 +0100 Subject: [PATCH] LibWeb: Add CustomIdentStyleValue, along with parsing for it This corresponds to the `` type in CSS grammar. --- .../LibWeb/GenerateCSSPropertyID.cpp | 5 ++- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 7 +++- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 7 ++++ Userland/Libraries/LibWeb/CSS/StyleValue.h | 4 ++ .../CSS/StyleValues/CustomIdentStyleValue.h | 39 +++++++++++++++++++ Userland/Libraries/LibWeb/Forward.h | 1 + 6 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 Userland/Libraries/LibWeb/CSS/StyleValues/CustomIdentStyleValue.h diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp index 351cf59d9b..fae37000c1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp @@ -17,7 +17,7 @@ ErrorOr generate_implementation_file(JsonObject& properties, Core::File& f static bool type_name_is_enum(StringView type_name) { - return !AK::first_is_one_of(type_name, "angle"sv, "color"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, "rect"sv, "resolution"sv, "string"sv, "time"sv, "url"sv); } ErrorOr serenity_main(Main::Arguments arguments) @@ -118,6 +118,7 @@ ErrorOr> property_initial_value(JS::Realm&, PropertyID enum class ValueType { Angle, Color, + CustomIdent, FilterValueList, Frequency, Image, @@ -455,6 +456,8 @@ bool property_accepts_type(PropertyID property_id, ValueType value_type) property_generator.appendln(" case ValueType::Angle:"); } else if (type_name == "color") { property_generator.appendln(" case ValueType::Color:"); + } else if (type_name == "custom-ident") { + property_generator.appendln(" case ValueType::CustomIdent:"); } else if (type_name == "frequency") { property_generator.appendln(" case ValueType::Frequency:"); } else if (type_name == "image") { diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 67c6e3f616..7c60c797b2 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -7205,7 +7206,11 @@ ErrorOr Parser::parse_css_value_for_properties(Readonl } } - // FIXME: Custom idents. https://www.w3.org/TR/css-values-4/#identifier-value + // Custom idents + if (auto property = any_property_accepts_type(property_ids, ValueType::CustomIdent); property.has_value()) { + (void)tokens.next_token(); + return PropertyAndValue { *property, TRY(CustomIdentStyleValue::create(TRY(FlyString::from_utf8(peek_token.token().ident())))) }; + } } if (auto property = any_property_accepts_type(property_ids, ValueType::Color); property.has_value()) { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 276effe1c3..60937e2df9 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -143,6 +144,12 @@ ContentStyleValue const& StyleValue::as_content() const return static_cast(*this); } +CustomIdentStyleValue const& StyleValue::as_custom_ident() const +{ + VERIFY(is_custom_ident()); + return static_cast(*this); +} + DisplayStyleValue const& StyleValue::as_display() const { VERIFY(is_display()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index badf589ffd..4fbb0cc1a9 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -98,6 +98,7 @@ public: Color, ConicGradient, Content, + CustomIdent, Display, Edge, FilterValueList, @@ -150,6 +151,7 @@ public: bool is_color() const { return type() == Type::Color; } bool is_conic_gradient() const { return type() == Type::ConicGradient; } bool is_content() const { return type() == Type::Content; } + bool is_custom_ident() const { return type() == Type::CustomIdent; } bool is_display() const { return type() == Type::Display; } bool is_edge() const { return type() == Type::Edge; } bool is_filter_value_list() const { return type() == Type::FilterValueList; } @@ -201,6 +203,7 @@ public: ColorStyleValue const& as_color() const; ConicGradientStyleValue const& as_conic_gradient() const; ContentStyleValue const& as_content() const; + CustomIdentStyleValue const& as_custom_ident() const; DisplayStyleValue const& as_display() const; EdgeStyleValue const& as_edge() const; FilterValueListStyleValue const& as_filter_value_list() const; @@ -250,6 +253,7 @@ public: ColorStyleValue& as_color() { return const_cast(const_cast(*this).as_color()); } ConicGradientStyleValue& as_conic_gradient() { return const_cast(const_cast(*this).as_conic_gradient()); } ContentStyleValue& as_content() { return const_cast(const_cast(*this).as_content()); } + CustomIdentStyleValue& as_custom_ident() { return const_cast(const_cast(*this).as_custom_ident()); } DisplayStyleValue& as_display() { return const_cast(const_cast(*this).as_display()); } EdgeStyleValue& as_edge() { return const_cast(const_cast(*this).as_edge()); } FilterValueListStyleValue& as_filter_value_list() { return const_cast(const_cast(*this).as_filter_value_list()); } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/CustomIdentStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/CustomIdentStyleValue.h new file mode 100644 index 0000000000..7c92c52d89 --- /dev/null +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/CustomIdentStyleValue.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::CSS { + +// https://www.w3.org/TR/css-values-4/#custom-idents +class CustomIdentStyleValue final : public StyleValueWithDefaultOperators { +public: + static ErrorOr> create(FlyString custom_ident) + { + return adopt_nonnull_ref_or_enomem(new (nothrow) CustomIdentStyleValue(move(custom_ident))); + } + virtual ~CustomIdentStyleValue() override = default; + + FlyString const& custom_ident() const { return m_custom_ident; } + + virtual ErrorOr to_string() const override { return m_custom_ident.to_string(); } + + bool properties_equal(CustomIdentStyleValue const& other) const { return m_custom_ident == other.m_custom_ident; } + +private: + explicit CustomIdentStyleValue(FlyString custom_ident) + : StyleValueWithDefaultOperators(Type::CustomIdent) + , m_custom_ident(move(custom_ident)) + { + } + + FlyString m_custom_ident; +}; + +} diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index ae77031791..68980477b7 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -84,6 +84,7 @@ class CSSStyleDeclaration; class CSSStyleRule; class CSSStyleSheet; class CSSSupportsRule; +class CustomIdentStyleValue; class Display; class DisplayStyleValue; class EdgeStyleValue;