From 8995839e3f0d4bc3018cb86a2dbd45d49460c969 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 16 Aug 2021 17:42:09 +0100 Subject: [PATCH] LibWeb: Generate property_initial_value() function from Properties.json Since we have initial-value data in Properties.json already, it makes sense to use that instead of needing to duplicate the same information in ComputedValues.h However, converting a StyleValue to the kind of types used in InitialValues is non-trivial. So this may or may not actually be useful. --- .../Generate_CSS_PropertyID_cpp.cpp | 32 +++++++++++++++++++ .../Generate_CSS_PropertyID_h.cpp | 2 ++ 2 files changed, 34 insertions(+) diff --git a/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_cpp.cpp b/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_cpp.cpp index 22937da989..9852fd57d9 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_cpp.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_cpp.cpp @@ -45,7 +45,9 @@ int main(int argc, char** argv) generator.append(R"~~~( #include +#include #include +#include namespace Web::CSS { @@ -153,6 +155,36 @@ bool is_pseudo_property(PropertyID property_id) } } +RefPtr property_initial_value(PropertyID property_id) +{ + static HashMap> initial_values; + if (initial_values.is_empty()) { + ParsingContext parsing_context; +)~~~"); + + json.value().as_object().for_each_member([&](auto& name, auto& value) { + VERIFY(value.is_object()); + + if (value.as_object().has("initial")) { + auto& initial_value = value.as_object().get("initial"); + VERIFY(initial_value.is_string()); + auto initial_value_string = initial_value.as_string(); + + auto member_generator = generator.fork(); + member_generator.set("name:titlecase", title_casify(name)); + member_generator.set("initial_value_string", initial_value_string); + member_generator.append(R"~~~( + if (auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@)) + initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull()); +)~~~"); + } + }); + + generator.append(R"~~~( + } + return initial_values.get(property_id).value_or(nullptr); +} + } // namespace Web::CSS )~~~"); diff --git a/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_h.cpp b/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_h.cpp index 89ad93c4d1..355d77b28f 100644 --- a/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_h.cpp +++ b/Userland/Libraries/LibWeb/CodeGenerators/Generate_CSS_PropertyID_h.cpp @@ -47,6 +47,7 @@ int main(int argc, char** argv) #include #include +#include namespace Web::CSS { @@ -73,6 +74,7 @@ PropertyID property_id_from_string(const StringView&); const char* string_from_property_id(PropertyID); bool is_inherited_property(PropertyID); bool is_pseudo_property(PropertyID); +RefPtr property_initial_value(PropertyID); } // namespace Web::CSS