From af58bddfc8bb264253663a30db111c19feacb630 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 11 Sep 2021 17:26:38 +0100 Subject: [PATCH] LibWeb: Generate CSS::property_has_quirk() function This lets you query if a given Quirk applies to a given PropertyID. Currently this applies only to the "Hashless hex color" and "Unitless length" quirks. --- .../LibWeb/Generate_CSS_PropertyID_cpp.cpp | 56 +++++++++++++++++-- .../LibWeb/Generate_CSS_PropertyID_h.cpp | 8 +++ 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp index 9852fd57d9c..3af8e6abfc7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_cpp.cpp @@ -40,6 +40,8 @@ int main(int argc, char** argv) VERIFY(json.has_value()); VERIFY(json.value().is_object()); + auto& properties = json.value().as_object(); + StringBuilder builder; SourceGenerator generator { builder }; @@ -55,7 +57,7 @@ PropertyID property_id_from_string(const StringView& string) { )~~~"); - json.value().as_object().for_each_member([&](auto& name, auto& value) { + properties.for_each_member([&](auto& name, auto& value) { VERIFY(value.is_object()); auto member_generator = generator.fork(); @@ -75,7 +77,7 @@ const char* string_from_property_id(PropertyID property_id) { switch (property_id) { )~~~"); - json.value().as_object().for_each_member([&](auto& name, auto& value) { + properties.for_each_member([&](auto& name, auto& value) { VERIFY(value.is_object()); auto member_generator = generator.fork(); @@ -98,7 +100,7 @@ bool is_inherited_property(PropertyID property_id) switch (property_id) { )~~~"); - json.value().as_object().for_each_member([&](auto& name, auto& value) { + properties.for_each_member([&](auto& name, auto& value) { VERIFY(value.is_object()); bool inherited = false; @@ -129,7 +131,7 @@ bool is_pseudo_property(PropertyID property_id) switch (property_id) { )~~~"); - json.value().as_object().for_each_member([&](auto& name, auto& value) { + properties.for_each_member([&](auto& name, auto& value) { VERIFY(value.is_object()); bool pseudo = false; @@ -162,7 +164,7 @@ RefPtr property_initial_value(PropertyID property_id) ParsingContext parsing_context; )~~~"); - json.value().as_object().for_each_member([&](auto& name, auto& value) { + properties.for_each_member([&](auto& name, auto& value) { VERIFY(value.is_object()); if (value.as_object().has("initial")) { @@ -185,6 +187,50 @@ RefPtr property_initial_value(PropertyID property_id) return initial_values.get(property_id).value_or(nullptr); } +bool property_has_quirk(PropertyID property_id, Quirk quirk) +{ + switch (property_id) { +)~~~"); + + properties.for_each_member([&](auto& name, auto& value) { + VERIFY(value.is_object()); + if (value.as_object().has("quirks")) { + auto& quirks_value = value.as_object().get("quirks"); + VERIFY(quirks_value.is_array()); + auto& quirks = quirks_value.as_array(); + + if (!quirks.is_empty()) { + auto property_generator = generator.fork(); + property_generator.set("name:titlecase", title_casify(name)); + property_generator.append(R"~~~( + case PropertyID::@name:titlecase@: { + switch (quirk) { +)~~~"); + for (auto& quirk : quirks.values()) { + VERIFY(quirk.is_string()); + auto quirk_generator = property_generator.fork(); + quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string())); + quirk_generator.append(R"~~~( + case Quirk::@quirk:titlecase@: + return true; +)~~~"); + } + property_generator.append(R"~~~( + default: + return false; + } + } +)~~~"); + } + } + }); + + generator.append(R"~~~( + default: + return false; + } +} + } // namespace Web::CSS )~~~"); diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_h.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_h.cpp index 355d77b28f8..bede1abac10 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_h.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/Generate_CSS_PropertyID_h.cpp @@ -76,6 +76,14 @@ bool is_inherited_property(PropertyID); bool is_pseudo_property(PropertyID); RefPtr property_initial_value(PropertyID); +enum class Quirk { + // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk + HashlessHexColor, + // https://quirks.spec.whatwg.org/#the-unitless-length-quirk + UnitlessLength, +}; +bool property_has_quirk(PropertyID, Quirk); + } // namespace Web::CSS namespace AK {