From 08bf9d39de7dd59fe9eea4a214e2b8a12b385317 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 10 Jul 2025 16:09:34 +0100 Subject: [PATCH] LibWeb/CSS: Add String->ValueType conversion function And also move this out of PropertyID.h because it's a separate thing. I considered generating this but there's really not much to it. --- Libraries/LibWeb/CSS/CSSNumericType.cpp | 22 +------ Libraries/LibWeb/CSS/ValueType.cpp | 66 +++++++++++++++++++ Libraries/LibWeb/CSS/ValueType.h | 43 ++++++++++++ .../LibWeb/GenerateCSSPropertyID.cpp | 28 +------- 4 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 Libraries/LibWeb/CSS/ValueType.cpp create mode 100644 Libraries/LibWeb/CSS/ValueType.h diff --git a/Libraries/LibWeb/CSS/CSSNumericType.cpp b/Libraries/LibWeb/CSS/CSSNumericType.cpp index 2e1b11630d3..5b2a4361254 100644 --- a/Libraries/LibWeb/CSS/CSSNumericType.cpp +++ b/Libraries/LibWeb/CSS/CSSNumericType.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace Web::CSS { @@ -31,28 +32,9 @@ Optional CSSNumericType::base_type_from_value_type(Val case ValueType::Time: return BaseType::Time; - case ValueType::BackgroundPosition: - case ValueType::BasicShape: - case ValueType::Color: - case ValueType::Counter: - case ValueType::CustomIdent: - case ValueType::EasingFunction: - case ValueType::FilterValueList: - case ValueType::FitContent: - case ValueType::Image: - case ValueType::Integer: - case ValueType::Number: - case ValueType::OpenTypeTag: - case ValueType::Paint: - case ValueType::Position: - case ValueType::Ratio: - case ValueType::Rect: - case ValueType::String: - case ValueType::Url: + default: return {}; } - - VERIFY_NOT_REACHED(); } // https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-create-a-type diff --git a/Libraries/LibWeb/CSS/ValueType.cpp b/Libraries/LibWeb/CSS/ValueType.cpp new file mode 100644 index 00000000000..e4765a7ec5c --- /dev/null +++ b/Libraries/LibWeb/CSS/ValueType.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Web::CSS { + +Optional value_type_from_string(StringView name) +{ + if (string.equals_ignoring_ascii_case("angle"sv)) + return ValueType::Angle; + if (string.equals_ignoring_ascii_case("background-position"sv)) + return ValueType::BackgroundPosition; + if (string.equals_ignoring_ascii_case("basic-shape"sv)) + return ValueType::BasicShape; + if (string.equals_ignoring_ascii_case("color"sv)) + return ValueType::Color; + if (string.equals_ignoring_ascii_case("counter"sv)) + return ValueType::Counter; + if (string.equals_ignoring_ascii_case("custom-ident"sv)) + return ValueType::CustomIdent; + if (string.equals_ignoring_ascii_case("easing-function"sv)) + return ValueType::EasingFunction; + if (string.equals_ignoring_ascii_case("filter-value-list"sv)) + return ValueType::FilterValueList; + if (string.equals_ignoring_ascii_case("fit-content"sv)) + return ValueType::FitContent; + if (string.equals_ignoring_ascii_case("flex"sv)) + return ValueType::Flex; + if (string.equals_ignoring_ascii_case("frequency"sv)) + return ValueType::Frequency; + if (string.equals_ignoring_ascii_case("image"sv)) + return ValueType::Image; + if (string.equals_ignoring_ascii_case("integer"sv)) + return ValueType::Integer; + if (string.equals_ignoring_ascii_case("length"sv)) + return ValueType::Length; + if (string.equals_ignoring_ascii_case("number"sv)) + return ValueType::Number; + if (string.equals_ignoring_ascii_case("opentype-tag"sv)) + return ValueType::OpenTypeTag; + if (string.equals_ignoring_ascii_case("paint"sv)) + return ValueType::Paint; + if (string.equals_ignoring_ascii_case("percentage"sv)) + return ValueType::Percentage; + if (string.equals_ignoring_ascii_case("position"sv)) + return ValueType::Position; + if (string.equals_ignoring_ascii_case("ratio"sv)) + return ValueType::Ratio; + if (string.equals_ignoring_ascii_case("rect"sv)) + return ValueType::Rect; + if (string.equals_ignoring_ascii_case("resolution"sv)) + return ValueType::Resolution; + if (string.equals_ignoring_ascii_case("string"sv)) + return ValueType::String; + if (string.equals_ignoring_ascii_case("time"sv)) + return ValueType::Time; + if (string.equals_ignoring_ascii_case("url"sv)) + return ValueType::Url; + return {}; +} + +} diff --git a/Libraries/LibWeb/CSS/ValueType.h b/Libraries/LibWeb/CSS/ValueType.h new file mode 100644 index 00000000000..df3ffc194c1 --- /dev/null +++ b/Libraries/LibWeb/CSS/ValueType.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::CSS { + +enum class ValueType : u8 { + Angle, + BackgroundPosition, + BasicShape, + Color, + Counter, + CustomIdent, + EasingFunction, + FilterValueList, + FitContent, + Flex, + Frequency, + Image, + Integer, + Length, + Number, + OpenTypeTag, + Paint, + Percentage, + Position, + Ratio, + Rect, + Resolution, + String, + Time, + Url, +}; + +Optional value_type_from_string(StringView); + +} diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp index 97473c5f5ec..cf66b8d82d5 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp @@ -165,6 +165,7 @@ ErrorOr generate_header_file(JsonObject& properties, JsonObject& logical_p #include #include #include +#include #include namespace Web::CSS { @@ -255,33 +256,6 @@ Optional property_id_from_string(StringView); bool is_inherited_property(PropertyID); NonnullRefPtr property_initial_value(PropertyID); -enum class ValueType { - Angle, - BackgroundPosition, - BasicShape, - Color, - Counter, - CustomIdent, - EasingFunction, - FilterValueList, - FitContent, - Flex, - Frequency, - Image, - Integer, - Length, - Number, - OpenTypeTag, - Paint, - Percentage, - Position, - Ratio, - Rect, - Resolution, - String, - Time, - Url, -}; bool property_accepts_type(PropertyID, ValueType); bool property_accepts_keyword(PropertyID, Keyword); Optional property_resolves_percentages_relative_to(PropertyID);