Browse Source

LibWeb: Generate CSS::property_id_from_camel_case_string()

This allows us to resolve a "camelCase" CSS property name to our own
CSS::PropertyID enum. This will be used by CSSOM bindings.
Andreas Kling 3 years ago
parent
commit
dadb92a155

+ 42 - 0
Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp

@@ -26,6 +26,27 @@ static String title_casify(const String& dashy_name)
     return builder.to_string();
 }
 
+static String camel_casify(StringView dashy_name)
+{
+    auto parts = dashy_name.split_view('-');
+    StringBuilder builder;
+    bool first = true;
+    for (auto& part : parts) {
+        if (part.is_empty())
+            continue;
+        char ch = part[0];
+        if (!first)
+            ch = toupper(ch);
+        else
+            first = false;
+        builder.append(ch);
+        if (part.length() == 1)
+            continue;
+        builder.append(part.substring_view(1, part.length() - 1));
+    }
+    return builder.to_string();
+}
+
 int main(int argc, char** argv)
 {
     if (argc != 2) {
@@ -53,6 +74,27 @@ int main(int argc, char** argv)
 
 namespace Web::CSS {
 
+PropertyID property_id_from_camel_case_string(StringView string)
+{
+)~~~");
+
+    properties.for_each_member([&](auto& name, auto& value) {
+        VERIFY(value.is_object());
+
+        auto member_generator = generator.fork();
+        member_generator.set("name", name);
+        member_generator.set("name:titlecase", title_casify(name));
+        member_generator.set("name:camelcase", camel_casify(name));
+        member_generator.append(R"~~~(
+    if (string.equals_ignoring_case("@name:camelcase@"sv))
+        return PropertyID::@name:titlecase@;
+)~~~");
+    });
+
+    generator.append(R"~~~(
+    return PropertyID::Invalid;
+}
+
 PropertyID property_id_from_string(const StringView& string)
 {
 )~~~");

+ 1 - 0
Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_h.cpp

@@ -100,6 +100,7 @@ enum class PropertyID {
     generator.append(R"~~~(
 };
 
+PropertyID property_id_from_camel_case_string(StringView);
 PropertyID property_id_from_string(const StringView&);
 const char* string_from_property_id(PropertyID);
 bool is_inherited_property(PropertyID);