From 73d7d6b83120db60c45ffa6fac7b0359e5cb68ea Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 21 Aug 2025 16:13:30 +0100 Subject: [PATCH] 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. --- .../LibWeb/GenerateCSSMathFunctions.cpp | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSMathFunctions.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSMathFunctions.cpp index 721cd29ac47..241f0a6ea60 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSMathFunctions.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSMathFunctions.cpp @@ -22,9 +22,13 @@ ErrorOr generate_header_file(JsonObject& functions_data, Core::File& file) #pragma once +#include +#include + namespace Web::CSS { enum class MathFunction { + Calc, )~~~"); functions_data.for_each_member([&](auto& name, auto&) { @@ -36,6 +40,8 @@ enum class MathFunction { generator.append(R"~~~( }; +Optional math_function_from_string(StringView); + } )~~~"); @@ -87,12 +93,36 @@ ErrorOr generate_implementation_file(JsonObject& functions_data, Core::Fil generator.append(R"~~~( // This file is generated by GenerateCSSMathFunctions.cpp +#include #include #include #include -#include #include +namespace Web::CSS { + +Optional 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 { static Optional parse_rounding_strategy(Vector const& tokens)