GenerateCSSPropertyID.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "GeneratorUtil.h"
  8. #include <AK/CharacterTypes.h>
  9. #include <AK/GenericShorthands.h>
  10. #include <AK/SourceGenerator.h>
  11. #include <AK/StringBuilder.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibMain/Main.h>
  14. ErrorOr<void> replace_logical_aliases(JsonObject& properties);
  15. ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file);
  16. ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file);
  17. ErrorOr<void> generate_bounds_checking_function(JsonObject& properties, SourceGenerator& parent_generator, StringView css_type_name, StringView type_name, Optional<StringView> default_unit_name = {}, Optional<StringView> value_getter = {});
  18. static bool type_name_is_enum(StringView type_name)
  19. {
  20. return !AK::first_is_one_of(type_name, "angle"sv, "color"sv, "custom-ident"sv, "frequency"sv, "image"sv, "integer"sv, "length"sv, "number"sv, "paint"sv, "percentage"sv, "ratio"sv, "rect"sv, "resolution"sv, "string"sv, "time"sv, "url"sv);
  21. }
  22. ErrorOr<int> serenity_main(Main::Arguments arguments)
  23. {
  24. StringView generated_header_path;
  25. StringView generated_implementation_path;
  26. StringView properties_json_path;
  27. Core::ArgsParser args_parser;
  28. args_parser.add_option(generated_header_path, "Path to the PropertyID header file to generate", "generated-header-path", 'h', "generated-header-path");
  29. args_parser.add_option(generated_implementation_path, "Path to the PropertyID implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  30. args_parser.add_option(properties_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
  31. args_parser.parse(arguments);
  32. auto json = TRY(read_entire_file_as_json(properties_json_path));
  33. VERIFY(json.is_object());
  34. auto properties = json.as_object();
  35. TRY(replace_logical_aliases(properties));
  36. auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
  37. auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
  38. TRY(generate_header_file(properties, *generated_header_file));
  39. TRY(generate_implementation_file(properties, *generated_implementation_file));
  40. return 0;
  41. }
  42. ErrorOr<void> replace_logical_aliases(JsonObject& properties)
  43. {
  44. AK::HashMap<DeprecatedString, DeprecatedString> logical_aliases;
  45. properties.for_each_member([&](auto& name, auto& value) {
  46. VERIFY(value.is_object());
  47. const auto& value_as_object = value.as_object();
  48. const auto logical_alias_for = value_as_object.get_array("logical-alias-for"sv);
  49. if (logical_alias_for.has_value()) {
  50. auto const& aliased_properties = logical_alias_for.value();
  51. for (auto const& aliased_property : aliased_properties.values()) {
  52. logical_aliases.set(name, aliased_property.to_deprecated_string());
  53. }
  54. }
  55. });
  56. for (auto& [name, alias] : logical_aliases) {
  57. auto const maybe_alias_object = properties.get_object(alias);
  58. if (!maybe_alias_object.has_value()) {
  59. dbgln("No property '{}' found for logical alias '{}'", alias, name);
  60. VERIFY_NOT_REACHED();
  61. }
  62. JsonObject alias_object = maybe_alias_object.value();
  63. // Copy over anything the logical property overrides
  64. properties.get_object(name).value().for_each_member([&](auto& key, auto& value) {
  65. alias_object.set(key, value);
  66. });
  67. properties.set(name, alias_object);
  68. }
  69. return {};
  70. }
  71. ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file)
  72. {
  73. StringBuilder builder;
  74. SourceGenerator generator { builder };
  75. TRY(generator.try_append(R"~~~(
  76. #pragma once
  77. #include <AK/NonnullRefPtr.h>
  78. #include <AK/StringView.h>
  79. #include <AK/Traits.h>
  80. #include <LibJS/Forward.h>
  81. #include <LibWeb/Forward.h>
  82. namespace Web::CSS {
  83. enum class PropertyID {
  84. Invalid,
  85. Custom,
  86. )~~~"));
  87. Vector<DeprecatedString> shorthand_property_ids;
  88. Vector<DeprecatedString> longhand_property_ids;
  89. properties.for_each_member([&](auto& name, auto& value) {
  90. VERIFY(value.is_object());
  91. if (value.as_object().has("longhands"sv))
  92. shorthand_property_ids.append(name);
  93. else
  94. longhand_property_ids.append(name);
  95. });
  96. auto first_property_id = shorthand_property_ids.first();
  97. auto last_property_id = longhand_property_ids.last();
  98. for (auto& name : shorthand_property_ids) {
  99. auto member_generator = TRY(generator.fork());
  100. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  101. TRY(member_generator.try_append(R"~~~(
  102. @name:titlecase@,
  103. )~~~"));
  104. }
  105. for (auto& name : longhand_property_ids) {
  106. auto member_generator = TRY(generator.fork());
  107. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  108. TRY(member_generator.try_append(R"~~~(
  109. @name:titlecase@,
  110. )~~~"));
  111. }
  112. TRY(generator.set("first_property_id", TRY(title_casify(first_property_id))));
  113. TRY(generator.set("last_property_id", TRY(title_casify(last_property_id))));
  114. TRY(generator.set("first_shorthand_property_id", TRY(title_casify(shorthand_property_ids.first()))));
  115. TRY(generator.set("last_shorthand_property_id", TRY(title_casify(shorthand_property_ids.last()))));
  116. TRY(generator.set("first_longhand_property_id", TRY(title_casify(longhand_property_ids.first()))));
  117. TRY(generator.set("last_longhand_property_id", TRY(title_casify(longhand_property_ids.last()))));
  118. TRY(generator.try_append(R"~~~(
  119. };
  120. Optional<PropertyID> property_id_from_camel_case_string(StringView);
  121. Optional<PropertyID> property_id_from_string(StringView);
  122. StringView string_from_property_id(PropertyID);
  123. bool is_inherited_property(PropertyID);
  124. ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm&, PropertyID);
  125. enum class ValueType {
  126. Angle,
  127. Color,
  128. CustomIdent,
  129. FilterValueList,
  130. Frequency,
  131. Image,
  132. Integer,
  133. Length,
  134. Number,
  135. Paint,
  136. Percentage,
  137. Position,
  138. Ratio,
  139. Rect,
  140. Resolution,
  141. String,
  142. Time,
  143. Url,
  144. };
  145. bool property_accepts_type(PropertyID, ValueType);
  146. bool property_accepts_identifier(PropertyID, ValueID);
  147. Optional<ValueType> property_resolves_percentages_relative_to(PropertyID);
  148. // These perform range-checking, but are also safe to call with properties that don't accept that type. (They'll just return false.)
  149. bool property_accepts_angle(PropertyID, Angle const&);
  150. bool property_accepts_frequency(PropertyID, Frequency const&);
  151. bool property_accepts_integer(PropertyID, i64 const&);
  152. bool property_accepts_length(PropertyID, Length const&);
  153. bool property_accepts_number(PropertyID, double const&);
  154. bool property_accepts_percentage(PropertyID, Percentage const&);
  155. bool property_accepts_resolution(PropertyID, Resolution const&);
  156. bool property_accepts_time(PropertyID, Time const&);
  157. Vector<PropertyID> longhands_for_shorthand(PropertyID);
  158. size_t property_maximum_value_count(PropertyID);
  159. bool property_affects_layout(PropertyID);
  160. bool property_affects_stacking_context(PropertyID);
  161. constexpr PropertyID first_property_id = PropertyID::@first_property_id@;
  162. constexpr PropertyID last_property_id = PropertyID::@last_property_id@;
  163. constexpr PropertyID first_shorthand_property_id = PropertyID::@first_shorthand_property_id@;
  164. constexpr PropertyID last_shorthand_property_id = PropertyID::@last_shorthand_property_id@;
  165. constexpr PropertyID first_longhand_property_id = PropertyID::@first_longhand_property_id@;
  166. constexpr PropertyID last_longhand_property_id = PropertyID::@last_longhand_property_id@;
  167. enum class Quirk {
  168. // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
  169. HashlessHexColor,
  170. // https://quirks.spec.whatwg.org/#the-unitless-length-quirk
  171. UnitlessLength,
  172. };
  173. bool property_has_quirk(PropertyID, Quirk);
  174. } // namespace Web::CSS
  175. namespace AK {
  176. template<>
  177. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  178. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  179. };
  180. } // namespace AK
  181. )~~~"));
  182. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  183. return {};
  184. }
  185. ErrorOr<void> generate_bounds_checking_function(JsonObject& properties, SourceGenerator& parent_generator, StringView css_type_name, StringView type_name, Optional<StringView> default_unit_name, Optional<StringView> value_getter)
  186. {
  187. auto generator = TRY(parent_generator.fork());
  188. TRY(generator.set("css_type_name", TRY(String::from_utf8(css_type_name))));
  189. TRY(generator.set("type_name", TRY(String::from_utf8(type_name))));
  190. TRY(generator.try_append(R"~~~(
  191. bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @type_name@ const& value)
  192. {
  193. switch (property_id) {
  194. )~~~"));
  195. TRY(properties.try_for_each_member([&](auto& name, JsonValue const& value) -> ErrorOr<void> {
  196. VERIFY(value.is_object());
  197. if (auto maybe_valid_types = value.as_object().get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  198. for (auto valid_type : maybe_valid_types->values()) {
  199. auto type_and_range = valid_type.as_string().split_view(' ');
  200. if (type_and_range.first() != css_type_name)
  201. continue;
  202. auto property_generator = TRY(generator.fork());
  203. TRY(property_generator.set("property_name:titlecase", TRY(title_casify(name))));
  204. TRY(property_generator.try_append(R"~~~(
  205. case PropertyID::@property_name:titlecase@:
  206. return )~~~"));
  207. if (type_and_range.size() > 1) {
  208. auto range = type_and_range[1];
  209. VERIFY(range.starts_with('[') && range.ends_with(']') && range.contains(','));
  210. auto comma_index = range.find(',').value();
  211. StringView min_value_string = range.substring_view(1, comma_index - 1);
  212. StringView max_value_string = range.substring_view(comma_index + 1, range.length() - comma_index - 2);
  213. // If the min/max value is infinite, we can just skip that side of the check.
  214. if (min_value_string == "-∞")
  215. min_value_string = {};
  216. if (max_value_string == "∞")
  217. max_value_string = {};
  218. if (min_value_string.is_empty() && max_value_string.is_empty()) {
  219. TRY(property_generator.try_appendln("true;"));
  220. break;
  221. }
  222. auto output_check = [&](auto& value_string, StringView comparator) -> ErrorOr<void> {
  223. if (value_getter.has_value()) {
  224. TRY(property_generator.set("value_number", TRY(String::from_utf8(value_string))));
  225. TRY(property_generator.set("value_getter", TRY(String::from_utf8(value_getter.value()))));
  226. TRY(property_generator.set("comparator", TRY(String::from_utf8(comparator))));
  227. TRY(property_generator.try_append("@value_getter@ @comparator@ @value_number@"));
  228. return {};
  229. }
  230. GenericLexer lexer { value_string };
  231. auto value_number = lexer.consume_until(is_ascii_alpha);
  232. auto value_unit = lexer.consume_while(is_ascii_alpha);
  233. if (value_unit.is_empty())
  234. value_unit = default_unit_name.value();
  235. VERIFY(lexer.is_eof());
  236. TRY(property_generator.set("value_number", TRY(String::from_utf8(value_number))));
  237. TRY(property_generator.set("value_unit", TRY(title_casify(value_unit))));
  238. TRY(property_generator.set("comparator", TRY(String::from_utf8(comparator))));
  239. TRY(property_generator.try_append("value @comparator@ @type_name@(@value_number@, @type_name@::Type::@value_unit@)"));
  240. return {};
  241. };
  242. if (!min_value_string.is_empty())
  243. TRY(output_check(min_value_string, ">="sv));
  244. if (!min_value_string.is_empty() && !max_value_string.is_empty())
  245. TRY(property_generator.try_append(" && "));
  246. if (!max_value_string.is_empty())
  247. TRY(output_check(max_value_string, "<="sv));
  248. TRY(property_generator.try_appendln(";"));
  249. } else {
  250. TRY(property_generator.try_appendln("true;"));
  251. }
  252. break;
  253. }
  254. }
  255. return {};
  256. }));
  257. TRY(generator.try_append(R"~~~(
  258. default:
  259. return false;
  260. }
  261. }
  262. )~~~"));
  263. return {};
  264. }
  265. ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file)
  266. {
  267. StringBuilder builder;
  268. SourceGenerator generator { builder };
  269. TRY(generator.try_append(R"~~~(
  270. #include <AK/Assertions.h>
  271. #include <LibWeb/CSS/Enums.h>
  272. #include <LibWeb/CSS/Parser/Parser.h>
  273. #include <LibWeb/CSS/PropertyID.h>
  274. #include <LibWeb/CSS/StyleValue.h>
  275. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  276. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  277. #include <LibWeb/Infra/Strings.h>
  278. namespace Web::CSS {
  279. Optional<PropertyID> property_id_from_camel_case_string(StringView string)
  280. {
  281. )~~~"));
  282. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  283. VERIFY(value.is_object());
  284. auto member_generator = TRY(generator.fork());
  285. TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
  286. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  287. TRY(member_generator.set("name:camelcase", TRY(camel_casify(name))));
  288. TRY(member_generator.try_append(R"~~~(
  289. if (string.equals_ignoring_ascii_case("@name:camelcase@"sv))
  290. return PropertyID::@name:titlecase@;
  291. )~~~"));
  292. return {};
  293. }));
  294. TRY(generator.try_append(R"~~~(
  295. return {};
  296. }
  297. Optional<PropertyID> property_id_from_string(StringView string)
  298. {
  299. )~~~"));
  300. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  301. VERIFY(value.is_object());
  302. auto member_generator = TRY(generator.fork());
  303. TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
  304. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  305. TRY(member_generator.try_append(R"~~~(
  306. if (Infra::is_ascii_case_insensitive_match(string, "@name@"sv))
  307. return PropertyID::@name:titlecase@;
  308. )~~~"));
  309. return {};
  310. }));
  311. TRY(generator.try_append(R"~~~(
  312. return {};
  313. }
  314. StringView string_from_property_id(PropertyID property_id) {
  315. switch (property_id) {
  316. )~~~"));
  317. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  318. VERIFY(value.is_object());
  319. auto member_generator = TRY(generator.fork());
  320. TRY(member_generator.set("name", TRY(String::from_deprecated_string(name))));
  321. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  322. TRY(member_generator.try_append(R"~~~(
  323. case PropertyID::@name:titlecase@:
  324. return "@name@"sv;
  325. )~~~"));
  326. return {};
  327. }));
  328. TRY(generator.try_append(R"~~~(
  329. default:
  330. return "(invalid CSS::PropertyID)"sv;
  331. }
  332. }
  333. bool is_inherited_property(PropertyID property_id)
  334. {
  335. switch (property_id) {
  336. )~~~"));
  337. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  338. VERIFY(value.is_object());
  339. bool inherited = false;
  340. if (value.as_object().has("inherited"sv)) {
  341. auto inherited_value = value.as_object().get_bool("inherited"sv);
  342. VERIFY(inherited_value.has_value());
  343. inherited = inherited_value.value();
  344. }
  345. if (inherited) {
  346. auto member_generator = TRY(generator.fork());
  347. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  348. TRY(member_generator.try_append(R"~~~(
  349. case PropertyID::@name:titlecase@:
  350. return true;
  351. )~~~"));
  352. }
  353. return {};
  354. }));
  355. TRY(generator.try_append(R"~~~(
  356. default:
  357. return false;
  358. }
  359. }
  360. bool property_affects_layout(PropertyID property_id)
  361. {
  362. switch (property_id) {
  363. )~~~"));
  364. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  365. VERIFY(value.is_object());
  366. bool affects_layout = true;
  367. if (value.as_object().has("affects-layout"sv))
  368. affects_layout = value.as_object().get_bool("affects-layout"sv).value_or(false);
  369. if (affects_layout) {
  370. auto member_generator = TRY(generator.fork());
  371. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  372. TRY(member_generator.try_append(R"~~~(
  373. case PropertyID::@name:titlecase@:
  374. )~~~"));
  375. }
  376. return {};
  377. }));
  378. TRY(generator.try_append(R"~~~(
  379. return true;
  380. default:
  381. return false;
  382. }
  383. }
  384. bool property_affects_stacking_context(PropertyID property_id)
  385. {
  386. switch (property_id) {
  387. )~~~"));
  388. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  389. VERIFY(value.is_object());
  390. bool affects_stacking_context = false;
  391. if (value.as_object().has("affects-stacking-context"sv))
  392. affects_stacking_context = value.as_object().get_bool("affects-stacking-context"sv).value_or(false);
  393. if (affects_stacking_context) {
  394. auto member_generator = TRY(generator.fork());
  395. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  396. TRY(member_generator.try_append(R"~~~(
  397. case PropertyID::@name:titlecase@:
  398. )~~~"));
  399. }
  400. return {};
  401. }));
  402. TRY(generator.try_append(R"~~~(
  403. return true;
  404. default:
  405. return false;
  406. }
  407. }
  408. ErrorOr<NonnullRefPtr<StyleValue>> property_initial_value(JS::Realm& context_realm, PropertyID property_id)
  409. {
  410. static Array<RefPtr<StyleValue>, to_underlying(last_property_id) + 1> initial_values;
  411. if (auto initial_value = initial_values[to_underlying(property_id)])
  412. return initial_value.release_nonnull();
  413. // Lazily parse initial values as needed.
  414. // This ensures the shorthands will always be able to get the initial values of their longhands.
  415. // This also now allows a longhand have its own longhand (like background-position-x).
  416. Parser::ParsingContext parsing_context(context_realm);
  417. switch (property_id) {
  418. )~~~"));
  419. auto output_initial_value_code = [&](auto& name, auto& object) -> ErrorOr<void> {
  420. if (!object.has("initial"sv)) {
  421. dbgln("No initial value specified for property '{}'", name);
  422. VERIFY_NOT_REACHED();
  423. }
  424. auto initial_value = object.get_deprecated_string("initial"sv);
  425. VERIFY(initial_value.has_value());
  426. auto& initial_value_string = initial_value.value();
  427. auto member_generator = TRY(generator.fork());
  428. TRY(member_generator.set("name:titlecase", TRY(title_casify(name))));
  429. TRY(member_generator.set("initial_value_string", TRY(String::from_deprecated_string(initial_value_string))));
  430. TRY(member_generator.try_append(
  431. R"~~~( case PropertyID::@name:titlecase@:
  432. {
  433. auto parsed_value = TRY(parse_css_value(parsing_context, "@initial_value_string@"sv, PropertyID::@name:titlecase@));
  434. VERIFY(!parsed_value.is_null());
  435. auto initial_value = parsed_value.release_nonnull();
  436. initial_values[to_underlying(PropertyID::@name:titlecase@)] = initial_value;
  437. return initial_value;
  438. }
  439. )~~~"));
  440. return {};
  441. };
  442. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  443. VERIFY(value.is_object());
  444. TRY(output_initial_value_code(name, value.as_object()));
  445. return {};
  446. }));
  447. TRY(generator.try_append(
  448. R"~~~( default: VERIFY_NOT_REACHED();
  449. }
  450. VERIFY_NOT_REACHED();
  451. }
  452. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  453. {
  454. switch (property_id) {
  455. )~~~"));
  456. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  457. VERIFY(value.is_object());
  458. if (value.as_object().has("quirks"sv)) {
  459. auto quirks_value = value.as_object().get_array("quirks"sv);
  460. VERIFY(quirks_value.has_value());
  461. auto& quirks = quirks_value.value();
  462. if (!quirks.is_empty()) {
  463. auto property_generator = TRY(generator.fork());
  464. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  465. TRY(property_generator.try_append(R"~~~(
  466. case PropertyID::@name:titlecase@: {
  467. switch (quirk) {
  468. )~~~"));
  469. for (auto& quirk : quirks.values()) {
  470. VERIFY(quirk.is_string());
  471. auto quirk_generator = TRY(property_generator.fork());
  472. TRY(quirk_generator.set("quirk:titlecase", TRY(title_casify(quirk.as_string()))));
  473. TRY(quirk_generator.try_append(R"~~~(
  474. case Quirk::@quirk:titlecase@:
  475. return true;
  476. )~~~"));
  477. }
  478. TRY(property_generator.try_append(R"~~~(
  479. default:
  480. return false;
  481. }
  482. }
  483. )~~~"));
  484. }
  485. }
  486. return {};
  487. }));
  488. TRY(generator.try_append(R"~~~(
  489. default:
  490. return false;
  491. }
  492. }
  493. bool property_accepts_type(PropertyID property_id, ValueType value_type)
  494. {
  495. switch (property_id) {
  496. )~~~"));
  497. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  498. VERIFY(value.is_object());
  499. auto& object = value.as_object();
  500. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  501. auto& valid_types = maybe_valid_types.value();
  502. auto property_generator = TRY(generator.fork());
  503. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  504. TRY(property_generator.try_append(R"~~~(
  505. case PropertyID::@name:titlecase@: {
  506. switch (value_type) {
  507. )~~~"));
  508. bool did_output_accepted_type = false;
  509. for (auto& type : valid_types.values()) {
  510. VERIFY(type.is_string());
  511. auto type_name = type.as_string().split_view(' ').first();
  512. if (type_name_is_enum(type_name))
  513. continue;
  514. if (type_name == "angle") {
  515. TRY(property_generator.try_appendln(" case ValueType::Angle:"));
  516. } else if (type_name == "color") {
  517. TRY(property_generator.try_appendln(" case ValueType::Color:"));
  518. } else if (type_name == "custom-ident") {
  519. TRY(property_generator.try_appendln(" case ValueType::CustomIdent:"));
  520. } else if (type_name == "frequency") {
  521. TRY(property_generator.try_appendln(" case ValueType::Frequency:"));
  522. } else if (type_name == "image") {
  523. TRY(property_generator.try_appendln(" case ValueType::Image:"));
  524. } else if (type_name == "integer") {
  525. TRY(property_generator.try_appendln(" case ValueType::Integer:"));
  526. } else if (type_name == "length") {
  527. TRY(property_generator.try_appendln(" case ValueType::Length:"));
  528. } else if (type_name == "number") {
  529. TRY(property_generator.try_appendln(" case ValueType::Number:"));
  530. } else if (type_name == "paint") {
  531. TRY(property_generator.try_appendln(" case ValueType::Paint:"));
  532. } else if (type_name == "percentage") {
  533. TRY(property_generator.try_appendln(" case ValueType::Percentage:"));
  534. } else if (type_name == "ratio") {
  535. TRY(property_generator.try_appendln(" case ValueType::Ratio:"));
  536. } else if (type_name == "rect") {
  537. TRY(property_generator.try_appendln(" case ValueType::Rect:"));
  538. } else if (type_name == "resolution") {
  539. TRY(property_generator.try_appendln(" case ValueType::Resolution:"));
  540. } else if (type_name == "string") {
  541. TRY(property_generator.try_appendln(" case ValueType::String:"));
  542. } else if (type_name == "time") {
  543. TRY(property_generator.try_appendln(" case ValueType::Time:"));
  544. } else if (type_name == "url") {
  545. TRY(property_generator.try_appendln(" case ValueType::Url:"));
  546. } else {
  547. VERIFY_NOT_REACHED();
  548. }
  549. did_output_accepted_type = true;
  550. }
  551. if (did_output_accepted_type)
  552. TRY(property_generator.try_appendln(" return true;"));
  553. TRY(property_generator.try_append(R"~~~(
  554. default:
  555. return false;
  556. }
  557. }
  558. )~~~"));
  559. }
  560. return {};
  561. }));
  562. TRY(generator.try_append(R"~~~(
  563. default:
  564. return false;
  565. }
  566. }
  567. bool property_accepts_identifier(PropertyID property_id, ValueID identifier)
  568. {
  569. switch (property_id) {
  570. )~~~"));
  571. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  572. VERIFY(value.is_object());
  573. auto& object = value.as_object();
  574. auto property_generator = TRY(generator.fork());
  575. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  576. TRY(property_generator.try_appendln(" case PropertyID::@name:titlecase@: {"));
  577. if (auto maybe_valid_identifiers = object.get_array("valid-identifiers"sv); maybe_valid_identifiers.has_value() && !maybe_valid_identifiers->is_empty()) {
  578. TRY(property_generator.try_appendln(" switch (identifier) {"));
  579. auto& valid_identifiers = maybe_valid_identifiers.value();
  580. for (auto& identifier : valid_identifiers.values()) {
  581. auto identifier_generator = TRY(generator.fork());
  582. TRY(identifier_generator.set("identifier:titlecase", TRY(title_casify(identifier.as_string()))));
  583. TRY(identifier_generator.try_appendln(" case ValueID::@identifier:titlecase@:"));
  584. }
  585. TRY(property_generator.try_append(R"~~~(
  586. return true;
  587. default:
  588. break;
  589. }
  590. )~~~"));
  591. }
  592. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  593. auto& valid_types = maybe_valid_types.value();
  594. for (auto& valid_type : valid_types.values()) {
  595. auto type_name = valid_type.as_string().split_view(' ').first();
  596. if (!type_name_is_enum(type_name))
  597. continue;
  598. auto type_generator = TRY(generator.fork());
  599. TRY(type_generator.set("type_name:snakecase", TRY(snake_casify(type_name))));
  600. TRY(type_generator.try_append(R"~~~(
  601. if (value_id_to_@type_name:snakecase@(identifier).has_value())
  602. return true;
  603. )~~~"));
  604. }
  605. }
  606. TRY(property_generator.try_append(R"~~~(
  607. return false;
  608. }
  609. )~~~"));
  610. return {};
  611. }));
  612. TRY(generator.try_append(R"~~~(
  613. default:
  614. return false;
  615. }
  616. }
  617. Optional<ValueType> property_resolves_percentages_relative_to(PropertyID property_id)
  618. {
  619. switch (property_id) {
  620. )~~~"));
  621. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  622. VERIFY(value.is_object());
  623. if (auto resolved_type = value.as_object().get_deprecated_string("percentages-resolve-to"sv); resolved_type.has_value()) {
  624. auto property_generator = TRY(generator.fork());
  625. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  626. TRY(property_generator.set("resolved_type:titlecase", TRY(title_casify(resolved_type.value()))));
  627. TRY(property_generator.try_append(R"~~~(
  628. case PropertyID::@name:titlecase@:
  629. return ValueType::@resolved_type:titlecase@;
  630. )~~~"));
  631. }
  632. return {};
  633. }));
  634. TRY(generator.try_append(R"~~~(
  635. default:
  636. return {};
  637. }
  638. }
  639. size_t property_maximum_value_count(PropertyID property_id)
  640. {
  641. switch (property_id) {
  642. )~~~"));
  643. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  644. VERIFY(value.is_object());
  645. if (value.as_object().has("max-values"sv)) {
  646. auto max_values = value.as_object().get("max-values"sv);
  647. VERIFY(max_values.has_value() && max_values->is_number() && !max_values->is_double());
  648. auto property_generator = TRY(generator.fork());
  649. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  650. TRY(property_generator.set("max_values", TRY(String::from_deprecated_string(max_values->to_deprecated_string()))));
  651. TRY(property_generator.try_append(R"~~~(
  652. case PropertyID::@name:titlecase@:
  653. return @max_values@;
  654. )~~~"));
  655. }
  656. return {};
  657. }));
  658. TRY(generator.try_append(R"~~~(
  659. default:
  660. return 1;
  661. }
  662. })~~~"));
  663. TRY(generate_bounds_checking_function(properties, generator, "angle"sv, "Angle"sv, "Deg"sv));
  664. TRY(generate_bounds_checking_function(properties, generator, "frequency"sv, "Frequency"sv, "Hertz"sv));
  665. TRY(generate_bounds_checking_function(properties, generator, "integer"sv, "i64"sv, {}, "value"sv));
  666. TRY(generate_bounds_checking_function(properties, generator, "length"sv, "Length"sv, {}, "value.raw_value()"sv));
  667. TRY(generate_bounds_checking_function(properties, generator, "number"sv, "double"sv, {}, "value"sv));
  668. TRY(generate_bounds_checking_function(properties, generator, "percentage"sv, "Percentage"sv, {}, "value.value()"sv));
  669. TRY(generate_bounds_checking_function(properties, generator, "resolution"sv, "Resolution"sv, "Dpi"sv));
  670. TRY(generate_bounds_checking_function(properties, generator, "time"sv, "Time"sv, "S"sv));
  671. TRY(generator.try_append(R"~~~(
  672. Vector<PropertyID> longhands_for_shorthand(PropertyID property_id)
  673. {
  674. switch (property_id) {
  675. )~~~"));
  676. TRY(properties.try_for_each_member([&](auto& name, auto& value) -> ErrorOr<void> {
  677. if (value.as_object().has("longhands"sv)) {
  678. auto longhands = value.as_object().get("longhands"sv);
  679. VERIFY(longhands.has_value() && longhands->is_array());
  680. auto longhand_values = longhands->as_array();
  681. auto property_generator = TRY(generator.fork());
  682. TRY(property_generator.set("name:titlecase", TRY(title_casify(name))));
  683. StringBuilder builder;
  684. bool first = true;
  685. TRY(longhand_values.try_for_each([&](auto& longhand) -> ErrorOr<IterationDecision> {
  686. if (first)
  687. first = false;
  688. else
  689. TRY(builder.try_append(", "sv));
  690. TRY(builder.try_appendff("PropertyID::{}", TRY(title_casify(longhand.to_deprecated_string()))));
  691. return IterationDecision::Continue;
  692. }));
  693. property_generator.set("longhands", builder.to_deprecated_string());
  694. TRY(property_generator.try_append(R"~~~(
  695. case PropertyID::@name:titlecase@:
  696. return { @longhands@ };
  697. )~~~"));
  698. }
  699. return {};
  700. }));
  701. TRY(generator.try_append(R"~~~(
  702. default:
  703. return { };
  704. }
  705. }
  706. )~~~"));
  707. TRY(generator.try_append(R"~~~(
  708. } // namespace Web::CSS
  709. )~~~"));
  710. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  711. return {};
  712. }