Generate_CSS_PropertyID_cpp.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. RefPtr<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. auto& initial_value = object.get("initial");
  158. VERIFY(initial_value.is_string());
  159. auto initial_value_string = initial_value.as_string();
  160. auto member_generator = generator.fork();
  161. member_generator.set("name:titlecase", title_casify(name));
  162. member_generator.set("initial_value_string", initial_value_string);
  163. member_generator.append(R"~~~(
  164. if (auto parsed_value = Parser(parsing_context, "@initial_value_string@").parse_as_css_value(PropertyID::@name:titlecase@))
  165. initial_values.set(PropertyID::@name:titlecase@, parsed_value.release_nonnull());
  166. )~~~");
  167. }
  168. };
  169. properties.for_each_member([&](auto& name, auto& value) {
  170. VERIFY(value.is_object());
  171. if (value.as_object().has("longhands"))
  172. return;
  173. output_initial_value_code(name, value.as_object());
  174. });
  175. properties.for_each_member([&](auto& name, auto& value) {
  176. VERIFY(value.is_object());
  177. if (!value.as_object().has("longhands"))
  178. return;
  179. output_initial_value_code(name, value.as_object());
  180. });
  181. generator.append(R"~~~(
  182. }
  183. auto it = initial_values.find(property_id);
  184. if (it == initial_values.end())
  185. return nullptr;
  186. return it->value;
  187. }
  188. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  189. {
  190. switch (property_id) {
  191. )~~~");
  192. properties.for_each_member([&](auto& name, auto& value) {
  193. VERIFY(value.is_object());
  194. if (value.as_object().has("quirks")) {
  195. auto& quirks_value = value.as_object().get("quirks");
  196. VERIFY(quirks_value.is_array());
  197. auto& quirks = quirks_value.as_array();
  198. if (!quirks.is_empty()) {
  199. auto property_generator = generator.fork();
  200. property_generator.set("name:titlecase", title_casify(name));
  201. property_generator.append(R"~~~(
  202. case PropertyID::@name:titlecase@: {
  203. switch (quirk) {
  204. )~~~");
  205. for (auto& quirk : quirks.values()) {
  206. VERIFY(quirk.is_string());
  207. auto quirk_generator = property_generator.fork();
  208. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  209. quirk_generator.append(R"~~~(
  210. case Quirk::@quirk:titlecase@:
  211. return true;
  212. )~~~");
  213. }
  214. property_generator.append(R"~~~(
  215. default:
  216. return false;
  217. }
  218. }
  219. )~~~");
  220. }
  221. }
  222. });
  223. generator.append(R"~~~(
  224. default:
  225. return false;
  226. }
  227. }
  228. bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
  229. {
  230. if (style_value.is_builtin() || style_value.is_custom_property())
  231. return true;
  232. switch (property_id) {
  233. )~~~");
  234. properties.for_each_member([&](auto& name, auto& value) {
  235. VERIFY(value.is_object());
  236. auto& object = value.as_object();
  237. bool has_valid_types = object.has("valid-types");
  238. auto has_valid_identifiers = object.has("valid-identifiers");
  239. if (has_valid_types || has_valid_identifiers) {
  240. auto property_generator = generator.fork();
  241. property_generator.set("name:titlecase", title_casify(name));
  242. property_generator.append(R"~~~(
  243. case PropertyID::@name:titlecase@: {
  244. )~~~");
  245. if (has_valid_types) {
  246. auto valid_types_value = object.get("valid-types");
  247. VERIFY(valid_types_value.is_array());
  248. auto valid_types = valid_types_value.as_array();
  249. if (!valid_types.is_empty()) {
  250. for (auto& type : valid_types.values()) {
  251. VERIFY(type.is_string());
  252. auto type_parts = type.as_string().split_view(' ');
  253. auto type_name = type_parts.first();
  254. auto type_args = type_parts.size() > 1 ? type_parts[1] : ""sv;
  255. if (type_name == "color") {
  256. property_generator.append(R"~~~(
  257. if (style_value.has_color())
  258. return true;
  259. )~~~");
  260. } else if (type_name == "image") {
  261. property_generator.append(R"~~~(
  262. if (style_value.is_image())
  263. return true;
  264. )~~~");
  265. } else if (type_name == "length") {
  266. property_generator.append(R"~~~(
  267. if ((style_value.has_length() && !style_value.to_length().is_percentage()) || style_value.is_calculated())
  268. return true;
  269. )~~~");
  270. } else if (type_name == "percentage") {
  271. property_generator.append(R"~~~(
  272. if ((style_value.has_length() && style_value.to_length().is_percentage()) || style_value.is_calculated())
  273. return true;
  274. )~~~");
  275. } else if (type_name == "number" || type_name == "integer") {
  276. auto test_generator = property_generator.fork();
  277. test_generator.set("numbertype", type_name);
  278. StringView min_value;
  279. StringView max_value;
  280. if (!type_args.is_empty()) {
  281. VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
  282. auto comma_index = type_args.find(',').value();
  283. min_value = type_args.substring_view(1, comma_index - 1);
  284. max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
  285. }
  286. test_generator.append(R"~~~(
  287. if (style_value.has_@numbertype@())~~~");
  288. if (!min_value.is_empty()) {
  289. test_generator.set("minvalue", min_value);
  290. test_generator.append(" && (style_value.to_@numbertype@() >= @minvalue@)");
  291. }
  292. if (!max_value.is_empty()) {
  293. test_generator.set("maxvalue", max_value);
  294. test_generator.append(" && (style_value.to_@numbertype@() <= @maxvalue@)");
  295. }
  296. test_generator.append(R"~~~()
  297. return true;
  298. )~~~");
  299. } else if (type_name == "string") {
  300. property_generator.append(R"~~~(
  301. if (style_value.is_string())
  302. return true;
  303. )~~~");
  304. } else if (type_name == "url") {
  305. // FIXME: Handle urls!
  306. } else {
  307. warnln("Unrecognized valid-type name: '{}'", type_name);
  308. VERIFY_NOT_REACHED();
  309. }
  310. }
  311. }
  312. }
  313. if (has_valid_identifiers) {
  314. auto valid_identifiers_value = object.get("valid-identifiers");
  315. VERIFY(valid_identifiers_value.is_array());
  316. auto valid_identifiers = valid_identifiers_value.as_array();
  317. if (!valid_identifiers.is_empty()) {
  318. property_generator.append(R"~~~(
  319. switch (style_value.to_identifier()) {
  320. )~~~");
  321. for (auto& identifier : valid_identifiers.values()) {
  322. VERIFY(identifier.is_string());
  323. auto identifier_generator = generator.fork();
  324. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  325. identifier_generator.append(R"~~~(
  326. case ValueID::@identifier:titlecase@:
  327. )~~~");
  328. }
  329. property_generator.append(R"~~~(
  330. return true;
  331. default:
  332. break;
  333. }
  334. )~~~");
  335. }
  336. }
  337. generator.append(R"~~~(
  338. return false;
  339. }
  340. )~~~");
  341. }
  342. });
  343. generator.append(R"~~~(
  344. default:
  345. return true;
  346. }
  347. }
  348. size_t property_maximum_value_count(PropertyID property_id)
  349. {
  350. switch (property_id) {
  351. )~~~");
  352. properties.for_each_member([&](auto& name, auto& value) {
  353. VERIFY(value.is_object());
  354. if (value.as_object().has("max-values")) {
  355. auto max_values = value.as_object().get("max-values");
  356. VERIFY(max_values.is_number() && !max_values.is_double());
  357. auto property_generator = generator.fork();
  358. property_generator.set("name:titlecase", title_casify(name));
  359. property_generator.set("max_values", max_values.to_string());
  360. property_generator.append(R"~~~(
  361. case PropertyID::@name:titlecase@:
  362. return @max_values@;
  363. )~~~");
  364. }
  365. });
  366. generator.append(R"~~~(
  367. default:
  368. return 1;
  369. }
  370. }
  371. } // namespace Web::CSS
  372. )~~~");
  373. outln("{}", generator.as_string_view());
  374. }