GenerateCSSPropertyID.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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. 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. 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. bool is_animatable_property(JsonObject& properties, StringView property_name);
  19. static bool type_name_is_enum(StringView type_name)
  20. {
  21. return !AK::first_is_one_of(type_name,
  22. "angle"sv,
  23. "background-position"sv,
  24. "basic-shape"sv,
  25. "color"sv,
  26. "counter"sv,
  27. "custom-ident"sv,
  28. "easing-function"sv,
  29. "flex"sv,
  30. "frequency"sv,
  31. "image"sv,
  32. "integer"sv,
  33. "length"sv,
  34. "number"sv,
  35. "opentype-tag"sv,
  36. "paint"sv,
  37. "percentage"sv,
  38. "position"sv,
  39. "ratio"sv,
  40. "rect"sv,
  41. "resolution"sv,
  42. "string"sv,
  43. "time"sv,
  44. "url"sv);
  45. }
  46. static bool is_legacy_alias(JsonObject const& property)
  47. {
  48. return property.has_string("legacy-alias-for"sv);
  49. }
  50. ErrorOr<int> serenity_main(Main::Arguments arguments)
  51. {
  52. StringView generated_header_path;
  53. StringView generated_implementation_path;
  54. StringView properties_json_path;
  55. Core::ArgsParser args_parser;
  56. args_parser.add_option(generated_header_path, "Path to the PropertyID header file to generate", "generated-header-path", 'h', "generated-header-path");
  57. args_parser.add_option(generated_implementation_path, "Path to the PropertyID implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  58. args_parser.add_option(properties_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
  59. args_parser.parse(arguments);
  60. auto json = TRY(read_entire_file_as_json(properties_json_path));
  61. VERIFY(json.is_object());
  62. auto properties = json.as_object();
  63. // Check we're in alphabetical order
  64. ByteString most_recent_name = "";
  65. properties.for_each_member([&](auto& name, auto&) {
  66. if (name < most_recent_name) {
  67. warnln("`{}` is in the wrong position in `{}`. Please keep this list alphabetical!", name, properties_json_path);
  68. VERIFY_NOT_REACHED();
  69. }
  70. most_recent_name = name;
  71. });
  72. replace_logical_aliases(properties);
  73. auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
  74. auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
  75. TRY(generate_header_file(properties, *generated_header_file));
  76. TRY(generate_implementation_file(properties, *generated_implementation_file));
  77. return 0;
  78. }
  79. void replace_logical_aliases(JsonObject& properties)
  80. {
  81. AK::HashMap<ByteString, ByteString> logical_aliases;
  82. properties.for_each_member([&](auto& name, auto& value) {
  83. VERIFY(value.is_object());
  84. auto const& value_as_object = value.as_object();
  85. auto const logical_alias_for = value_as_object.get_array("logical-alias-for"sv);
  86. if (logical_alias_for.has_value()) {
  87. auto const& aliased_properties = logical_alias_for.value();
  88. for (auto const& aliased_property : aliased_properties.values()) {
  89. logical_aliases.set(name, aliased_property.as_string());
  90. }
  91. }
  92. });
  93. for (auto& [name, alias] : logical_aliases) {
  94. auto const maybe_alias_object = properties.get_object(alias);
  95. if (!maybe_alias_object.has_value()) {
  96. dbgln("No property '{}' found for logical alias '{}'", alias, name);
  97. VERIFY_NOT_REACHED();
  98. }
  99. JsonObject alias_object = maybe_alias_object.value();
  100. // Copy over anything the logical property overrides
  101. properties.get_object(name).value().for_each_member([&](auto& key, auto& value) {
  102. alias_object.set(key, value);
  103. });
  104. properties.set(name, alias_object);
  105. }
  106. }
  107. ErrorOr<void> generate_header_file(JsonObject& properties, Core::File& file)
  108. {
  109. StringBuilder builder;
  110. SourceGenerator generator { builder };
  111. generator.append(R"~~~(
  112. #pragma once
  113. #include <AK/NonnullRefPtr.h>
  114. #include <AK/StringView.h>
  115. #include <AK/Traits.h>
  116. #include <LibJS/Forward.h>
  117. #include <LibWeb/Forward.h>
  118. namespace Web::CSS {
  119. enum class PropertyID {
  120. Invalid,
  121. Custom,
  122. All,
  123. )~~~");
  124. Vector<ByteString> inherited_shorthand_property_ids;
  125. Vector<ByteString> inherited_longhand_property_ids;
  126. Vector<ByteString> noninherited_shorthand_property_ids;
  127. Vector<ByteString> noninherited_longhand_property_ids;
  128. properties.for_each_member([&](auto& name, auto& value) {
  129. VERIFY(value.is_object());
  130. // Legacy aliases don't get a PropertyID
  131. if (is_legacy_alias(value.as_object()))
  132. return;
  133. bool inherited = value.as_object().get_bool("inherited"sv).value_or(false);
  134. if (value.as_object().has("longhands"sv)) {
  135. if (inherited)
  136. inherited_shorthand_property_ids.append(name);
  137. else
  138. noninherited_shorthand_property_ids.append(name);
  139. } else {
  140. if (inherited)
  141. inherited_longhand_property_ids.append(name);
  142. else
  143. noninherited_longhand_property_ids.append(name);
  144. }
  145. });
  146. // Section order:
  147. // 1. inherited shorthand properties
  148. // 2. noninherited shorthand properties
  149. // 3. inherited longhand properties
  150. // 4. noninherited longhand properties
  151. auto first_property_id = inherited_shorthand_property_ids.first();
  152. auto last_property_id = noninherited_longhand_property_ids.last();
  153. auto emit_properties = [&](auto& property_ids) {
  154. for (auto& name : property_ids) {
  155. auto member_generator = generator.fork();
  156. member_generator.set("name:titlecase", title_casify(name));
  157. member_generator.append(R"~~~(
  158. @name:titlecase@,
  159. )~~~");
  160. }
  161. };
  162. emit_properties(inherited_shorthand_property_ids);
  163. emit_properties(noninherited_shorthand_property_ids);
  164. emit_properties(inherited_longhand_property_ids);
  165. emit_properties(noninherited_longhand_property_ids);
  166. generator.set("first_property_id", title_casify(first_property_id));
  167. generator.set("last_property_id", title_casify(last_property_id));
  168. generator.set("first_longhand_property_id", title_casify(inherited_longhand_property_ids.first()));
  169. generator.set("last_longhand_property_id", title_casify(noninherited_longhand_property_ids.last()));
  170. generator.set("first_inherited_shorthand_property_id", title_casify(inherited_shorthand_property_ids.first()));
  171. generator.set("last_inherited_shorthand_property_id", title_casify(inherited_shorthand_property_ids.last()));
  172. generator.set("first_inherited_longhand_property_id", title_casify(inherited_longhand_property_ids.first()));
  173. generator.set("last_inherited_longhand_property_id", title_casify(inherited_longhand_property_ids.last()));
  174. generator.append(R"~~~(
  175. };
  176. enum class AnimationType {
  177. Discrete,
  178. ByComputedValue,
  179. RepeatableList,
  180. Custom,
  181. None,
  182. };
  183. AnimationType animation_type_from_longhand_property(PropertyID);
  184. bool is_animatable_property(PropertyID);
  185. Optional<PropertyID> property_id_from_camel_case_string(StringView);
  186. Optional<PropertyID> property_id_from_string(StringView);
  187. [[nodiscard]] FlyString const& string_from_property_id(PropertyID);
  188. [[nodiscard]] FlyString const& camel_case_string_from_property_id(PropertyID);
  189. bool is_inherited_property(PropertyID);
  190. NonnullRefPtr<CSSStyleValue> property_initial_value(Optional<JS::Realm&>, PropertyID);
  191. enum class ValueType {
  192. Angle,
  193. BackgroundPosition,
  194. BasicShape,
  195. Color,
  196. Counter,
  197. CustomIdent,
  198. EasingFunction,
  199. FilterValueList,
  200. Flex,
  201. Frequency,
  202. Image,
  203. Integer,
  204. Length,
  205. Number,
  206. OpenTypeTag,
  207. Paint,
  208. Percentage,
  209. Position,
  210. Ratio,
  211. Rect,
  212. Resolution,
  213. String,
  214. Time,
  215. Url,
  216. };
  217. bool property_accepts_type(PropertyID, ValueType);
  218. bool property_accepts_keyword(PropertyID, Keyword);
  219. Optional<ValueType> property_resolves_percentages_relative_to(PropertyID);
  220. // These perform range-checking, but are also safe to call with properties that don't accept that type. (They'll just return false.)
  221. bool property_accepts_angle(PropertyID, Angle const&);
  222. bool property_accepts_flex(PropertyID, Flex const&);
  223. bool property_accepts_frequency(PropertyID, Frequency const&);
  224. bool property_accepts_integer(PropertyID, i64 const&);
  225. bool property_accepts_length(PropertyID, Length const&);
  226. bool property_accepts_number(PropertyID, double const&);
  227. bool property_accepts_percentage(PropertyID, Percentage const&);
  228. bool property_accepts_resolution(PropertyID, Resolution const&);
  229. bool property_accepts_time(PropertyID, Time const&);
  230. bool property_is_shorthand(PropertyID);
  231. Vector<PropertyID> longhands_for_shorthand(PropertyID);
  232. size_t property_maximum_value_count(PropertyID);
  233. bool property_affects_layout(PropertyID);
  234. bool property_affects_stacking_context(PropertyID);
  235. constexpr PropertyID first_property_id = PropertyID::@first_property_id@;
  236. constexpr PropertyID last_property_id = PropertyID::@last_property_id@;
  237. constexpr PropertyID first_inherited_shorthand_property_id = PropertyID::@first_inherited_shorthand_property_id@;
  238. constexpr PropertyID last_inherited_shorthand_property_id = PropertyID::@last_inherited_shorthand_property_id@;
  239. constexpr PropertyID first_inherited_longhand_property_id = PropertyID::@first_inherited_longhand_property_id@;
  240. constexpr PropertyID last_inherited_longhand_property_id = PropertyID::@last_inherited_longhand_property_id@;
  241. constexpr PropertyID first_longhand_property_id = PropertyID::@first_longhand_property_id@;
  242. constexpr PropertyID last_longhand_property_id = PropertyID::@last_longhand_property_id@;
  243. enum class Quirk {
  244. // https://quirks.spec.whatwg.org/#the-hashless-hex-color-quirk
  245. HashlessHexColor,
  246. // https://quirks.spec.whatwg.org/#the-unitless-length-quirk
  247. UnitlessLength,
  248. };
  249. bool property_has_quirk(PropertyID, Quirk);
  250. } // namespace Web::CSS
  251. namespace AK {
  252. template<>
  253. struct Traits<Web::CSS::PropertyID> : public DefaultTraits<Web::CSS::PropertyID> {
  254. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  255. };
  256. } // namespace AK
  257. )~~~");
  258. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  259. return {};
  260. }
  261. 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)
  262. {
  263. auto generator = parent_generator.fork();
  264. generator.set("css_type_name", css_type_name);
  265. generator.set("type_name", type_name);
  266. generator.append(R"~~~(
  267. bool property_accepts_@css_type_name@(PropertyID property_id, [[maybe_unused]] @type_name@ const& value)
  268. {
  269. switch (property_id) {
  270. )~~~");
  271. properties.for_each_member([&](auto& name, JsonValue const& value) -> void {
  272. VERIFY(value.is_object());
  273. if (is_legacy_alias(value.as_object()))
  274. return;
  275. if (auto maybe_valid_types = value.as_object().get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  276. for (auto valid_type : maybe_valid_types->values()) {
  277. auto type_and_range = valid_type.as_string().split_view(' ');
  278. if (type_and_range.first() != css_type_name)
  279. continue;
  280. auto property_generator = generator.fork();
  281. property_generator.set("property_name:titlecase", title_casify(name));
  282. property_generator.append(R"~~~(
  283. case PropertyID::@property_name:titlecase@:
  284. return )~~~");
  285. if (type_and_range.size() > 1) {
  286. auto range = type_and_range[1];
  287. VERIFY(range.starts_with('[') && range.ends_with(']') && range.contains(','));
  288. auto comma_index = range.find(',').value();
  289. StringView min_value_string = range.substring_view(1, comma_index - 1);
  290. StringView max_value_string = range.substring_view(comma_index + 1, range.length() - comma_index - 2);
  291. // If the min/max value is infinite, we can just skip that side of the check.
  292. if (min_value_string == "-∞")
  293. min_value_string = {};
  294. if (max_value_string == "∞")
  295. max_value_string = {};
  296. if (min_value_string.is_empty() && max_value_string.is_empty()) {
  297. property_generator.appendln("true;");
  298. break;
  299. }
  300. auto output_check = [&](auto& value_string, StringView comparator) {
  301. if (value_getter.has_value()) {
  302. property_generator.set("value_number", value_string);
  303. property_generator.set("value_getter", value_getter.value());
  304. property_generator.set("comparator", comparator);
  305. property_generator.append("@value_getter@ @comparator@ @value_number@");
  306. return;
  307. }
  308. GenericLexer lexer { value_string };
  309. auto value_number = lexer.consume_until(is_ascii_alpha);
  310. auto value_unit = lexer.consume_while(is_ascii_alpha);
  311. if (value_unit.is_empty())
  312. value_unit = default_unit_name.value();
  313. VERIFY(lexer.is_eof());
  314. property_generator.set("value_number", value_number);
  315. property_generator.set("value_unit", title_casify(value_unit));
  316. property_generator.set("comparator", comparator);
  317. property_generator.append("value @comparator@ @type_name@(@value_number@, @type_name@::Type::@value_unit@)");
  318. };
  319. if (!min_value_string.is_empty())
  320. output_check(min_value_string, ">="sv);
  321. if (!min_value_string.is_empty() && !max_value_string.is_empty())
  322. property_generator.append(" && ");
  323. if (!max_value_string.is_empty())
  324. output_check(max_value_string, "<="sv);
  325. property_generator.appendln(";");
  326. } else {
  327. property_generator.appendln("true;");
  328. }
  329. break;
  330. }
  331. }
  332. });
  333. generator.append(R"~~~(
  334. default:
  335. return false;
  336. }
  337. }
  338. )~~~");
  339. }
  340. ErrorOr<void> generate_implementation_file(JsonObject& properties, Core::File& file)
  341. {
  342. StringBuilder builder;
  343. SourceGenerator generator { builder };
  344. generator.append(R"~~~(
  345. #include <AK/Assertions.h>
  346. #include <LibWeb/CSS/Enums.h>
  347. #include <LibWeb/CSS/Parser/Parser.h>
  348. #include <LibWeb/CSS/PropertyID.h>
  349. #include <LibWeb/CSS/PropertyName.h>
  350. #include <LibWeb/CSS/CSSStyleValue.h>
  351. #include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
  352. #include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
  353. #include <LibWeb/Infra/Strings.h>
  354. namespace Web::CSS {
  355. Optional<PropertyID> property_id_from_camel_case_string(StringView string)
  356. {
  357. )~~~");
  358. properties.for_each_member([&](auto& name, auto& value) {
  359. VERIFY(value.is_object());
  360. auto member_generator = generator.fork();
  361. member_generator.set("name", name);
  362. member_generator.set("name:camelcase", camel_casify(name));
  363. if (auto legacy_alias_for = value.as_object().get_byte_string("legacy-alias-for"sv); legacy_alias_for.has_value()) {
  364. member_generator.set("name:titlecase", title_casify(legacy_alias_for.value()));
  365. } else {
  366. member_generator.set("name:titlecase", title_casify(name));
  367. }
  368. member_generator.append(R"~~~(
  369. if (string.equals_ignoring_ascii_case("@name:camelcase@"sv))
  370. return PropertyID::@name:titlecase@;
  371. )~~~");
  372. });
  373. generator.append(R"~~~(
  374. return {};
  375. }
  376. Optional<PropertyID> property_id_from_string(StringView string)
  377. {
  378. if (is_a_custom_property_name_string(string))
  379. return PropertyID::Custom;
  380. if (Infra::is_ascii_case_insensitive_match(string, "all"sv))
  381. return PropertyID::All;
  382. )~~~");
  383. properties.for_each_member([&](auto& name, auto& value) {
  384. VERIFY(value.is_object());
  385. auto member_generator = generator.fork();
  386. member_generator.set("name", name);
  387. if (auto legacy_alias_for = value.as_object().get_byte_string("legacy-alias-for"sv); legacy_alias_for.has_value()) {
  388. member_generator.set("name:titlecase", title_casify(legacy_alias_for.value()));
  389. } else {
  390. member_generator.set("name:titlecase", title_casify(name));
  391. }
  392. member_generator.append(R"~~~(
  393. if (Infra::is_ascii_case_insensitive_match(string, "@name@"sv))
  394. return PropertyID::@name:titlecase@;
  395. )~~~");
  396. });
  397. generator.append(R"~~~(
  398. return {};
  399. }
  400. FlyString const& string_from_property_id(PropertyID property_id) {
  401. switch (property_id) {
  402. )~~~");
  403. properties.for_each_member([&](auto& name, auto& value) {
  404. VERIFY(value.is_object());
  405. if (is_legacy_alias(value.as_object()))
  406. return;
  407. auto member_generator = generator.fork();
  408. member_generator.set("name", name);
  409. member_generator.set("name:titlecase", title_casify(name));
  410. member_generator.append(R"~~~(
  411. case PropertyID::@name:titlecase@: {
  412. static FlyString name = "@name@"_fly_string;
  413. return name;
  414. }
  415. )~~~");
  416. });
  417. generator.append(R"~~~(
  418. default: {
  419. static FlyString invalid_property_id_string = "(invalid CSS::PropertyID)"_fly_string;
  420. return invalid_property_id_string;
  421. }
  422. }
  423. }
  424. FlyString const& camel_case_string_from_property_id(PropertyID property_id) {
  425. switch (property_id) {
  426. )~~~");
  427. properties.for_each_member([&](auto& name, auto& value) {
  428. VERIFY(value.is_object());
  429. if (is_legacy_alias(value.as_object()))
  430. return;
  431. auto member_generator = generator.fork();
  432. member_generator.set("name", name);
  433. member_generator.set("name:titlecase", title_casify(name));
  434. member_generator.set("name:camelcase", camel_casify(name));
  435. member_generator.append(R"~~~(
  436. case PropertyID::@name:titlecase@: {
  437. static FlyString name = "@name:camelcase@"_fly_string;
  438. return name;
  439. }
  440. )~~~");
  441. });
  442. generator.append(R"~~~(
  443. default: {
  444. static FlyString invalid_property_id_string = "(invalid CSS::PropertyID)"_fly_string;
  445. return invalid_property_id_string;
  446. }
  447. }
  448. }
  449. AnimationType animation_type_from_longhand_property(PropertyID property_id)
  450. {
  451. switch (property_id) {
  452. )~~~");
  453. properties.for_each_member([&](auto& name, auto& value) {
  454. VERIFY(value.is_object());
  455. if (is_legacy_alias(value.as_object()))
  456. return;
  457. auto member_generator = generator.fork();
  458. member_generator.set("name:titlecase", title_casify(name));
  459. // Shorthand properties should have already been expanded before calling into this function
  460. if (value.as_object().has("longhands"sv)) {
  461. if (value.as_object().has("animation-type"sv)) {
  462. dbgln("Property '{}' with longhands cannot specify 'animation-type'", name);
  463. VERIFY_NOT_REACHED();
  464. }
  465. member_generator.append(R"~~~(
  466. case PropertyID::@name:titlecase@:
  467. VERIFY_NOT_REACHED();
  468. )~~~");
  469. return;
  470. }
  471. if (!value.as_object().has("animation-type"sv)) {
  472. dbgln("No animation-type specified for property '{}'", name);
  473. VERIFY_NOT_REACHED();
  474. }
  475. auto animation_type = value.as_object().get_byte_string("animation-type"sv).value();
  476. member_generator.set("value", title_casify(animation_type));
  477. member_generator.append(R"~~~(
  478. case PropertyID::@name:titlecase@:
  479. return AnimationType::@value@;
  480. )~~~");
  481. });
  482. generator.append(R"~~~(
  483. default:
  484. return AnimationType::None;
  485. }
  486. }
  487. bool is_animatable_property(PropertyID property_id)
  488. {
  489. switch (property_id) {
  490. )~~~");
  491. properties.for_each_member([&](auto& name, auto& value) {
  492. VERIFY(value.is_object());
  493. if (is_legacy_alias(value.as_object()))
  494. return;
  495. if (is_animatable_property(properties, name)) {
  496. auto member_generator = generator.fork();
  497. member_generator.set("name:titlecase", title_casify(name));
  498. member_generator.append(R"~~~(
  499. case PropertyID::@name:titlecase@:
  500. )~~~");
  501. }
  502. });
  503. generator.append(R"~~~(
  504. return true;
  505. default:
  506. return false;
  507. }
  508. }
  509. bool is_inherited_property(PropertyID property_id)
  510. {
  511. if (property_id >= first_inherited_shorthand_property_id && property_id <= last_inherited_longhand_property_id)
  512. return true;
  513. if (property_id >= first_inherited_longhand_property_id && property_id <= last_inherited_longhand_property_id)
  514. return true;
  515. return false;
  516. }
  517. bool property_affects_layout(PropertyID property_id)
  518. {
  519. switch (property_id) {
  520. )~~~");
  521. properties.for_each_member([&](auto& name, auto& value) {
  522. VERIFY(value.is_object());
  523. if (is_legacy_alias(value.as_object()))
  524. return;
  525. bool affects_layout = true;
  526. if (value.as_object().has("affects-layout"sv))
  527. affects_layout = value.as_object().get_bool("affects-layout"sv).value_or(false);
  528. if (affects_layout) {
  529. auto member_generator = generator.fork();
  530. member_generator.set("name:titlecase", title_casify(name));
  531. member_generator.append(R"~~~(
  532. case PropertyID::@name:titlecase@:
  533. )~~~");
  534. }
  535. });
  536. generator.append(R"~~~(
  537. return true;
  538. default:
  539. return false;
  540. }
  541. }
  542. bool property_affects_stacking_context(PropertyID property_id)
  543. {
  544. switch (property_id) {
  545. )~~~");
  546. properties.for_each_member([&](auto& name, auto& value) {
  547. VERIFY(value.is_object());
  548. if (is_legacy_alias(value.as_object()))
  549. return;
  550. bool affects_stacking_context = false;
  551. if (value.as_object().has("affects-stacking-context"sv))
  552. affects_stacking_context = value.as_object().get_bool("affects-stacking-context"sv).value_or(false);
  553. if (affects_stacking_context) {
  554. auto member_generator = generator.fork();
  555. member_generator.set("name:titlecase", title_casify(name));
  556. member_generator.append(R"~~~(
  557. case PropertyID::@name:titlecase@:
  558. )~~~");
  559. }
  560. });
  561. generator.append(R"~~~(
  562. return true;
  563. default:
  564. return false;
  565. }
  566. }
  567. NonnullRefPtr<CSSStyleValue> property_initial_value(Optional<JS::Realm&> context_realm, PropertyID property_id)
  568. {
  569. static Array<RefPtr<CSSStyleValue>, to_underlying(last_property_id) + 1> initial_values;
  570. if (auto initial_value = initial_values[to_underlying(property_id)])
  571. return initial_value.release_nonnull();
  572. // We need a Realm to parse any new values.
  573. VERIFY(context_realm.has_value());
  574. // Lazily parse initial values as needed.
  575. // This ensures the shorthands will always be able to get the initial values of their longhands.
  576. // This also now allows a longhand have its own longhand (like background-position-x).
  577. Parser::ParsingContext parsing_context(context_realm.value());
  578. switch (property_id) {
  579. )~~~");
  580. auto output_initial_value_code = [&](auto& name, auto& object) {
  581. if (!object.has("initial"sv)) {
  582. dbgln("No initial value specified for property '{}'", name);
  583. VERIFY_NOT_REACHED();
  584. }
  585. auto initial_value = object.get_byte_string("initial"sv);
  586. VERIFY(initial_value.has_value());
  587. auto& initial_value_string = initial_value.value();
  588. auto member_generator = generator.fork();
  589. member_generator.set("name:titlecase", title_casify(name));
  590. member_generator.set("initial_value_string", initial_value_string);
  591. member_generator.append(
  592. R"~~~( case PropertyID::@name:titlecase@:
  593. {
  594. auto parsed_value = parse_css_value(parsing_context, "@initial_value_string@"sv, PropertyID::@name:titlecase@);
  595. VERIFY(!parsed_value.is_null());
  596. auto initial_value = parsed_value.release_nonnull();
  597. initial_values[to_underlying(PropertyID::@name:titlecase@)] = initial_value;
  598. return initial_value;
  599. }
  600. )~~~");
  601. };
  602. properties.for_each_member([&](auto& name, auto& value) {
  603. VERIFY(value.is_object());
  604. if (is_legacy_alias(value.as_object()))
  605. return;
  606. output_initial_value_code(name, value.as_object());
  607. });
  608. generator.append(
  609. R"~~~( default: VERIFY_NOT_REACHED();
  610. }
  611. VERIFY_NOT_REACHED();
  612. }
  613. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  614. {
  615. switch (property_id) {
  616. )~~~");
  617. properties.for_each_member([&](auto& name, auto& value) {
  618. VERIFY(value.is_object());
  619. if (is_legacy_alias(value.as_object()))
  620. return;
  621. if (value.as_object().has("quirks"sv)) {
  622. auto quirks_value = value.as_object().get_array("quirks"sv);
  623. VERIFY(quirks_value.has_value());
  624. auto& quirks = quirks_value.value();
  625. if (!quirks.is_empty()) {
  626. auto property_generator = generator.fork();
  627. property_generator.set("name:titlecase", title_casify(name));
  628. property_generator.append(R"~~~(
  629. case PropertyID::@name:titlecase@: {
  630. switch (quirk) {
  631. )~~~");
  632. for (auto& quirk : quirks.values()) {
  633. VERIFY(quirk.is_string());
  634. auto quirk_generator = property_generator.fork();
  635. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  636. quirk_generator.append(R"~~~(
  637. case Quirk::@quirk:titlecase@:
  638. return true;
  639. )~~~");
  640. }
  641. property_generator.append(R"~~~(
  642. default:
  643. return false;
  644. }
  645. }
  646. )~~~");
  647. }
  648. }
  649. });
  650. generator.append(R"~~~(
  651. default:
  652. return false;
  653. }
  654. }
  655. bool property_accepts_type(PropertyID property_id, ValueType value_type)
  656. {
  657. switch (property_id) {
  658. )~~~");
  659. properties.for_each_member([&](auto& name, auto& value) {
  660. VERIFY(value.is_object());
  661. auto& object = value.as_object();
  662. if (is_legacy_alias(object))
  663. return;
  664. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  665. auto& valid_types = maybe_valid_types.value();
  666. auto property_generator = generator.fork();
  667. property_generator.set("name:titlecase", title_casify(name));
  668. property_generator.append(R"~~~(
  669. case PropertyID::@name:titlecase@: {
  670. switch (value_type) {
  671. )~~~");
  672. bool did_output_accepted_type = false;
  673. for (auto& type : valid_types.values()) {
  674. VERIFY(type.is_string());
  675. auto type_name = type.as_string().split_view(' ').first();
  676. if (type_name_is_enum(type_name))
  677. continue;
  678. if (type_name == "angle") {
  679. property_generator.appendln(" case ValueType::Angle:");
  680. } else if (type_name == "background-position") {
  681. property_generator.appendln(" case ValueType::BackgroundPosition:");
  682. } else if (type_name == "basic-shape") {
  683. property_generator.appendln(" case ValueType::BasicShape:");
  684. } else if (type_name == "color") {
  685. property_generator.appendln(" case ValueType::Color:");
  686. } else if (type_name == "counter") {
  687. property_generator.appendln(" case ValueType::Counter:");
  688. } else if (type_name == "custom-ident") {
  689. property_generator.appendln(" case ValueType::CustomIdent:");
  690. } else if (type_name == "easing-function") {
  691. property_generator.appendln(" case ValueType::EasingFunction:");
  692. } else if (type_name == "flex") {
  693. property_generator.appendln(" case ValueType::Flex:");
  694. } else if (type_name == "frequency") {
  695. property_generator.appendln(" case ValueType::Frequency:");
  696. } else if (type_name == "image") {
  697. property_generator.appendln(" case ValueType::Image:");
  698. } else if (type_name == "integer") {
  699. property_generator.appendln(" case ValueType::Integer:");
  700. } else if (type_name == "length") {
  701. property_generator.appendln(" case ValueType::Length:");
  702. } else if (type_name == "number") {
  703. property_generator.appendln(" case ValueType::Number:");
  704. } else if (type_name == "opentype-tag") {
  705. property_generator.appendln(" case ValueType::OpenTypeTag:");
  706. } else if (type_name == "paint") {
  707. property_generator.appendln(" case ValueType::Paint:");
  708. } else if (type_name == "percentage") {
  709. property_generator.appendln(" case ValueType::Percentage:");
  710. } else if (type_name == "position") {
  711. property_generator.appendln(" case ValueType::Position:");
  712. } else if (type_name == "ratio") {
  713. property_generator.appendln(" case ValueType::Ratio:");
  714. } else if (type_name == "rect") {
  715. property_generator.appendln(" case ValueType::Rect:");
  716. } else if (type_name == "resolution") {
  717. property_generator.appendln(" case ValueType::Resolution:");
  718. } else if (type_name == "string") {
  719. property_generator.appendln(" case ValueType::String:");
  720. } else if (type_name == "time") {
  721. property_generator.appendln(" case ValueType::Time:");
  722. } else if (type_name == "url") {
  723. property_generator.appendln(" case ValueType::Url:");
  724. } else {
  725. VERIFY_NOT_REACHED();
  726. }
  727. did_output_accepted_type = true;
  728. }
  729. if (did_output_accepted_type)
  730. property_generator.appendln(" return true;");
  731. property_generator.append(R"~~~(
  732. default:
  733. return false;
  734. }
  735. }
  736. )~~~");
  737. }
  738. });
  739. generator.append(R"~~~(
  740. default:
  741. return false;
  742. }
  743. }
  744. bool property_accepts_keyword(PropertyID property_id, Keyword keyword)
  745. {
  746. switch (property_id) {
  747. )~~~");
  748. properties.for_each_member([&](auto& name, auto& value) {
  749. VERIFY(value.is_object());
  750. auto& object = value.as_object();
  751. if (is_legacy_alias(object))
  752. return;
  753. auto property_generator = generator.fork();
  754. property_generator.set("name:titlecase", title_casify(name));
  755. property_generator.appendln(" case PropertyID::@name:titlecase@: {");
  756. if (auto maybe_valid_identifiers = object.get_array("valid-identifiers"sv); maybe_valid_identifiers.has_value() && !maybe_valid_identifiers->is_empty()) {
  757. property_generator.appendln(" switch (keyword) {");
  758. auto& valid_identifiers = maybe_valid_identifiers.value();
  759. for (auto& keyword : valid_identifiers.values()) {
  760. auto keyword_generator = generator.fork();
  761. keyword_generator.set("keyword:titlecase", title_casify(keyword.as_string()));
  762. keyword_generator.appendln(" case Keyword::@keyword:titlecase@:");
  763. }
  764. property_generator.append(R"~~~(
  765. return true;
  766. default:
  767. break;
  768. }
  769. )~~~");
  770. }
  771. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  772. auto& valid_types = maybe_valid_types.value();
  773. for (auto& valid_type : valid_types.values()) {
  774. auto type_name = valid_type.as_string().split_view(' ').first();
  775. if (!type_name_is_enum(type_name))
  776. continue;
  777. auto type_generator = generator.fork();
  778. type_generator.set("type_name:snakecase", snake_casify(type_name));
  779. type_generator.append(R"~~~(
  780. if (keyword_to_@type_name:snakecase@(keyword).has_value())
  781. return true;
  782. )~~~");
  783. }
  784. }
  785. property_generator.append(R"~~~(
  786. return false;
  787. }
  788. )~~~");
  789. });
  790. generator.append(R"~~~(
  791. default:
  792. return false;
  793. }
  794. }
  795. Optional<ValueType> property_resolves_percentages_relative_to(PropertyID property_id)
  796. {
  797. switch (property_id) {
  798. )~~~");
  799. properties.for_each_member([&](auto& name, auto& value) {
  800. VERIFY(value.is_object());
  801. if (is_legacy_alias(value.as_object()))
  802. return;
  803. if (auto resolved_type = value.as_object().get_byte_string("percentages-resolve-to"sv); resolved_type.has_value()) {
  804. auto property_generator = generator.fork();
  805. property_generator.set("name:titlecase", title_casify(name));
  806. property_generator.set("resolved_type:titlecase", title_casify(resolved_type.value()));
  807. property_generator.append(R"~~~(
  808. case PropertyID::@name:titlecase@:
  809. return ValueType::@resolved_type:titlecase@;
  810. )~~~");
  811. }
  812. });
  813. generator.append(R"~~~(
  814. default:
  815. return {};
  816. }
  817. }
  818. size_t property_maximum_value_count(PropertyID property_id)
  819. {
  820. switch (property_id) {
  821. )~~~");
  822. properties.for_each_member([&](auto& name, auto& value) {
  823. VERIFY(value.is_object());
  824. if (is_legacy_alias(value.as_object()))
  825. return;
  826. if (value.as_object().has("max-values"sv)) {
  827. JsonValue max_values = value.as_object().get("max-values"sv).release_value();
  828. VERIFY(max_values.is_integer<size_t>());
  829. auto property_generator = generator.fork();
  830. property_generator.set("name:titlecase", title_casify(name));
  831. property_generator.set("max_values", MUST(String::formatted("{}", max_values.as_integer<size_t>())));
  832. property_generator.append(R"~~~(
  833. case PropertyID::@name:titlecase@:
  834. return @max_values@;
  835. )~~~");
  836. }
  837. });
  838. generator.append(R"~~~(
  839. default:
  840. return 1;
  841. }
  842. })~~~");
  843. generate_bounds_checking_function(properties, generator, "angle"sv, "Angle"sv, "Deg"sv);
  844. generate_bounds_checking_function(properties, generator, "flex"sv, "Flex"sv, "Fr"sv);
  845. generate_bounds_checking_function(properties, generator, "frequency"sv, "Frequency"sv, "Hertz"sv);
  846. generate_bounds_checking_function(properties, generator, "integer"sv, "i64"sv, {}, "value"sv);
  847. generate_bounds_checking_function(properties, generator, "length"sv, "Length"sv, {}, "value.raw_value()"sv);
  848. generate_bounds_checking_function(properties, generator, "number"sv, "double"sv, {}, "value"sv);
  849. generate_bounds_checking_function(properties, generator, "percentage"sv, "Percentage"sv, {}, "value.value()"sv);
  850. generate_bounds_checking_function(properties, generator, "resolution"sv, "Resolution"sv, "Dpi"sv);
  851. generate_bounds_checking_function(properties, generator, "time"sv, "Time"sv, "S"sv);
  852. generator.append(R"~~~(
  853. bool property_is_shorthand(PropertyID property_id)
  854. {
  855. switch (property_id) {
  856. )~~~");
  857. properties.for_each_member([&](auto& name, auto& value) {
  858. if (is_legacy_alias(value.as_object()))
  859. return;
  860. if (value.as_object().has("longhands"sv)) {
  861. auto property_generator = generator.fork();
  862. property_generator.set("name:titlecase", title_casify(name));
  863. property_generator.append(R"~~~(
  864. case PropertyID::@name:titlecase@:
  865. )~~~");
  866. }
  867. });
  868. generator.append(R"~~~(
  869. return true;
  870. default:
  871. return false;
  872. }
  873. }
  874. )~~~");
  875. generator.append(R"~~~(
  876. Vector<PropertyID> longhands_for_shorthand(PropertyID property_id)
  877. {
  878. switch (property_id) {
  879. )~~~");
  880. properties.for_each_member([&](auto& name, auto& value) {
  881. if (is_legacy_alias(value.as_object()))
  882. return;
  883. if (value.as_object().has("longhands"sv)) {
  884. auto longhands = value.as_object().get("longhands"sv);
  885. VERIFY(longhands.has_value() && longhands->is_array());
  886. auto longhand_values = longhands->as_array();
  887. auto property_generator = generator.fork();
  888. property_generator.set("name:titlecase", title_casify(name));
  889. StringBuilder builder;
  890. bool first = true;
  891. longhand_values.for_each([&](auto& longhand) {
  892. if (first)
  893. first = false;
  894. else
  895. builder.append(", "sv);
  896. builder.appendff("PropertyID::{}", title_casify(longhand.as_string()));
  897. return IterationDecision::Continue;
  898. });
  899. property_generator.set("longhands", builder.to_byte_string());
  900. property_generator.append(R"~~~(
  901. case PropertyID::@name:titlecase@:
  902. return { @longhands@ };
  903. )~~~");
  904. }
  905. });
  906. generator.append(R"~~~(
  907. default:
  908. return { };
  909. }
  910. }
  911. )~~~");
  912. generator.append(R"~~~(
  913. } // namespace Web::CSS
  914. )~~~");
  915. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  916. return {};
  917. }
  918. bool is_animatable_property(JsonObject& properties, StringView property_name)
  919. {
  920. auto property = properties.get_object(property_name);
  921. VERIFY(property.has_value());
  922. if (auto animation_type = property.value().get_byte_string("animation-type"sv); animation_type.has_value()) {
  923. return animation_type != "none";
  924. }
  925. if (!property.value().has("longhands"sv)) {
  926. dbgln("Property '{}' must specify either 'animation-type' or 'longhands'"sv, property_name);
  927. VERIFY_NOT_REACHED();
  928. }
  929. auto longhands = property.value().get_array("longhands"sv);
  930. VERIFY(longhands.has_value());
  931. for (auto const& subproperty_name : longhands->values()) {
  932. VERIFY(subproperty_name.is_string());
  933. if (is_animatable_property(properties, subproperty_name.as_string()))
  934. return true;
  935. }
  936. return false;
  937. }