Generate_CSS_PropertyID_cpp.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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()).release_value_but_fixme_should_propagate_errors();
  57. VERIFY(json.is_object());
  58. auto& properties = json.as_object();
  59. StringBuilder builder;
  60. SourceGenerator generator { builder };
  61. generator.append(R"~~~(
  62. #include <AK/Assertions.h>
  63. #include <LibWeb/CSS/Parser/Parser.h>
  64. #include <LibWeb/CSS/PropertyID.h>
  65. #include <LibWeb/CSS/StyleValue.h>
  66. namespace Web::CSS {
  67. PropertyID property_id_from_camel_case_string(StringView string)
  68. {
  69. )~~~");
  70. properties.for_each_member([&](auto& name, auto& value) {
  71. VERIFY(value.is_object());
  72. auto member_generator = generator.fork();
  73. member_generator.set("name", name);
  74. member_generator.set("name:titlecase", title_casify(name));
  75. member_generator.set("name:camelcase", camel_casify(name));
  76. member_generator.append(R"~~~(
  77. if (string.equals_ignoring_case("@name:camelcase@"sv))
  78. return PropertyID::@name:titlecase@;
  79. )~~~");
  80. });
  81. generator.append(R"~~~(
  82. return PropertyID::Invalid;
  83. }
  84. PropertyID property_id_from_string(StringView string)
  85. {
  86. )~~~");
  87. properties.for_each_member([&](auto& name, auto& value) {
  88. VERIFY(value.is_object());
  89. auto member_generator = generator.fork();
  90. member_generator.set("name", name);
  91. member_generator.set("name:titlecase", title_casify(name));
  92. member_generator.append(R"~~~(
  93. if (string.equals_ignoring_case("@name@"))
  94. return PropertyID::@name:titlecase@;
  95. )~~~");
  96. });
  97. generator.append(R"~~~(
  98. return PropertyID::Invalid;
  99. }
  100. const char* string_from_property_id(PropertyID property_id) {
  101. switch (property_id) {
  102. )~~~");
  103. properties.for_each_member([&](auto& name, auto& value) {
  104. VERIFY(value.is_object());
  105. auto member_generator = generator.fork();
  106. member_generator.set("name", name);
  107. member_generator.set("name:titlecase", title_casify(name));
  108. member_generator.append(R"~~~(
  109. case PropertyID::@name:titlecase@:
  110. return "@name@";
  111. )~~~");
  112. });
  113. generator.append(R"~~~(
  114. default:
  115. return "(invalid CSS::PropertyID)";
  116. }
  117. }
  118. bool is_inherited_property(PropertyID property_id)
  119. {
  120. switch (property_id) {
  121. )~~~");
  122. properties.for_each_member([&](auto& name, auto& value) {
  123. VERIFY(value.is_object());
  124. bool inherited = false;
  125. if (value.as_object().has("inherited")) {
  126. auto& inherited_value = value.as_object().get("inherited");
  127. VERIFY(inherited_value.is_bool());
  128. inherited = inherited_value.as_bool();
  129. }
  130. if (inherited) {
  131. auto member_generator = generator.fork();
  132. member_generator.set("name:titlecase", title_casify(name));
  133. member_generator.append(R"~~~(
  134. case PropertyID::@name:titlecase@:
  135. return true;
  136. )~~~");
  137. }
  138. });
  139. generator.append(R"~~~(
  140. default:
  141. return false;
  142. }
  143. }
  144. NonnullRefPtr<StyleValue> property_initial_value(PropertyID property_id)
  145. {
  146. static HashMap<PropertyID, NonnullRefPtr<StyleValue>> initial_values;
  147. if (initial_values.is_empty()) {
  148. ParsingContext parsing_context;
  149. )~~~");
  150. // NOTE: Parsing a shorthand property requires that its longhands are already available here.
  151. // So, we do this in two passes: First longhands, then shorthands.
  152. // Probably we should build a dependency graph and then handle them in order, but this
  153. // works for now! :^)
  154. auto output_initial_value_code = [&](auto& name, auto& object) {
  155. if (!object.has("initial")) {
  156. dbgln("No initial value specified for property '{}'", name);
  157. VERIFY_NOT_REACHED();
  158. }
  159. auto& initial_value = object.get("initial");
  160. VERIFY(initial_value.is_string());
  161. auto initial_value_string = initial_value.as_string();
  162. auto member_generator = generator.fork();
  163. member_generator.set("name:titlecase", title_casify(name));
  164. member_generator.set("initial_value_string", initial_value_string);
  165. member_generator.append(R"~~~(
  166. {
  167. auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@);
  168. VERIFY(!parsed_value.is_null());
  169. initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull());
  170. }
  171. )~~~");
  172. };
  173. properties.for_each_member([&](auto& name, auto& value) {
  174. VERIFY(value.is_object());
  175. if (value.as_object().has("longhands"))
  176. return;
  177. output_initial_value_code(name, value.as_object());
  178. });
  179. properties.for_each_member([&](auto& name, auto& value) {
  180. VERIFY(value.is_object());
  181. if (!value.as_object().has("longhands"))
  182. return;
  183. output_initial_value_code(name, value.as_object());
  184. });
  185. generator.append(R"~~~(
  186. }
  187. return *initial_values.find(property_id)->value;
  188. }
  189. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  190. {
  191. switch (property_id) {
  192. )~~~");
  193. properties.for_each_member([&](auto& name, auto& value) {
  194. VERIFY(value.is_object());
  195. if (value.as_object().has("quirks")) {
  196. auto& quirks_value = value.as_object().get("quirks");
  197. VERIFY(quirks_value.is_array());
  198. auto& quirks = quirks_value.as_array();
  199. if (!quirks.is_empty()) {
  200. auto property_generator = generator.fork();
  201. property_generator.set("name:titlecase", title_casify(name));
  202. property_generator.append(R"~~~(
  203. case PropertyID::@name:titlecase@: {
  204. switch (quirk) {
  205. )~~~");
  206. for (auto& quirk : quirks.values()) {
  207. VERIFY(quirk.is_string());
  208. auto quirk_generator = property_generator.fork();
  209. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  210. quirk_generator.append(R"~~~(
  211. case Quirk::@quirk:titlecase@:
  212. return true;
  213. )~~~");
  214. }
  215. property_generator.append(R"~~~(
  216. default:
  217. return false;
  218. }
  219. }
  220. )~~~");
  221. }
  222. }
  223. });
  224. generator.append(R"~~~(
  225. default:
  226. return false;
  227. }
  228. }
  229. bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
  230. {
  231. if (style_value.is_builtin())
  232. return true;
  233. switch (property_id) {
  234. )~~~");
  235. properties.for_each_member([&](auto& name, auto& value) {
  236. VERIFY(value.is_object());
  237. auto& object = value.as_object();
  238. bool has_valid_types = object.has("valid-types");
  239. auto has_valid_identifiers = object.has("valid-identifiers");
  240. if (has_valid_types || has_valid_identifiers) {
  241. auto property_generator = generator.fork();
  242. property_generator.set("name:titlecase", title_casify(name));
  243. property_generator.append(R"~~~(
  244. case PropertyID::@name:titlecase@: {
  245. )~~~");
  246. if (has_valid_types) {
  247. auto valid_types_value = object.get("valid-types");
  248. VERIFY(valid_types_value.is_array());
  249. auto valid_types = valid_types_value.as_array();
  250. if (!valid_types.is_empty()) {
  251. for (auto& type : valid_types.values()) {
  252. VERIFY(type.is_string());
  253. auto type_parts = type.as_string().split_view(' ');
  254. auto type_name = type_parts.first();
  255. auto type_args = type_parts.size() > 1 ? type_parts[1] : ""sv;
  256. if (type_name == "angle") {
  257. property_generator.append(R"~~~(
  258. if (style_value.is_angle()
  259. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Angle))
  260. return true;
  261. )~~~");
  262. } else if (type_name == "color") {
  263. property_generator.append(R"~~~(
  264. if (style_value.has_color())
  265. return true;
  266. )~~~");
  267. } else if (type_name == "frequency") {
  268. property_generator.append(R"~~~(
  269. if (style_value.is_frequency()
  270. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Frequency))
  271. return true;
  272. )~~~");
  273. } else if (type_name == "image") {
  274. property_generator.append(R"~~~(
  275. if (style_value.is_image())
  276. return true;
  277. )~~~");
  278. } else if (type_name == "length") {
  279. property_generator.append(R"~~~(
  280. if (style_value.has_length()
  281. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Length))
  282. return true;
  283. )~~~");
  284. } else if (type_name == "number" || type_name == "integer") {
  285. auto test_generator = property_generator.fork();
  286. test_generator.set("numbertype", type_name);
  287. StringView min_value;
  288. StringView max_value;
  289. if (!type_args.is_empty()) {
  290. VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
  291. auto comma_index = type_args.find(',').value();
  292. min_value = type_args.substring_view(1, comma_index - 1);
  293. max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
  294. }
  295. test_generator.append(R"~~~(
  296. if ((style_value.has_@numbertype@())~~~");
  297. if (!min_value.is_empty()) {
  298. test_generator.set("minvalue", min_value);
  299. test_generator.append(" && (style_value.to_@numbertype@() >= @minvalue@)");
  300. }
  301. if (!max_value.is_empty()) {
  302. test_generator.set("maxvalue", max_value);
  303. test_generator.append(" && (style_value.to_@numbertype@() <= @maxvalue@)");
  304. }
  305. test_generator.append(R"~~~()
  306. || (style_value.is_calculated() && (style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Integer)~~~");
  307. if (type_name == "number")
  308. test_generator.append(R"~~~(|| style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Number)~~~");
  309. test_generator.append(R"~~~()))
  310. return true;
  311. )~~~");
  312. } else if (type_name == "percentage") {
  313. property_generator.append(R"~~~(
  314. if (style_value.is_percentage()
  315. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Percentage))
  316. return true;
  317. )~~~");
  318. } else if (type_name == "resolution") {
  319. property_generator.append(R"~~~(
  320. if (style_value.is_resolution())
  321. return true;
  322. )~~~");
  323. } else if (type_name == "string") {
  324. property_generator.append(R"~~~(
  325. if (style_value.is_string())
  326. return true;
  327. )~~~");
  328. } else if (type_name == "time") {
  329. property_generator.append(R"~~~(
  330. if (style_value.is_time()
  331. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Time))
  332. return true;
  333. )~~~");
  334. } else if (type_name == "url") {
  335. // FIXME: Handle urls!
  336. } else {
  337. warnln("Unrecognized valid-type name: '{}'", type_name);
  338. VERIFY_NOT_REACHED();
  339. }
  340. }
  341. }
  342. }
  343. if (has_valid_identifiers) {
  344. auto valid_identifiers_value = object.get("valid-identifiers");
  345. VERIFY(valid_identifiers_value.is_array());
  346. auto valid_identifiers = valid_identifiers_value.as_array();
  347. if (!valid_identifiers.is_empty()) {
  348. property_generator.append(R"~~~(
  349. switch (style_value.to_identifier()) {
  350. )~~~");
  351. for (auto& identifier : valid_identifiers.values()) {
  352. VERIFY(identifier.is_string());
  353. auto identifier_generator = generator.fork();
  354. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  355. identifier_generator.append(R"~~~(
  356. case ValueID::@identifier:titlecase@:
  357. )~~~");
  358. }
  359. property_generator.append(R"~~~(
  360. return true;
  361. default:
  362. break;
  363. }
  364. )~~~");
  365. }
  366. }
  367. generator.append(R"~~~(
  368. return false;
  369. }
  370. )~~~");
  371. }
  372. });
  373. generator.append(R"~~~(
  374. default:
  375. return true;
  376. }
  377. }
  378. size_t property_maximum_value_count(PropertyID property_id)
  379. {
  380. switch (property_id) {
  381. )~~~");
  382. properties.for_each_member([&](auto& name, auto& value) {
  383. VERIFY(value.is_object());
  384. if (value.as_object().has("max-values")) {
  385. auto max_values = value.as_object().get("max-values");
  386. VERIFY(max_values.is_number() && !max_values.is_double());
  387. auto property_generator = generator.fork();
  388. property_generator.set("name:titlecase", title_casify(name));
  389. property_generator.set("max_values", max_values.to_string());
  390. property_generator.append(R"~~~(
  391. case PropertyID::@name:titlecase@:
  392. return @max_values@;
  393. )~~~");
  394. }
  395. });
  396. generator.append(R"~~~(
  397. default:
  398. return 1;
  399. }
  400. }
  401. } // namespace Web::CSS
  402. )~~~");
  403. outln("{}", generator.as_string_view());
  404. }