Generate_CSS_PropertyID_cpp.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 == "color") {
  257. property_generator.append(R"~~~(
  258. if (style_value.has_color())
  259. return true;
  260. )~~~");
  261. } else if (type_name == "image") {
  262. property_generator.append(R"~~~(
  263. if (style_value.is_image())
  264. return true;
  265. )~~~");
  266. } else if (type_name == "length") {
  267. property_generator.append(R"~~~(
  268. if (style_value.has_length()
  269. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Length))
  270. return true;
  271. )~~~");
  272. } else if (type_name == "percentage") {
  273. property_generator.append(R"~~~(
  274. if (style_value.is_percentage()
  275. || (style_value.is_calculated() && style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Percentage))
  276. return true;
  277. )~~~");
  278. } else if (type_name == "number" || type_name == "integer") {
  279. auto test_generator = property_generator.fork();
  280. test_generator.set("numbertype", type_name);
  281. StringView min_value;
  282. StringView max_value;
  283. if (!type_args.is_empty()) {
  284. VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
  285. auto comma_index = type_args.find(',').value();
  286. min_value = type_args.substring_view(1, comma_index - 1);
  287. max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
  288. }
  289. test_generator.append(R"~~~(
  290. if ((style_value.has_@numbertype@())~~~");
  291. if (!min_value.is_empty()) {
  292. test_generator.set("minvalue", min_value);
  293. test_generator.append(" && (style_value.to_@numbertype@() >= @minvalue@)");
  294. }
  295. if (!max_value.is_empty()) {
  296. test_generator.set("maxvalue", max_value);
  297. test_generator.append(" && (style_value.to_@numbertype@() <= @maxvalue@)");
  298. }
  299. test_generator.append(R"~~~()
  300. || (style_value.is_calculated() && (style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Integer)~~~");
  301. if (type_name == "number")
  302. test_generator.append(R"~~~(|| style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::Number)~~~");
  303. test_generator.append(R"~~~()))
  304. return true;
  305. )~~~");
  306. } else if (type_name == "string") {
  307. property_generator.append(R"~~~(
  308. if (style_value.is_string())
  309. return true;
  310. )~~~");
  311. } else if (type_name == "url") {
  312. // FIXME: Handle urls!
  313. } else {
  314. warnln("Unrecognized valid-type name: '{}'", type_name);
  315. VERIFY_NOT_REACHED();
  316. }
  317. }
  318. }
  319. }
  320. if (has_valid_identifiers) {
  321. auto valid_identifiers_value = object.get("valid-identifiers");
  322. VERIFY(valid_identifiers_value.is_array());
  323. auto valid_identifiers = valid_identifiers_value.as_array();
  324. if (!valid_identifiers.is_empty()) {
  325. property_generator.append(R"~~~(
  326. switch (style_value.to_identifier()) {
  327. )~~~");
  328. for (auto& identifier : valid_identifiers.values()) {
  329. VERIFY(identifier.is_string());
  330. auto identifier_generator = generator.fork();
  331. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  332. identifier_generator.append(R"~~~(
  333. case ValueID::@identifier:titlecase@:
  334. )~~~");
  335. }
  336. property_generator.append(R"~~~(
  337. return true;
  338. default:
  339. break;
  340. }
  341. )~~~");
  342. }
  343. }
  344. generator.append(R"~~~(
  345. return false;
  346. }
  347. )~~~");
  348. }
  349. });
  350. generator.append(R"~~~(
  351. default:
  352. return true;
  353. }
  354. }
  355. size_t property_maximum_value_count(PropertyID property_id)
  356. {
  357. switch (property_id) {
  358. )~~~");
  359. properties.for_each_member([&](auto& name, auto& value) {
  360. VERIFY(value.is_object());
  361. if (value.as_object().has("max-values")) {
  362. auto max_values = value.as_object().get("max-values");
  363. VERIFY(max_values.is_number() && !max_values.is_double());
  364. auto property_generator = generator.fork();
  365. property_generator.set("name:titlecase", title_casify(name));
  366. property_generator.set("max_values", max_values.to_string());
  367. property_generator.append(R"~~~(
  368. case PropertyID::@name:titlecase@:
  369. return @max_values@;
  370. )~~~");
  371. }
  372. });
  373. generator.append(R"~~~(
  374. default:
  375. return 1;
  376. }
  377. }
  378. } // namespace Web::CSS
  379. )~~~");
  380. outln("{}", generator.as_string_view());
  381. }