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.
This commit is contained in:
Sam Atkins 2025-07-10 16:09:34 +01:00 committed by Tim Ledbetter
commit 08bf9d39de
Notes: github-actions[bot] 2025-07-16 13:49:40 +00:00
4 changed files with 112 additions and 47 deletions

View file

@ -10,6 +10,7 @@
#include <LibWeb/CSS/Length.h> #include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/Resolution.h> #include <LibWeb/CSS/Resolution.h>
#include <LibWeb/CSS/Time.h> #include <LibWeb/CSS/Time.h>
#include <LibWeb/CSS/ValueType.h>
namespace Web::CSS { namespace Web::CSS {
@ -31,28 +32,9 @@ Optional<CSSNumericType::BaseType> CSSNumericType::base_type_from_value_type(Val
case ValueType::Time: case ValueType::Time:
return BaseType::Time; return BaseType::Time;
case ValueType::BackgroundPosition: default:
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:
return {}; return {};
} }
VERIFY_NOT_REACHED();
} }
// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-create-a-type // https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-create-a-type

View file

@ -0,0 +1,66 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/ValueType.h>
namespace Web::CSS {
Optional<ValueType> 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 {};
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
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<ValueType> value_type_from_string(StringView);
}

View file

@ -165,6 +165,7 @@ ErrorOr<void> generate_header_file(JsonObject& properties, JsonObject& logical_p
#include <AK/Variant.h> #include <AK/Variant.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibWeb/CSS/Enums.h> #include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/ValueType.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
namespace Web::CSS { namespace Web::CSS {
@ -255,33 +256,6 @@ Optional<PropertyID> property_id_from_string(StringView);
bool is_inherited_property(PropertyID); bool is_inherited_property(PropertyID);
NonnullRefPtr<CSSStyleValue const> property_initial_value(PropertyID); NonnullRefPtr<CSSStyleValue const> 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_type(PropertyID, ValueType);
bool property_accepts_keyword(PropertyID, Keyword); bool property_accepts_keyword(PropertyID, Keyword);
Optional<ValueType> property_resolves_percentages_relative_to(PropertyID); Optional<ValueType> property_resolves_percentages_relative_to(PropertyID);