Generate_CSS_PropertyID_h.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/File.h>
  11. #include <ctype.h>
  12. static String title_casify(const String& dashy_name)
  13. {
  14. auto parts = dashy_name.split('-');
  15. StringBuilder builder;
  16. for (auto& part : parts) {
  17. if (part.is_empty())
  18. continue;
  19. builder.append(toupper(part[0]));
  20. if (part.length() == 1)
  21. continue;
  22. builder.append(part.substring_view(1, part.length() - 1));
  23. }
  24. return builder.to_string();
  25. }
  26. int main(int argc, char** argv)
  27. {
  28. if (argc != 2) {
  29. warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
  30. return 1;
  31. }
  32. auto file = Core::File::construct(argv[1]);
  33. if (!file->open(Core::OpenMode::ReadOnly))
  34. return 1;
  35. auto json = JsonValue::from_string(file->read_all());
  36. VERIFY(json.has_value());
  37. VERIFY(json.value().is_object());
  38. StringBuilder builder;
  39. SourceGenerator generator { builder };
  40. generator.append(R"~~~(
  41. #pragma once
  42. #include <AK/StringView.h>
  43. #include <AK/Traits.h>
  44. #include <LibWeb/Forward.h>
  45. namespace Web::CSS {
  46. enum class PropertyID {
  47. Invalid,
  48. Custom,
  49. )~~~");
  50. Vector<String> shorthand_property_ids;
  51. Vector<String> longhand_property_ids;
  52. json.value().as_object().for_each_member([&](auto& name, auto& value) {
  53. VERIFY(value.is_object());
  54. if (value.as_object().has("longhands"))
  55. shorthand_property_ids.append(name);
  56. else
  57. longhand_property_ids.append(name);
  58. });
  59. auto first_property_id = shorthand_property_ids.first();
  60. auto last_property_id = longhand_property_ids.last();
  61. for (auto& name : shorthand_property_ids) {
  62. auto member_generator = generator.fork();
  63. member_generator.set("name:titlecase", title_casify(name));
  64. member_generator.append(R"~~~(
  65. @name:titlecase@,
  66. )~~~");
  67. }
  68. for (auto& name : longhand_property_ids) {
  69. auto member_generator = generator.fork();
  70. member_generator.set("name:titlecase", title_casify(name));
  71. member_generator.append(R"~~~(
  72. @name:titlecase@,
  73. )~~~");
  74. }
  75. generator.set("first_property_id", title_casify(first_property_id));
  76. generator.set("last_property_id", title_casify(last_property_id));
  77. generator.set("first_shorthand_property_id", title_casify(shorthand_property_ids.first()));
  78. generator.set("last_shorthand_property_id", title_casify(shorthand_property_ids.last()));
  79. generator.set("first_longhand_property_id", title_casify(longhand_property_ids.first()));
  80. generator.set("last_longhand_property_id", title_casify(longhand_property_ids.last()));
  81. generator.append(R"~~~(
  82. };
  83. PropertyID property_id_from_camel_case_string(StringView);
  84. PropertyID property_id_from_string(const StringView&);
  85. const char* string_from_property_id(PropertyID);
  86. bool is_inherited_property(PropertyID);
  87. bool is_pseudo_property(PropertyID);
  88. RefPtr<StyleValue> property_initial_value(PropertyID);
  89. bool property_accepts_value(PropertyID, StyleValue&);
  90. size_t property_maximum_value_count(PropertyID);
  91. constexpr PropertyID first_property_id = PropertyID::@first_property_id@;
  92. constexpr PropertyID last_property_id = PropertyID::@last_property_id@;
  93. constexpr PropertyID first_shorthand_property_id = PropertyID::@first_shorthand_property_id@;
  94. constexpr PropertyID last_shorthand_property_id = PropertyID::@last_shorthand_property_id@;
  95. constexpr PropertyID first_longhand_property_id = PropertyID::@first_longhand_property_id@;
  96. constexpr PropertyID last_longhand_property_id = PropertyID::@last_longhand_property_id@;
  97. enum class Quirk {
  98. // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
  99. HashlessHexColor,
  100. // https://quirks.spec.whatwg.org/#the-unitless-length-quirk
  101. UnitlessLength,
  102. };
  103. bool property_has_quirk(PropertyID, Quirk);
  104. } // namespace Web::CSS
  105. namespace AK {
  106. template<>
  107. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  108. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  109. };
  110. } // namespace AK
  111. )~~~");
  112. outln("{}", generator.as_string_view());
  113. }