Generate_CSS_PropertyID_cpp.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
  217. {
  218. if (style_value.is_builtin() || style_value.is_custom_property())
  219. return true;
  220. switch (property_id) {
  221. )~~~");
  222. properties.for_each_member([&](auto& name, auto& value) {
  223. VERIFY(value.is_object());
  224. auto& object = value.as_object();
  225. bool has_valid_types = object.has("valid-types");
  226. auto has_valid_identifiers = object.has("valid-identifiers");
  227. if (has_valid_types || has_valid_identifiers) {
  228. auto property_generator = generator.fork();
  229. property_generator.set("name:titlecase", title_casify(name));
  230. property_generator.append(R"~~~(
  231. case PropertyID::@name:titlecase@: {
  232. )~~~");
  233. if (has_valid_types) {
  234. auto valid_types_value = object.get("valid-types");
  235. VERIFY(valid_types_value.is_array());
  236. auto valid_types = valid_types_value.as_array();
  237. if (!valid_types.is_empty()) {
  238. for (auto& type : valid_types.values()) {
  239. VERIFY(type.is_string());
  240. auto type_name = type.as_string();
  241. if (type_name == "color") {
  242. property_generator.append(R"~~~(
  243. if (style_value.is_color())
  244. return true;
  245. )~~~");
  246. } else if (type_name == "image") {
  247. property_generator.append(R"~~~(
  248. if (style_value.is_image())
  249. return true;
  250. )~~~");
  251. } else if (type_name == "length" || type_name == "percentage") {
  252. // FIXME: Handle lengths and percentages separately
  253. property_generator.append(R"~~~(
  254. if (style_value.is_length() || style_value.is_calculated())
  255. return true;
  256. )~~~");
  257. } else if (type_name == "number" || type_name == "integer") {
  258. // FIXME: Handle integers separately
  259. property_generator.append(R"~~~(
  260. if (style_value.is_numeric())
  261. return true;
  262. )~~~");
  263. } else if (type_name == "string") {
  264. property_generator.append(R"~~~(
  265. if (style_value.is_string())
  266. return true;
  267. )~~~");
  268. } else if (type_name == "url") {
  269. // FIXME: Handle urls!
  270. } else {
  271. warnln("Unrecognized valid-type name: '{}'", type_name);
  272. VERIFY_NOT_REACHED();
  273. }
  274. }
  275. }
  276. }
  277. if (has_valid_identifiers) {
  278. auto valid_identifiers_value = object.get("valid-identifiers");
  279. VERIFY(valid_identifiers_value.is_array());
  280. auto valid_identifiers = valid_identifiers_value.as_array();
  281. if (!valid_identifiers.is_empty()) {
  282. property_generator.append(R"~~~(
  283. switch (style_value.to_identifier()) {
  284. )~~~");
  285. for (auto& identifier : valid_identifiers.values()) {
  286. VERIFY(identifier.is_string());
  287. auto identifier_generator = generator.fork();
  288. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  289. identifier_generator.append(R"~~~(
  290. case ValueID::@identifier:titlecase@:
  291. )~~~");
  292. }
  293. property_generator.append(R"~~~(
  294. return true;
  295. default:
  296. break;
  297. }
  298. )~~~");
  299. }
  300. }
  301. generator.append(R"~~~(
  302. return false;
  303. }
  304. )~~~");
  305. }
  306. });
  307. generator.append(R"~~~(
  308. default:
  309. return true;
  310. }
  311. }
  312. size_t property_maximum_value_count(PropertyID property_id)
  313. {
  314. switch (property_id) {
  315. )~~~");
  316. properties.for_each_member([&](auto& name, auto& value) {
  317. VERIFY(value.is_object());
  318. if (value.as_object().has("max-values")) {
  319. auto max_values = value.as_object().get("max-values");
  320. VERIFY(max_values.is_number() && !max_values.is_double());
  321. auto property_generator = generator.fork();
  322. property_generator.set("name:titlecase", title_casify(name));
  323. property_generator.set("max_values", max_values.to_string());
  324. property_generator.append(R"~~~(
  325. case PropertyID::@name:titlecase@:
  326. return @max_values@;
  327. )~~~");
  328. }
  329. });
  330. generator.append(R"~~~(
  331. default:
  332. return 1;
  333. }
  334. }
  335. } // namespace Web::CSS
  336. )~~~");
  337. outln("{}", generator.as_string_view());
  338. }