GenerateCSSPropertyID.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "GeneratorUtil.h"
  8. #include <AK/SourceGenerator.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibMain/Main.h>
  12. ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file);
  13. ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file);
  14. ErrorOr<int> serenity_main(Main::Arguments arguments)
  15. {
  16. StringView generated_header_path;
  17. StringView generated_implementation_path;
  18. StringView properties_json_path;
  19. Core::ArgsParser args_parser;
  20. args_parser.add_option(generated_header_path, "Path to the PropertyID header file to generate", "generated-header-path", 'h', "generated-header-path");
  21. args_parser.add_option(generated_implementation_path, "Path to the PropertyID implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  22. args_parser.add_option(properties_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
  23. args_parser.parse(arguments);
  24. auto json = TRY(read_entire_file_as_json(properties_json_path));
  25. VERIFY(json.is_object());
  26. auto properties = json.as_object();
  27. auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
  28. auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
  29. TRY(generate_header_file(properties, *generated_header_file));
  30. TRY(generate_implementation_file(properties, *generated_implementation_file));
  31. return 0;
  32. }
  33. ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file)
  34. {
  35. StringBuilder builder;
  36. SourceGenerator generator { builder };
  37. generator.append(R"~~~(
  38. #pragma once
  39. #include <AK/NonnullRefPtr.h>
  40. #include <AK/StringView.h>
  41. #include <AK/Traits.h>
  42. #include <LibJS/Forward.h>
  43. #include <LibWeb/Forward.h>
  44. namespace Web::CSS {
  45. enum class PropertyID {
  46. Invalid,
  47. Custom,
  48. )~~~");
  49. Vector<DeprecatedString> shorthand_property_ids;
  50. Vector<DeprecatedString> longhand_property_ids;
  51. properties.for_each_member([&](auto& name, auto& value) {
  52. VERIFY(value.is_object());
  53. if (value.as_object().has("longhands"sv))
  54. shorthand_property_ids.append(name);
  55. else
  56. longhand_property_ids.append(name);
  57. });
  58. auto first_property_id = shorthand_property_ids.first();
  59. auto last_property_id = longhand_property_ids.last();
  60. for (auto& name : shorthand_property_ids) {
  61. auto member_generator = generator.fork();
  62. member_generator.set("name:titlecase", title_casify(name));
  63. member_generator.append(R"~~~(
  64. @name:titlecase@,
  65. )~~~");
  66. }
  67. for (auto& name : longhand_property_ids) {
  68. auto member_generator = generator.fork();
  69. member_generator.set("name:titlecase", title_casify(name));
  70. member_generator.append(R"~~~(
  71. @name:titlecase@,
  72. )~~~");
  73. }
  74. generator.set("first_property_id", title_casify(first_property_id));
  75. generator.set("last_property_id", title_casify(last_property_id));
  76. generator.set("first_shorthand_property_id", title_casify(shorthand_property_ids.first()));
  77. generator.set("last_shorthand_property_id", title_casify(shorthand_property_ids.last()));
  78. generator.set("first_longhand_property_id", title_casify(longhand_property_ids.first()));
  79. generator.set("last_longhand_property_id", title_casify(longhand_property_ids.last()));
  80. generator.append(R"~~~(
  81. };
  82. PropertyID property_id_from_camel_case_string(StringView);
  83. PropertyID property_id_from_string(StringView);
  84. StringView string_from_property_id(PropertyID);
  85. bool is_inherited_property(PropertyID);
  86. NonnullRefPtr<StyleValue> property_initial_value(JS::Realm&, PropertyID);
  87. bool property_accepts_value(PropertyID, StyleValue&);
  88. size_t property_maximum_value_count(PropertyID);
  89. bool property_affects_layout(PropertyID);
  90. bool property_affects_stacking_context(PropertyID);
  91. constexpr PropertyID first_property_id = PropertyID::@first_property_id@;
  92. constexpr PropertyID last_property_id = PropertyID::@last_property_id@;
  93. constexpr PropertyID first_shorthand_property_id = PropertyID::@first_shorthand_property_id@;
  94. constexpr PropertyID last_shorthand_property_id = PropertyID::@last_shorthand_property_id@;
  95. constexpr PropertyID first_longhand_property_id = PropertyID::@first_longhand_property_id@;
  96. constexpr PropertyID last_longhand_property_id = PropertyID::@last_longhand_property_id@;
  97. enum class Quirk {
  98. // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
  99. HashlessHexColor,
  100. // https://quirks.spec.whatwg.org/#the-unitless-length-quirk
  101. UnitlessLength,
  102. };
  103. bool property_has_quirk(PropertyID, Quirk);
  104. } // namespace Web::CSS
  105. namespace AK {
  106. template<>
  107. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  108. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  109. };
  110. } // namespace AK
  111. )~~~");
  112. // FIXME: This should write the entire span.
  113. TRY(file.write_some(generator.as_string_view().bytes()));
  114. return {};
  115. }
  116. ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file)
  117. {
  118. StringBuilder builder;
  119. SourceGenerator generator { builder };
  120. generator.append(R"~~~(
  121. #include <AK/Assertions.h>
  122. #include <LibWeb/CSS/Enums.h>
  123. #include <LibWeb/CSS/Parser/Parser.h>
  124. #include <LibWeb/CSS/PropertyID.h>
  125. #include <LibWeb/CSS/StyleValue.h>
  126. #include <LibWeb/Infra/Strings.h>
  127. namespace Web::CSS {
  128. PropertyID property_id_from_camel_case_string(StringView string)
  129. {
  130. )~~~");
  131. properties.for_each_member([&](auto& name, auto& value) {
  132. VERIFY(value.is_object());
  133. auto member_generator = generator.fork();
  134. member_generator.set("name", name);
  135. member_generator.set("name:titlecase", title_casify(name));
  136. member_generator.set("name:camelcase", camel_casify(name));
  137. member_generator.append(R"~~~(
  138. if (string.equals_ignoring_ascii_case("@name:camelcase@"sv))
  139. return PropertyID::@name:titlecase@;
  140. )~~~");
  141. });
  142. generator.append(R"~~~(
  143. return PropertyID::Invalid;
  144. }
  145. PropertyID property_id_from_string(StringView string)
  146. {
  147. )~~~");
  148. properties.for_each_member([&](auto& name, auto& value) {
  149. VERIFY(value.is_object());
  150. auto member_generator = generator.fork();
  151. member_generator.set("name", name);
  152. member_generator.set("name:titlecase", title_casify(name));
  153. member_generator.append(R"~~~(
  154. if (Infra::is_ascii_case_insensitive_match(string, "@name@"sv))
  155. return PropertyID::@name:titlecase@;
  156. )~~~");
  157. });
  158. generator.append(R"~~~(
  159. return PropertyID::Invalid;
  160. }
  161. StringView string_from_property_id(PropertyID property_id) {
  162. switch (property_id) {
  163. )~~~");
  164. properties.for_each_member([&](auto& name, auto& value) {
  165. VERIFY(value.is_object());
  166. auto member_generator = generator.fork();
  167. member_generator.set("name", name);
  168. member_generator.set("name:titlecase", title_casify(name));
  169. member_generator.append(R"~~~(
  170. case PropertyID::@name:titlecase@:
  171. return "@name@"sv;
  172. )~~~");
  173. });
  174. generator.append(R"~~~(
  175. default:
  176. return "(invalid CSS::PropertyID)"sv;
  177. }
  178. }
  179. bool is_inherited_property(PropertyID property_id)
  180. {
  181. switch (property_id) {
  182. )~~~");
  183. properties.for_each_member([&](auto& name, auto& value) {
  184. VERIFY(value.is_object());
  185. bool inherited = false;
  186. if (value.as_object().has("inherited"sv)) {
  187. auto inherited_value = value.as_object().get_bool("inherited"sv);
  188. VERIFY(inherited_value.has_value());
  189. inherited = inherited_value.value();
  190. }
  191. if (inherited) {
  192. auto member_generator = generator.fork();
  193. member_generator.set("name:titlecase", title_casify(name));
  194. member_generator.append(R"~~~(
  195. case PropertyID::@name:titlecase@:
  196. return true;
  197. )~~~");
  198. }
  199. });
  200. generator.append(R"~~~(
  201. default:
  202. return false;
  203. }
  204. }
  205. bool property_affects_layout(PropertyID property_id)
  206. {
  207. switch (property_id) {
  208. )~~~");
  209. properties.for_each_member([&](auto& name, auto& value) {
  210. VERIFY(value.is_object());
  211. bool affects_layout = true;
  212. if (value.as_object().has("affects-layout"sv))
  213. affects_layout = value.as_object().get_bool("affects-layout"sv).value_or(false);
  214. if (affects_layout) {
  215. auto member_generator = generator.fork();
  216. member_generator.set("name:titlecase", title_casify(name));
  217. member_generator.append(R"~~~(
  218. case PropertyID::@name:titlecase@:
  219. )~~~");
  220. }
  221. });
  222. generator.append(R"~~~(
  223. return true;
  224. default:
  225. return false;
  226. }
  227. }
  228. bool property_affects_stacking_context(PropertyID property_id)
  229. {
  230. switch (property_id) {
  231. )~~~");
  232. properties.for_each_member([&](auto& name, auto& value) {
  233. VERIFY(value.is_object());
  234. bool affects_stacking_context = false;
  235. if (value.as_object().has("affects-stacking-context"sv))
  236. affects_stacking_context = value.as_object().get_bool("affects-stacking-context"sv).value_or(false);
  237. if (affects_stacking_context) {
  238. auto member_generator = generator.fork();
  239. member_generator.set("name:titlecase", title_casify(name));
  240. member_generator.append(R"~~~(
  241. case PropertyID::@name:titlecase@:
  242. )~~~");
  243. }
  244. });
  245. generator.append(R"~~~(
  246. return true;
  247. default:
  248. return false;
  249. }
  250. }
  251. NonnullRefPtr<StyleValue> property_initial_value(JS::Realm& context_realm, PropertyID property_id)
  252. {
  253. static Array<RefPtr<StyleValue>, to_underlying(last_property_id) + 1> initial_values;
  254. static bool initialized = false;
  255. if (!initialized) {
  256. initialized = true;
  257. Parser::ParsingContext parsing_context(context_realm);
  258. )~~~");
  259. // NOTE: Parsing a shorthand property requires that its longhands are already available here.
  260. // So, we do this in two passes: First longhands, then shorthands.
  261. // Probably we should build a dependency graph and then handle them in order, but this
  262. // works for now! :^)
  263. auto output_initial_value_code = [&](auto& name, auto& object) {
  264. if (!object.has("initial"sv)) {
  265. dbgln("No initial value specified for property '{}'", name);
  266. VERIFY_NOT_REACHED();
  267. }
  268. auto initial_value = object.get_deprecated_string("initial"sv);
  269. VERIFY(initial_value.has_value());
  270. auto& initial_value_string = initial_value.value();
  271. auto member_generator = generator.fork();
  272. member_generator.set("name:titlecase", title_casify(name));
  273. member_generator.set("initial_value_string", initial_value_string);
  274. member_generator.append(R"~~~(
  275. {
  276. auto parsed_value = parse_css_value(parsing_context, "@initial_value_string@"sv, PropertyID::@name:titlecase@);
  277. VERIFY(!parsed_value.is_null());
  278. initial_values[to_underlying(PropertyID::@name:titlecase@)] = parsed_value.release_nonnull();
  279. }
  280. )~~~");
  281. };
  282. properties.for_each_member([&](auto& name, auto& value) {
  283. VERIFY(value.is_object());
  284. if (value.as_object().has("longhands"sv))
  285. return;
  286. output_initial_value_code(name, value.as_object());
  287. });
  288. properties.for_each_member([&](auto& name, auto& value) {
  289. VERIFY(value.is_object());
  290. if (!value.as_object().has("longhands"sv))
  291. return;
  292. output_initial_value_code(name, value.as_object());
  293. });
  294. generator.append(R"~~~(
  295. }
  296. return *initial_values[to_underlying(property_id)];
  297. }
  298. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  299. {
  300. switch (property_id) {
  301. )~~~");
  302. properties.for_each_member([&](auto& name, auto& value) {
  303. VERIFY(value.is_object());
  304. if (value.as_object().has("quirks"sv)) {
  305. auto quirks_value = value.as_object().get_array("quirks"sv);
  306. VERIFY(quirks_value.has_value());
  307. auto& quirks = quirks_value.value();
  308. if (!quirks.is_empty()) {
  309. auto property_generator = generator.fork();
  310. property_generator.set("name:titlecase", title_casify(name));
  311. property_generator.append(R"~~~(
  312. case PropertyID::@name:titlecase@: {
  313. switch (quirk) {
  314. )~~~");
  315. for (auto& quirk : quirks.values()) {
  316. VERIFY(quirk.is_string());
  317. auto quirk_generator = property_generator.fork();
  318. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  319. quirk_generator.append(R"~~~(
  320. case Quirk::@quirk:titlecase@:
  321. return true;
  322. )~~~");
  323. }
  324. property_generator.append(R"~~~(
  325. default:
  326. return false;
  327. }
  328. }
  329. )~~~");
  330. }
  331. }
  332. });
  333. generator.append(R"~~~(
  334. default:
  335. return false;
  336. }
  337. }
  338. bool property_accepts_value(PropertyID property_id, StyleValue& style_value)
  339. {
  340. if (style_value.is_builtin())
  341. return true;
  342. switch (property_id) {
  343. )~~~");
  344. properties.for_each_member([&](auto& name, auto& value) {
  345. VERIFY(value.is_object());
  346. auto& object = value.as_object();
  347. bool has_valid_types = object.has("valid-types"sv);
  348. auto has_valid_identifiers = object.has("valid-identifiers"sv);
  349. if (has_valid_types || has_valid_identifiers) {
  350. auto property_generator = generator.fork();
  351. property_generator.set("name:titlecase", title_casify(name));
  352. property_generator.append(R"~~~(
  353. case PropertyID::@name:titlecase@: {
  354. )~~~");
  355. auto output_numeric_value_check = [](SourceGenerator& generator, StringView type_check_function, StringView value_getter, Span<StringView> resolved_type_names, StringView min_value, StringView max_value) {
  356. auto test_generator = generator.fork();
  357. test_generator.set("type_check_function", type_check_function);
  358. test_generator.set("value_getter", value_getter);
  359. test_generator.append(R"~~~(
  360. if ((style_value.@type_check_function@())~~~");
  361. if (!min_value.is_empty() && min_value != "-∞") {
  362. test_generator.set("minvalue", min_value);
  363. test_generator.append(" && (style_value.@value_getter@ >= @minvalue@)");
  364. }
  365. if (!max_value.is_empty() && max_value != "∞") {
  366. test_generator.set("maxvalue", max_value);
  367. test_generator.append(" && (style_value.@value_getter@ <= @maxvalue@)");
  368. }
  369. test_generator.append(")");
  370. if (!resolved_type_names.is_empty()) {
  371. test_generator.append(R"~~~(
  372. || (style_value.is_calculated() && ()~~~");
  373. bool first = true;
  374. for (auto& type_name : resolved_type_names) {
  375. test_generator.set("resolved_type_name", type_name);
  376. if (!first)
  377. test_generator.append(" || ");
  378. test_generator.append("style_value.as_calculated().resolved_type() == CalculatedStyleValue::ResolvedType::@resolved_type_name@");
  379. first = false;
  380. }
  381. test_generator.append("))");
  382. }
  383. test_generator.append(R"~~~() {
  384. return true;
  385. }
  386. )~~~");
  387. };
  388. if (has_valid_types) {
  389. auto valid_types_value = object.get_array("valid-types"sv);
  390. VERIFY(valid_types_value.has_value());
  391. auto& valid_types = valid_types_value.value();
  392. if (!valid_types.is_empty()) {
  393. for (auto& type : valid_types.values()) {
  394. VERIFY(type.is_string());
  395. auto type_parts = type.as_string().split_view(' ');
  396. auto type_name = type_parts.first();
  397. auto type_args = type_parts.size() > 1 ? type_parts[1] : ""sv;
  398. StringView min_value;
  399. StringView max_value;
  400. if (!type_args.is_empty()) {
  401. VERIFY(type_args.starts_with('[') && type_args.ends_with(']'));
  402. auto comma_index = type_args.find(',').value();
  403. min_value = type_args.substring_view(1, comma_index - 1);
  404. max_value = type_args.substring_view(comma_index + 1, type_args.length() - comma_index - 2);
  405. }
  406. if (type_name == "angle") {
  407. output_numeric_value_check(property_generator, "is_angle"sv, "as_angle().angle().to_degrees()"sv, Array { "Angle"sv }, min_value, max_value);
  408. } else if (type_name == "color") {
  409. property_generator.append(R"~~~(
  410. if (style_value.has_color())
  411. return true;
  412. )~~~");
  413. } else if (type_name == "filter-value-list") {
  414. property_generator.append(R"~~~(
  415. if (style_value.is_filter_value_list())
  416. return true;
  417. )~~~");
  418. } else if (type_name == "frequency") {
  419. output_numeric_value_check(property_generator, "is_frequency"sv, "as_frequency().frequency().to_hertz()"sv, Array { "Frequency"sv }, min_value, max_value);
  420. } else if (type_name == "image") {
  421. property_generator.append(R"~~~(
  422. if (style_value.is_abstract_image())
  423. return true;
  424. )~~~");
  425. } else if (type_name == "integer") {
  426. output_numeric_value_check(property_generator, "has_integer"sv, "to_integer()"sv, Array { "Integer"sv }, min_value, max_value);
  427. } else if (type_name == "length") {
  428. output_numeric_value_check(property_generator, "has_length"sv, "to_length().raw_value()"sv, Array { "Length"sv }, min_value, max_value);
  429. } else if (type_name == "number") {
  430. output_numeric_value_check(property_generator, "has_number"sv, "to_number()"sv, Array { "Integer"sv, "Number"sv }, min_value, max_value);
  431. } else if (type_name == "percentage") {
  432. output_numeric_value_check(property_generator, "is_percentage"sv, "as_percentage().percentage().value()"sv, Array { "Percentage"sv }, min_value, max_value);
  433. } else if (type_name == "rect") {
  434. property_generator.append(R"~~~(
  435. if (style_value.has_rect())
  436. return true;
  437. )~~~");
  438. } else if (type_name == "resolution") {
  439. output_numeric_value_check(property_generator, "is_resolution"sv, "as_resolution().resolution().to_dots_per_pixel()"sv, Array<StringView, 0> {}, min_value, max_value);
  440. } else if (type_name == "string") {
  441. property_generator.append(R"~~~(
  442. if (style_value.is_string())
  443. return true;
  444. )~~~");
  445. } else if (type_name == "time") {
  446. output_numeric_value_check(property_generator, "is_time"sv, "as_time().time().to_seconds()"sv, Array { "Time"sv }, min_value, max_value);
  447. } else if (type_name == "url") {
  448. // FIXME: Handle urls!
  449. } else {
  450. // Assume that any other type names are defined in Enums.json.
  451. // If they're not, the output won't compile, but that's fine since it's invalid.
  452. property_generator.set("type_name:snakecase", snake_casify(type_name));
  453. property_generator.append(R"~~~(
  454. if (auto converted_identifier = value_id_to_@type_name:snakecase@(style_value.to_identifier()); converted_identifier.has_value())
  455. return true;
  456. )~~~");
  457. }
  458. }
  459. }
  460. }
  461. if (has_valid_identifiers) {
  462. auto valid_identifiers_value = object.get_array("valid-identifiers"sv);
  463. VERIFY(valid_identifiers_value.has_value());
  464. auto& valid_identifiers = valid_identifiers_value.value();
  465. if (!valid_identifiers.is_empty()) {
  466. property_generator.append(R"~~~(
  467. switch (style_value.to_identifier()) {
  468. )~~~");
  469. for (auto& identifier : valid_identifiers.values()) {
  470. VERIFY(identifier.is_string());
  471. auto identifier_generator = generator.fork();
  472. identifier_generator.set("identifier:titlecase", title_casify(identifier.as_string()));
  473. identifier_generator.append(R"~~~(
  474. case ValueID::@identifier:titlecase@:
  475. )~~~");
  476. }
  477. property_generator.append(R"~~~(
  478. return true;
  479. default:
  480. break;
  481. }
  482. )~~~");
  483. }
  484. }
  485. generator.append(R"~~~(
  486. return false;
  487. }
  488. )~~~");
  489. }
  490. });
  491. generator.append(R"~~~(
  492. default:
  493. return true;
  494. }
  495. }
  496. size_t property_maximum_value_count(PropertyID property_id)
  497. {
  498. switch (property_id) {
  499. )~~~");
  500. properties.for_each_member([&](auto& name, auto& value) {
  501. VERIFY(value.is_object());
  502. if (value.as_object().has("max-values"sv)) {
  503. auto max_values = value.as_object().get("max-values"sv);
  504. VERIFY(max_values.has_value() && max_values->is_number() && !max_values->is_double());
  505. auto property_generator = generator.fork();
  506. property_generator.set("name:titlecase", title_casify(name));
  507. property_generator.set("max_values", max_values->to_deprecated_string());
  508. property_generator.append(R"~~~(
  509. case PropertyID::@name:titlecase@:
  510. return @max_values@;
  511. )~~~");
  512. }
  513. });
  514. generator.append(R"~~~(
  515. default:
  516. return 1;
  517. }
  518. }
  519. } // namespace Web::CSS
  520. )~~~");
  521. // FIXME: This should write the entire span.
  522. TRY(file.write_some(generator.as_string_view().bytes()));
  523. return {};
  524. }