123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
- #include <AK/ByteBuffer.h>
- #include <AK/JsonObject.h>
- #include <AK/SourceGenerator.h>
- #include <AK/StringBuilder.h>
- #include <LibCore/File.h>
- #include <ctype.h>
- static String title_casify(const String& dashy_name)
- {
- auto parts = dashy_name.split('-');
- StringBuilder builder;
- for (auto& part : parts) {
- if (part.is_empty())
- continue;
- builder.append(toupper(part[0]));
- 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) {
- warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
- return 1;
- }
- auto file = Core::File::construct(argv[1]);
- if (!file->open(Core::OpenMode::ReadOnly))
- return 1;
- auto json = JsonValue::from_string(file->read_all());
- VERIFY(json.has_value());
- VERIFY(json.value().is_object());
- auto& properties = json.value().as_object();
- StringBuilder builder;
- SourceGenerator generator { builder };
- generator.append(R"~~~(
- #include <AK/Assertions.h>
- #include <LibWeb/CSS/Parser/Parser.h>
- #include <LibWeb/CSS/PropertyID.h>
- #include <LibWeb/CSS/StyleValue.h>
- namespace Web::CSS {
- PropertyID property_id_from_string(const 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.append(R"~~~(
- if (string.equals_ignoring_case("@name@"))
- return PropertyID::@name:titlecase@;
- )~~~");
- });
- generator.append(R"~~~(
- return PropertyID::Invalid;
- }
- const char* string_from_property_id(PropertyID property_id) {
- switch (property_id) {
- )~~~");
- 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.append(R"~~~(
- case PropertyID::@name:titlecase@:
- return "@name@";
- )~~~");
- });
- generator.append(R"~~~(
- default:
- return "(invalid CSS::PropertyID)";
- }
- }
- bool is_inherited_property(PropertyID property_id)
- {
- switch (property_id) {
- )~~~");
- properties.for_each_member([&](auto& name, auto& value) {
- VERIFY(value.is_object());
- bool inherited = false;
- if (value.as_object().has("inherited")) {
- auto& inherited_value = value.as_object().get("inherited");
- VERIFY(inherited_value.is_bool());
- inherited = inherited_value.as_bool();
- }
- if (inherited) {
- auto member_generator = generator.fork();
- member_generator.set("name:titlecase", title_casify(name));
- member_generator.append(R"~~~(
- case PropertyID::@name:titlecase@:
- return true;
- )~~~");
- }
- });
- generator.append(R"~~~(
- default:
- return false;
- }
- }
- bool is_pseudo_property(PropertyID property_id)
- {
- switch (property_id) {
- )~~~");
- properties.for_each_member([&](auto& name, auto& value) {
- VERIFY(value.is_object());
- bool pseudo = false;
- if (value.as_object().has("pseudo")) {
- auto& pseudo_value = value.as_object().get("pseudo");
- VERIFY(pseudo_value.is_bool());
- pseudo = pseudo_value.as_bool();
- }
- if (pseudo) {
- auto member_generator = generator.fork();
- member_generator.set("name:titlecase", title_casify(name));
- member_generator.append(R"~~~(
- case PropertyID::@name:titlecase@:
- return true;
- )~~~");
- }
- });
- generator.append(R"~~~(
- default:
- return false;
- }
- }
- RefPtr<StyleValue> property_initial_value(PropertyID property_id)
- {
- static HashMap<PropertyID, NonnullRefPtr<StyleValue>> initial_values;
- if (initial_values.is_empty()) {
- ParsingContext parsing_context;
- )~~~");
- // NOTE: Parsing a shorthand property requires that its longhands are already available here.
- // So, we do this in two passes: First longhands, then shorthands.
- // Probably we should build a dependency graph and then handle them in order, but this
- // works for now! :^)
- auto output_initial_value_code = [&](auto& name, auto& object) {
- if (object.has("initial")) {
- auto& initial_value = 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());
- )~~~");
- }
- };
- properties.for_each_member([&](auto& name, auto& value) {
- VERIFY(value.is_object());
- if (value.as_object().has("longhands"))
- return;
- output_initial_value_code(name, value.as_object());
- });
- properties.for_each_member([&](auto& name, auto& value) {
- VERIFY(value.is_object());
- if (!value.as_object().has("longhands"))
- return;
- output_initial_value_code(name, value.as_object());
- });
- generator.append(R"~~~(
- }
- auto it = initial_values.find(property_id);
- if (it == initial_values.end())
- return nullptr;
- return it->value;
- }
- 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;
- }
- }
- size_t property_maximum_value_count(PropertyID property_id)
- {
- switch (property_id) {
- )~~~");
- properties.for_each_member([&](auto& name, auto& value) {
- VERIFY(value.is_object());
- if (value.as_object().has("max-values")) {
- auto max_values = value.as_object().get("max-values");
- VERIFY(max_values.is_number() && !max_values.is_double());
- auto property_generator = generator.fork();
- property_generator.set("name:titlecase", title_casify(name));
- property_generator.set("max_values", max_values.to_string());
- property_generator.append(R"~~~(
- case PropertyID::@name:titlecase@:
- return @max_values@;
- )~~~");
- }
- });
- generator.append(R"~~~(
- default:
- return 1;
- }
- }
- } // namespace Web::CSS
- )~~~");
- outln("{}", generator.as_string_view());
- }
|