GenerateCSSPropertyID.cpp 32 KB

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