Generate_CSS_PropertyID_cpp.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. auto& properties = json.value().as_object();
  39. StringBuilder builder;
  40. SourceGenerator generator { builder };
  41. generator.append(R"~~~(
  42. #include <AK/Assertions.h>
  43. #include <LibWeb/CSS/Parser/Parser.h>
  44. #include <LibWeb/CSS/PropertyID.h>
  45. #include <LibWeb/CSS/StyleValue.h>
  46. namespace Web::CSS {
  47. PropertyID property_id_from_string(const StringView& string)
  48. {
  49. )~~~");
  50. properties.for_each_member([&](auto& name, auto& value) {
  51. VERIFY(value.is_object());
  52. auto member_generator = generator.fork();
  53. member_generator.set("name", name);
  54. member_generator.set("name:titlecase", title_casify(name));
  55. member_generator.append(R"~~~(
  56. if (string.equals_ignoring_case("@name@"))
  57. return PropertyID::@name:titlecase@;
  58. )~~~");
  59. });
  60. generator.append(R"~~~(
  61. return PropertyID::Invalid;
  62. }
  63. const char* string_from_property_id(PropertyID property_id) {
  64. switch (property_id) {
  65. )~~~");
  66. properties.for_each_member([&](auto& name, auto& value) {
  67. VERIFY(value.is_object());
  68. auto member_generator = generator.fork();
  69. member_generator.set("name", name);
  70. member_generator.set("name:titlecase", title_casify(name));
  71. member_generator.append(R"~~~(
  72. case PropertyID::@name:titlecase@:
  73. return "@name@";
  74. )~~~");
  75. });
  76. generator.append(R"~~~(
  77. default:
  78. return "(invalid CSS::PropertyID)";
  79. }
  80. }
  81. bool is_inherited_property(PropertyID property_id)
  82. {
  83. switch (property_id) {
  84. )~~~");
  85. properties.for_each_member([&](auto& name, auto& value) {
  86. VERIFY(value.is_object());
  87. bool inherited = false;
  88. if (value.as_object().has("inherited")) {
  89. auto& inherited_value = value.as_object().get("inherited");
  90. VERIFY(inherited_value.is_bool());
  91. inherited = inherited_value.as_bool();
  92. }
  93. if (inherited) {
  94. auto member_generator = generator.fork();
  95. member_generator.set("name:titlecase", title_casify(name));
  96. member_generator.append(R"~~~(
  97. case PropertyID::@name:titlecase@:
  98. return true;
  99. )~~~");
  100. }
  101. });
  102. generator.append(R"~~~(
  103. default:
  104. return false;
  105. }
  106. }
  107. bool is_pseudo_property(PropertyID property_id)
  108. {
  109. switch (property_id) {
  110. )~~~");
  111. properties.for_each_member([&](auto& name, auto& value) {
  112. VERIFY(value.is_object());
  113. bool pseudo = false;
  114. if (value.as_object().has("pseudo")) {
  115. auto& pseudo_value = value.as_object().get("pseudo");
  116. VERIFY(pseudo_value.is_bool());
  117. pseudo = pseudo_value.as_bool();
  118. }
  119. if (pseudo) {
  120. auto member_generator = generator.fork();
  121. member_generator.set("name:titlecase", title_casify(name));
  122. member_generator.append(R"~~~(
  123. case PropertyID::@name:titlecase@:
  124. return true;
  125. )~~~");
  126. }
  127. });
  128. generator.append(R"~~~(
  129. default:
  130. return false;
  131. }
  132. }
  133. RefPtr<StyleValue> property_initial_value(PropertyID property_id)
  134. {
  135. static HashMap<PropertyID, NonnullRefPtr<StyleValue>> initial_values;
  136. if (initial_values.is_empty()) {
  137. ParsingContext parsing_context;
  138. )~~~");
  139. // NOTE: Parsing a shorthand property requires that its longhands are already available here.
  140. // So, we do this in two passes: First longhands, then shorthands.
  141. // Probably we should build a dependency graph and then handle them in order, but this
  142. // works for now! :^)
  143. auto output_initial_value_code = [&](auto& name, auto& object) {
  144. if (object.has("initial")) {
  145. auto& initial_value = object.get("initial");
  146. VERIFY(initial_value.is_string());
  147. auto initial_value_string = initial_value.as_string();
  148. auto member_generator = generator.fork();
  149. member_generator.set("name:titlecase", title_casify(name));
  150. member_generator.set("initial_value_string", initial_value_string);
  151. member_generator.append(R"~~~(
  152. if (auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@))
  153. initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull());
  154. )~~~");
  155. }
  156. };
  157. properties.for_each_member([&](auto& name, auto& value) {
  158. VERIFY(value.is_object());
  159. if (value.as_object().has("longhands"))
  160. return;
  161. output_initial_value_code(name, value.as_object());
  162. });
  163. properties.for_each_member([&](auto& name, auto& value) {
  164. VERIFY(value.is_object());
  165. if (!value.as_object().has("longhands"))
  166. return;
  167. output_initial_value_code(name, value.as_object());
  168. });
  169. generator.append(R"~~~(
  170. }
  171. auto it = initial_values.find(property_id);
  172. if (it == initial_values.end())
  173. return nullptr;
  174. return it->value;
  175. }
  176. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  177. {
  178. switch (property_id) {
  179. )~~~");
  180. properties.for_each_member([&](auto& name, auto& value) {
  181. VERIFY(value.is_object());
  182. if (value.as_object().has("quirks")) {
  183. auto& quirks_value = value.as_object().get("quirks");
  184. VERIFY(quirks_value.is_array());
  185. auto& quirks = quirks_value.as_array();
  186. if (!quirks.is_empty()) {
  187. auto property_generator = generator.fork();
  188. property_generator.set("name:titlecase", title_casify(name));
  189. property_generator.append(R"~~~(
  190. case PropertyID::@name:titlecase@: {
  191. switch (quirk) {
  192. )~~~");
  193. for (auto& quirk : quirks.values()) {
  194. VERIFY(quirk.is_string());
  195. auto quirk_generator = property_generator.fork();
  196. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  197. quirk_generator.append(R"~~~(
  198. case Quirk::@quirk:titlecase@:
  199. return true;
  200. )~~~");
  201. }
  202. property_generator.append(R"~~~(
  203. default:
  204. return false;
  205. }
  206. }
  207. )~~~");
  208. }
  209. }
  210. });
  211. generator.append(R"~~~(
  212. default:
  213. return false;
  214. }
  215. }
  216. size_t property_maximum_value_count(PropertyID property_id)
  217. {
  218. switch (property_id) {
  219. )~~~");
  220. properties.for_each_member([&](auto& name, auto& value) {
  221. VERIFY(value.is_object());
  222. if (value.as_object().has("max-values")) {
  223. auto max_values = value.as_object().get("max-values");
  224. VERIFY(max_values.is_number() && !max_values.is_double());
  225. auto property_generator = generator.fork();
  226. property_generator.set("name:titlecase", title_casify(name));
  227. property_generator.set("max_values", max_values.to_string());
  228. property_generator.append(R"~~~(
  229. case PropertyID::@name:titlecase@:
  230. return @max_values@;
  231. )~~~");
  232. }
  233. });
  234. generator.append(R"~~~(
  235. default:
  236. return 1;
  237. }
  238. }
  239. } // namespace Web::CSS
  240. )~~~");
  241. outln("{}", generator.as_string_view());
  242. }