LibWeb: Generate a math_function_from_string() function

This will be used as a convenient way to see if a string is the name of
a math function.
This commit is contained in:
Sam Atkins 2025-08-21 16:13:30 +01:00 committed by Andreas Kling
commit 73d7d6b831
Notes: github-actions[bot] 2025-08-29 09:59:04 +00:00

View file

@ -22,9 +22,13 @@ ErrorOr<void> generate_header_file(JsonObject& functions_data, Core::File& file)
#pragma once #pragma once
#include <AK/Optional.h>
#include <AK/StringView.h>
namespace Web::CSS { namespace Web::CSS {
enum class MathFunction { enum class MathFunction {
Calc,
)~~~"); )~~~");
functions_data.for_each_member([&](auto& name, auto&) { functions_data.for_each_member([&](auto& name, auto&) {
@ -36,6 +40,8 @@ enum class MathFunction {
generator.append(R"~~~( generator.append(R"~~~(
}; };
Optional<MathFunction> math_function_from_string(StringView);
} }
)~~~"); )~~~");
@ -87,12 +93,36 @@ ErrorOr<void> generate_implementation_file(JsonObject& functions_data, Core::Fil
generator.append(R"~~~( generator.append(R"~~~(
// This file is generated by GenerateCSSMathFunctions.cpp // This file is generated by GenerateCSSMathFunctions.cpp
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/MathFunctions.h> #include <LibWeb/CSS/MathFunctions.h>
#include <LibWeb/CSS/Parser/ErrorReporter.h> #include <LibWeb/CSS/Parser/ErrorReporter.h>
#include <LibWeb/CSS/Parser/Parser.h> #include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h> #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
namespace Web::CSS {
Optional<MathFunction> math_function_from_string(StringView name)
{
if (name.equals_ignoring_ascii_case("calc"sv))
return MathFunction::Calc;
)~~~");
functions_data.for_each_member([&](auto& name, auto&) {
auto member_generator = generator.fork();
member_generator.set("name:lowercase", name);
member_generator.set("name:titlecase", title_casify(name));
member_generator.append(R"~~~(
if (name.equals_ignoring_ascii_case("@name:lowercase@"sv))
return MathFunction::@name:titlecase@;
)~~~");
});
generator.append(R"~~~(
return {};
}
}
namespace Web::CSS::Parser { namespace Web::CSS::Parser {
static Optional<RoundingStrategy> parse_rounding_strategy(Vector<ComponentValue> const& tokens) static Optional<RoundingStrategy> parse_rounding_strategy(Vector<ComponentValue> const& tokens)