Generate_CSS_PropertyID_cpp.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/SourceGenerator.h>
  10. #include <AK/StringBuilder.h>
  11. #include <LibCore/File.h>
  12. #include <ctype.h>
  13. static String title_casify(const String& dashy_name)
  14. {
  15. auto parts = dashy_name.split('-');
  16. StringBuilder builder;
  17. for (auto& part : parts) {
  18. if (part.is_empty())
  19. continue;
  20. builder.append(toupper(part[0]));
  21. if (part.length() == 1)
  22. continue;
  23. builder.append(part.substring_view(1, part.length() - 1));
  24. }
  25. return builder.to_string();
  26. }
  27. static String camel_casify(StringView dashy_name)
  28. {
  29. auto parts = dashy_name.split_view('-');
  30. StringBuilder builder;
  31. bool first = true;
  32. for (auto& part : parts) {
  33. if (part.is_empty())
  34. continue;
  35. char ch = part[0];
  36. if (!first)
  37. ch = toupper(ch);
  38. else
  39. first = false;
  40. builder.append(ch);
  41. if (part.length() == 1)
  42. continue;
  43. builder.append(part.substring_view(1, part.length() - 1));
  44. }
  45. return builder.to_string();
  46. }
  47. int main(int argc, char** argv)
  48. {
  49. if (argc != 2) {
  50. warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
  51. return 1;
  52. }
  53. auto file = Core::File::construct(argv[1]);
  54. if (!file->open(Core::OpenMode::ReadOnly))
  55. return 1;
  56. auto json = JsonValue::from_string(file->read_all());
  57. VERIFY(json.has_value());
  58. VERIFY(json.value().is_object());
  59. auto& properties = json.value().as_object();
  60. StringBuilder builder;
  61. SourceGenerator generator { builder };
  62. generator.append(R"~~~(
  63. #include <AK/Assertions.h>
  64. #include <LibWeb/CSS/Parser/Parser.h>
  65. #include <LibWeb/CSS/PropertyID.h>
  66. #include <LibWeb/CSS/StyleValue.h>
  67. namespace Web::CSS {
  68. PropertyID property_id_from_camel_case_string(StringView string)
  69. {
  70. )~~~");
  71. properties.for_each_member([&](auto& name, auto& value) {
  72. VERIFY(value.is_object());
  73. auto member_generator = generator.fork();
  74. member_generator.set("name", name);
  75. member_generator.set("name:titlecase", title_casify(name));
  76. member_generator.set("name:camelcase", camel_casify(name));
  77. member_generator.append(R"~~~(
  78. if (string.equals_ignoring_case("@name:camelcase@"sv))
  79. return PropertyID::@name:titlecase@;
  80. )~~~");
  81. });
  82. generator.append(R"~~~(
  83. return PropertyID::Invalid;
  84. }
  85. PropertyID property_id_from_string(const StringView& string)
  86. {
  87. )~~~");
  88. properties.for_each_member([&](auto& name, auto& value) {
  89. VERIFY(value.is_object());
  90. auto member_generator = generator.fork();
  91. member_generator.set("name", name);
  92. member_generator.set("name:titlecase", title_casify(name));
  93. member_generator.append(R"~~~(
  94. if (string.equals_ignoring_case("@name@"))
  95. return PropertyID::@name:titlecase@;
  96. )~~~");
  97. });
  98. generator.append(R"~~~(
  99. return PropertyID::Invalid;
  100. }
  101. const char* string_from_property_id(PropertyID property_id) {
  102. switch (property_id) {
  103. )~~~");
  104. properties.for_each_member([&](auto& name, auto& value) {
  105. VERIFY(value.is_object());
  106. auto member_generator = generator.fork();
  107. member_generator.set("name", name);
  108. member_generator.set("name:titlecase", title_casify(name));
  109. member_generator.append(R"~~~(
  110. case PropertyID::@name:titlecase@:
  111. return "@name@";
  112. )~~~");
  113. });
  114. generator.append(R"~~~(
  115. default:
  116. return "(invalid CSS::PropertyID)";
  117. }
  118. }
  119. bool is_inherited_property(PropertyID property_id)
  120. {
  121. switch (property_id) {
  122. )~~~");
  123. properties.for_each_member([&](auto& name, auto& value) {
  124. VERIFY(value.is_object());
  125. bool inherited = false;
  126. if (value.as_object().has("inherited")) {
  127. auto& inherited_value = value.as_object().get("inherited");
  128. VERIFY(inherited_value.is_bool());
  129. inherited = inherited_value.as_bool();
  130. }
  131. if (inherited) {
  132. auto member_generator = generator.fork();
  133. member_generator.set("name:titlecase", title_casify(name));
  134. member_generator.append(R"~~~(
  135. case PropertyID::@name:titlecase@:
  136. return true;
  137. )~~~");
  138. }
  139. });
  140. generator.append(R"~~~(
  141. default:
  142. return false;
  143. }
  144. }
  145. NonnullRefPtr<StyleValue> property_initial_value(PropertyID property_id)
  146. {
  147. static HashMap<PropertyID, NonnullRefPtr<StyleValue>> initial_values;
  148. if (initial_values.is_empty()) {
  149. ParsingContext parsing_context;
  150. )~~~");
  151. // NOTE: Parsing a shorthand property requires that its longhands are already available here.
  152. // So, we do this in two passes: First longhands, then shorthands.
  153. // Probably we should build a dependency graph and then handle them in order, but this
  154. // works for now! :^)
  155. auto output_initial_value_code = [&](auto& name, auto& object) {
  156. if (!object.has("initial")) {
  157. dbgln("No initial value specified for property '{}'", name);
  158. VERIFY_NOT_REACHED();
  159. }
  160. auto& initial_value = object.get("initial");
  161. VERIFY(initial_value.is_string());
  162. auto initial_value_string = initial_value.as_string();
  163. auto member_generator = generator.fork();
  164. member_generator.set("name:titlecase", title_casify(name));
  165. member_generator.set("initial_value_string", initial_value_string);
  166. member_generator.append(R"~~~(
  167. {
  168. auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@);
  169. VERIFY(!parsed_value.is_null());
  170. initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull());
  171. }
  172. )~~~");
  173. };
  174. properties.for_each_member([&](auto& name, auto& value) {
  175. VERIFY(value.is_object());
  176. if (value.as_object().has("longhands"))
  177. return;
  178. output_initial_value_code(name, value.as_object());
  179. });
  180. properties.for_each_member([&](auto& name, auto& value) {
  181. VERIFY(value.is_object());
  182. if (!value.as_object().has("longhands"))
  183. return;
  184. output_initial_value_code(name, value.as_object());
  185. });
  186. generator.append(R"~~~(
  187. }
  188. return *initial_values.find(property_id)->value;
  189. }
  190. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  191. {
  192. switch (property_id) {
  193. )~~~");
  194. properties.for_each_member([&](auto& name, auto& value) {
  195. VERIFY(value.is_object());
  196. if (value.as_object().has("quirks")) {
  197. auto& quirks_value = value.as_object().get("quirks");
  198. VERIFY(quirks_value.is_array());
  199. auto& quirks = quirks_value.as_array();
  200. if (!quirks.is_empty()) {
  201. auto property_generator = generator.fork();
  202. property_generator.set("name:titlecase", title_casify(name));
  203. property_generator.append(R"~~~(
  204. case PropertyID::@name:titlecase@: {
  205. switch (quirk) {
  206. )~~~");
  207. for (auto& quirk : quirks.values()) {
  208. VERIFY(quirk.is_string());
  209. auto quirk_generator = property_generator.fork();
  210. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  211. quirk_generator.append(R"~~~(
  212. case Quirk::@quirk:titlecase@:
  213. return true;
  214. )~~~");
  215. }
  216. property_generator.append(R"~~~(
  217. default:
  218. return false;
  219. }
  220. }
  221. )~~~");
  222. }
  223. }
  224. });
  225. generator.append(R"~~~(
  226. default:
  227. return false;
  228. }
  229. }
  230. bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
  231. {
  232. if (style_value.is_builtin() || style_value.is_custom_property())
  233. return true;
  234. switch (property_id) {
  235. )~~~");
  236. properties.for_each_member([&](auto& name, auto& value) {
  237. VERIFY(value.is_object());
  238. auto& object = value.as_object();
  239. bool has_valid_types = object.has("valid-types");
  240. auto has_valid_identifiers = object.has("valid-identifiers");
  241. if (has_valid_types || has_valid_identifiers) {
  242. auto property_generator = generator.fork();
  243. property_generator.set("name:titlecase", title_casify(name));
  244. property_generator.append(R"~~~(
  245. case PropertyID::@name:titlecase@: {
  246. )~~~");
  247. if (has_valid_types) {
  248. auto valid_types_value = object.get("valid-types");
  249. VERIFY(valid_types_value.is_array());
  250. auto valid_types = valid_types_value.as_array();
  251. if (!valid_types.is_empty()) {
  252. for (auto& type : valid_types.values()) {
  253. VERIFY(type.is_string());
  254. auto type_parts = type.as_string().split_view(' ');
  255. auto type_name = type_parts.first();
  256. auto type_args = type_parts.size() > 1 ? type_parts[1] : ""sv;
  257. if (type_name == "color") {
  258. property_generator.append(R"~~~(
  259. if (style_value.has_color())
  260. return true;
  261. )~~~");
  262. } else if (type_name == "image") {
  263. property_generator.append(R"~~~(
  264. if (style_value.is_image())
  265. return true;
  266. )~~~");
  267. } else if (type_name == "length") {
  268. property_generator.append(R"~~~(
  269. if ((style_value.has_length() && !style_value.to_length().is_percentage()) || style_value.is_calculated())
  270. return true;
  271. )~~~");
  272. } else if (type_name == "percentage") {
  273. property_generator.append(R"~~~(
  274. if ((style_value.has_length() && style_value.to_length().is_percentage()) || style_value.is_calculated())
  275. return true;
  276. )~~~");
  277. } else if (type_name == "number" || type_name == "integer") {
  278. auto test_generator = property_generator.fork();
  279. test_generator.set("numbertype", type_name);
  280. StringView min_value;
  281. StringView max_value;
  282. if (!type_args.is_empty()) {
  283. VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
  284. auto comma_index = type_args.find(',').value();
  285. min_value = type_args.substring_view(1, comma_index - 1);
  286. max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
  287. }
  288. test_generator.append(R"~~~(
  289. if (style_value.has_@numbertype@())~~~");
  290. if (!min_value.is_empty()) {
  291. test_generator.set("minvalue", min_value);
  292. test_generator.append(" && (style_value.to_@numbertype@() >= @minvalue@)");
  293. }
  294. if (!max_value.is_empty()) {
  295. test_generator.set("maxvalue", max_value);
  296. test_generator.append(" && (style_value.to_@numbertype@() <= @maxvalue@)");
  297. }
  298. test_generator.append(R"~~~()
  299. return true;
  300. )~~~");
  301. } else if (type_name == "string") {
  302. property_generator.append(R"~~~(
  303. if (style_value.is_string())
  304. return true;
  305. )~~~");
  306. } else if (type_name == "url") {
  307. // FIXME: Handle urls!
  308. } else {
  309. warnln("Unrecognized valid-type name: '{}'", type_name);
  310. VERIFY_NOT_REACHED();
  311. }
  312. }
  313. }
  314. }
  315. if (has_valid_identifiers) {
  316. auto valid_identifiers_value = object.get("valid-identifiers");
  317. VERIFY(valid_identifiers_value.is_array());
  318. auto valid_identifiers = valid_identifiers_value.as_array();
  319. if (!valid_identifiers.is_empty()) {
  320. property_generator.append(R"~~~(
  321. switch (style_value.to_identifier()) {
  322. )~~~");
  323. for (auto& identifier : valid_identifiers.values()) {
  324. VERIFY(identifier.is_string());
  325. auto identifier_generator = generator.fork();
  326. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  327. identifier_generator.append(R"~~~(
  328. case ValueID::@identifier:titlecase@:
  329. )~~~");
  330. }
  331. property_generator.append(R"~~~(
  332. return true;
  333. default:
  334. break;
  335. }
  336. )~~~");
  337. }
  338. }
  339. generator.append(R"~~~(
  340. return false;
  341. }
  342. )~~~");
  343. }
  344. });
  345. generator.append(R"~~~(
  346. default:
  347. return true;
  348. }
  349. }
  350. size_t property_maximum_value_count(PropertyID property_id)
  351. {
  352. switch (property_id) {
  353. )~~~");
  354. properties.for_each_member([&](auto& name, auto& value) {
  355. VERIFY(value.is_object());
  356. if (value.as_object().has("max-values")) {
  357. auto max_values = value.as_object().get("max-values");
  358. VERIFY(max_values.is_number() && !max_values.is_double());
  359. auto property_generator = generator.fork();
  360. property_generator.set("name:titlecase", title_casify(name));
  361. property_generator.set("max_values", max_values.to_string());
  362. property_generator.append(R"~~~(
  363. case PropertyID::@name:titlecase@:
  364. return @max_values@;
  365. )~~~");
  366. }
  367. });
  368. generator.append(R"~~~(
  369. default:
  370. return 1;
  371. }
  372. }
  373. } // namespace Web::CSS
  374. )~~~");
  375. outln("{}", generator.as_string_view());
  376. }