Generate_CSS_PropertyID_h.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GeneratorUtil.h"
  7. #include <AK/SourceGenerator.h>
  8. #include <AK/StringBuilder.h>
  9. #include <LibMain/Main.h>
  10. ErrorOr<int> serenity_main(Main::Arguments arguments)
  11. {
  12. if (arguments.argc != 2) {
  13. warnln("usage: {} <path/to/CSS/Properties.json>", arguments.strings[0]);
  14. return 1;
  15. }
  16. auto json = TRY(read_entire_file_as_json(arguments.strings[1]));
  17. VERIFY(json.is_object());
  18. StringBuilder builder;
  19. SourceGenerator generator { builder };
  20. generator.append(R"~~~(
  21. #pragma once
  22. #include <AK/NonnullRefPtr.h>
  23. #include <AK/StringView.h>
  24. #include <AK/Traits.h>
  25. #include <LibWeb/Forward.h>
  26. namespace Web::CSS {
  27. enum class PropertyID {
  28. Invalid,
  29. Custom,
  30. )~~~");
  31. Vector<String> shorthand_property_ids;
  32. Vector<String> longhand_property_ids;
  33. json.as_object().for_each_member([&](auto& name, auto& value) {
  34. VERIFY(value.is_object());
  35. if (value.as_object().has("longhands"))
  36. shorthand_property_ids.append(name);
  37. else
  38. longhand_property_ids.append(name);
  39. });
  40. auto first_property_id = shorthand_property_ids.first();
  41. auto last_property_id = longhand_property_ids.last();
  42. for (auto& name : shorthand_property_ids) {
  43. auto member_generator = generator.fork();
  44. member_generator.set("name:titlecase", title_casify(name));
  45. member_generator.append(R"~~~(
  46. @name:titlecase@,
  47. )~~~");
  48. }
  49. for (auto& name : longhand_property_ids) {
  50. auto member_generator = generator.fork();
  51. member_generator.set("name:titlecase", title_casify(name));
  52. member_generator.append(R"~~~(
  53. @name:titlecase@,
  54. )~~~");
  55. }
  56. generator.set("first_property_id", title_casify(first_property_id));
  57. generator.set("last_property_id", title_casify(last_property_id));
  58. generator.set("first_shorthand_property_id", title_casify(shorthand_property_ids.first()));
  59. generator.set("last_shorthand_property_id", title_casify(shorthand_property_ids.last()));
  60. generator.set("first_longhand_property_id", title_casify(longhand_property_ids.first()));
  61. generator.set("last_longhand_property_id", title_casify(longhand_property_ids.last()));
  62. generator.append(R"~~~(
  63. };
  64. PropertyID property_id_from_camel_case_string(StringView);
  65. PropertyID property_id_from_string(StringView);
  66. const char* string_from_property_id(PropertyID);
  67. bool is_inherited_property(PropertyID);
  68. NonnullRefPtr<StyleValue> property_initial_value(PropertyID);
  69. bool property_accepts_value(PropertyID, StyleValue&);
  70. size_t property_maximum_value_count(PropertyID);
  71. bool property_affects_layout(PropertyID);
  72. constexpr PropertyID first_property_id = PropertyID::@first_property_id@;
  73. constexpr PropertyID last_property_id = PropertyID::@last_property_id@;
  74. constexpr PropertyID first_shorthand_property_id = PropertyID::@first_shorthand_property_id@;
  75. constexpr PropertyID last_shorthand_property_id = PropertyID::@last_shorthand_property_id@;
  76. constexpr PropertyID first_longhand_property_id = PropertyID::@first_longhand_property_id@;
  77. constexpr PropertyID last_longhand_property_id = PropertyID::@last_longhand_property_id@;
  78. enum class Quirk {
  79. // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
  80. HashlessHexColor,
  81. // https://quirks.spec.whatwg.org/#the-unitless-length-quirk
  82. UnitlessLength,
  83. };
  84. bool property_has_quirk(PropertyID, Quirk);
  85. } // namespace Web::CSS
  86. namespace AK {
  87. template<>
  88. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  89. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  90. };
  91. } // namespace AK
  92. )~~~");
  93. outln("{}", generator.as_string_view());
  94. return 0;
  95. }