GenerateCSSPropertyID.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. VERIFY(!name.is_empty() && !is_ascii_digit(name[0])); // Ensure `PropertyKey`s are not Numbers.
  494. if (is_legacy_alias(value.as_object()))
  495. return;
  496. if (is_animatable_property(properties, name)) {
  497. auto member_generator = generator.fork();
  498. member_generator.set("name:titlecase", title_casify(name));
  499. member_generator.append(R"~~~(
  500. case PropertyID::@name:titlecase@:
  501. )~~~");
  502. }
  503. });
  504. generator.append(R"~~~(
  505. return true;
  506. default:
  507. return false;
  508. }
  509. }
  510. bool is_inherited_property(PropertyID property_id)
  511. {
  512. if (property_id >= first_inherited_shorthand_property_id && property_id <= last_inherited_longhand_property_id)
  513. return true;
  514. if (property_id >= first_inherited_longhand_property_id && property_id <= last_inherited_longhand_property_id)
  515. return true;
  516. return false;
  517. }
  518. bool property_affects_layout(PropertyID property_id)
  519. {
  520. switch (property_id) {
  521. )~~~");
  522. properties.for_each_member([&](auto& name, auto& value) {
  523. VERIFY(value.is_object());
  524. if (is_legacy_alias(value.as_object()))
  525. return;
  526. bool affects_layout = true;
  527. if (value.as_object().has("affects-layout"sv))
  528. affects_layout = value.as_object().get_bool("affects-layout"sv).value_or(false);
  529. if (affects_layout) {
  530. auto member_generator = generator.fork();
  531. member_generator.set("name:titlecase", title_casify(name));
  532. member_generator.append(R"~~~(
  533. case PropertyID::@name:titlecase@:
  534. )~~~");
  535. }
  536. });
  537. generator.append(R"~~~(
  538. return true;
  539. default:
  540. return false;
  541. }
  542. }
  543. bool property_affects_stacking_context(PropertyID property_id)
  544. {
  545. switch (property_id) {
  546. )~~~");
  547. properties.for_each_member([&](auto& name, auto& value) {
  548. VERIFY(value.is_object());
  549. if (is_legacy_alias(value.as_object()))
  550. return;
  551. bool affects_stacking_context = false;
  552. if (value.as_object().has("affects-stacking-context"sv))
  553. affects_stacking_context = value.as_object().get_bool("affects-stacking-context"sv).value_or(false);
  554. if (affects_stacking_context) {
  555. auto member_generator = generator.fork();
  556. member_generator.set("name:titlecase", title_casify(name));
  557. member_generator.append(R"~~~(
  558. case PropertyID::@name:titlecase@:
  559. )~~~");
  560. }
  561. });
  562. generator.append(R"~~~(
  563. return true;
  564. default:
  565. return false;
  566. }
  567. }
  568. NonnullRefPtr<CSSStyleValue> property_initial_value(Optional<JS::Realm&> context_realm, PropertyID property_id)
  569. {
  570. static Array<RefPtr<CSSStyleValue>, to_underlying(last_property_id) + 1> initial_values;
  571. if (auto initial_value = initial_values[to_underlying(property_id)])
  572. return initial_value.release_nonnull();
  573. // We need a Realm to parse any new values.
  574. VERIFY(context_realm.has_value());
  575. // Lazily parse initial values as needed.
  576. // This ensures the shorthands will always be able to get the initial values of their longhands.
  577. // This also now allows a longhand have its own longhand (like background-position-x).
  578. Parser::ParsingContext parsing_context(context_realm.value());
  579. switch (property_id) {
  580. )~~~");
  581. auto output_initial_value_code = [&](auto& name, auto& object) {
  582. if (!object.has("initial"sv)) {
  583. dbgln("No initial value specified for property '{}'", name);
  584. VERIFY_NOT_REACHED();
  585. }
  586. auto initial_value = object.get_byte_string("initial"sv);
  587. VERIFY(initial_value.has_value());
  588. auto& initial_value_string = initial_value.value();
  589. auto member_generator = generator.fork();
  590. member_generator.set("name:titlecase", title_casify(name));
  591. member_generator.set("initial_value_string", initial_value_string);
  592. member_generator.append(
  593. R"~~~( case PropertyID::@name:titlecase@:
  594. {
  595. auto parsed_value = parse_css_value(parsing_context, "@initial_value_string@"sv, PropertyID::@name:titlecase@);
  596. VERIFY(!parsed_value.is_null());
  597. auto initial_value = parsed_value.release_nonnull();
  598. initial_values[to_underlying(PropertyID::@name:titlecase@)] = initial_value;
  599. return initial_value;
  600. }
  601. )~~~");
  602. };
  603. properties.for_each_member([&](auto& name, auto& value) {
  604. VERIFY(value.is_object());
  605. if (is_legacy_alias(value.as_object()))
  606. return;
  607. output_initial_value_code(name, value.as_object());
  608. });
  609. generator.append(
  610. R"~~~( default: VERIFY_NOT_REACHED();
  611. }
  612. VERIFY_NOT_REACHED();
  613. }
  614. bool property_has_quirk(PropertyID property_id, Quirk quirk)
  615. {
  616. switch (property_id) {
  617. )~~~");
  618. properties.for_each_member([&](auto& name, auto& value) {
  619. VERIFY(value.is_object());
  620. if (is_legacy_alias(value.as_object()))
  621. return;
  622. if (value.as_object().has("quirks"sv)) {
  623. auto quirks_value = value.as_object().get_array("quirks"sv);
  624. VERIFY(quirks_value.has_value());
  625. auto& quirks = quirks_value.value();
  626. if (!quirks.is_empty()) {
  627. auto property_generator = generator.fork();
  628. property_generator.set("name:titlecase", title_casify(name));
  629. property_generator.append(R"~~~(
  630. case PropertyID::@name:titlecase@: {
  631. switch (quirk) {
  632. )~~~");
  633. for (auto& quirk : quirks.values()) {
  634. VERIFY(quirk.is_string());
  635. auto quirk_generator = property_generator.fork();
  636. quirk_generator.set("quirk:titlecase", title_casify(quirk.as_string()));
  637. quirk_generator.append(R"~~~(
  638. case Quirk::@quirk:titlecase@:
  639. return true;
  640. )~~~");
  641. }
  642. property_generator.append(R"~~~(
  643. default:
  644. return false;
  645. }
  646. }
  647. )~~~");
  648. }
  649. }
  650. });
  651. generator.append(R"~~~(
  652. default:
  653. return false;
  654. }
  655. }
  656. bool property_accepts_type(PropertyID property_id, ValueType value_type)
  657. {
  658. switch (property_id) {
  659. )~~~");
  660. properties.for_each_member([&](auto& name, auto& value) {
  661. VERIFY(value.is_object());
  662. auto& object = value.as_object();
  663. if (is_legacy_alias(object))
  664. return;
  665. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  666. auto& valid_types = maybe_valid_types.value();
  667. auto property_generator = generator.fork();
  668. property_generator.set("name:titlecase", title_casify(name));
  669. property_generator.append(R"~~~(
  670. case PropertyID::@name:titlecase@: {
  671. switch (value_type) {
  672. )~~~");
  673. bool did_output_accepted_type = false;
  674. for (auto& type : valid_types.values()) {
  675. VERIFY(type.is_string());
  676. auto type_name = type.as_string().split_view(' ').first();
  677. if (type_name_is_enum(type_name))
  678. continue;
  679. if (type_name == "angle") {
  680. property_generator.appendln(" case ValueType::Angle:");
  681. } else if (type_name == "background-position") {
  682. property_generator.appendln(" case ValueType::BackgroundPosition:");
  683. } else if (type_name == "basic-shape") {
  684. property_generator.appendln(" case ValueType::BasicShape:");
  685. } else if (type_name == "color") {
  686. property_generator.appendln(" case ValueType::Color:");
  687. } else if (type_name == "counter") {
  688. property_generator.appendln(" case ValueType::Counter:");
  689. } else if (type_name == "custom-ident") {
  690. property_generator.appendln(" case ValueType::CustomIdent:");
  691. } else if (type_name == "easing-function") {
  692. property_generator.appendln(" case ValueType::EasingFunction:");
  693. } else if (type_name == "flex") {
  694. property_generator.appendln(" case ValueType::Flex:");
  695. } else if (type_name == "frequency") {
  696. property_generator.appendln(" case ValueType::Frequency:");
  697. } else if (type_name == "image") {
  698. property_generator.appendln(" case ValueType::Image:");
  699. } else if (type_name == "integer") {
  700. property_generator.appendln(" case ValueType::Integer:");
  701. } else if (type_name == "length") {
  702. property_generator.appendln(" case ValueType::Length:");
  703. } else if (type_name == "number") {
  704. property_generator.appendln(" case ValueType::Number:");
  705. } else if (type_name == "opentype-tag") {
  706. property_generator.appendln(" case ValueType::OpenTypeTag:");
  707. } else if (type_name == "paint") {
  708. property_generator.appendln(" case ValueType::Paint:");
  709. } else if (type_name == "percentage") {
  710. property_generator.appendln(" case ValueType::Percentage:");
  711. } else if (type_name == "position") {
  712. property_generator.appendln(" case ValueType::Position:");
  713. } else if (type_name == "ratio") {
  714. property_generator.appendln(" case ValueType::Ratio:");
  715. } else if (type_name == "rect") {
  716. property_generator.appendln(" case ValueType::Rect:");
  717. } else if (type_name == "resolution") {
  718. property_generator.appendln(" case ValueType::Resolution:");
  719. } else if (type_name == "string") {
  720. property_generator.appendln(" case ValueType::String:");
  721. } else if (type_name == "time") {
  722. property_generator.appendln(" case ValueType::Time:");
  723. } else if (type_name == "url") {
  724. property_generator.appendln(" case ValueType::Url:");
  725. } else {
  726. VERIFY_NOT_REACHED();
  727. }
  728. did_output_accepted_type = true;
  729. }
  730. if (did_output_accepted_type)
  731. property_generator.appendln(" return true;");
  732. property_generator.append(R"~~~(
  733. default:
  734. return false;
  735. }
  736. }
  737. )~~~");
  738. }
  739. });
  740. generator.append(R"~~~(
  741. default:
  742. return false;
  743. }
  744. }
  745. bool property_accepts_keyword(PropertyID property_id, Keyword keyword)
  746. {
  747. switch (property_id) {
  748. )~~~");
  749. properties.for_each_member([&](auto& name, auto& value) {
  750. VERIFY(value.is_object());
  751. auto& object = value.as_object();
  752. if (is_legacy_alias(object))
  753. return;
  754. auto property_generator = generator.fork();
  755. property_generator.set("name:titlecase", title_casify(name));
  756. property_generator.appendln(" case PropertyID::@name:titlecase@: {");
  757. if (auto maybe_valid_identifiers = object.get_array("valid-identifiers"sv); maybe_valid_identifiers.has_value() && !maybe_valid_identifiers->is_empty()) {
  758. property_generator.appendln(" switch (keyword) {");
  759. auto& valid_identifiers = maybe_valid_identifiers.value();
  760. for (auto& keyword : valid_identifiers.values()) {
  761. auto keyword_generator = generator.fork();
  762. keyword_generator.set("keyword:titlecase", title_casify(keyword.as_string()));
  763. keyword_generator.appendln(" case Keyword::@keyword:titlecase@:");
  764. }
  765. property_generator.append(R"~~~(
  766. return true;
  767. default:
  768. break;
  769. }
  770. )~~~");
  771. }
  772. if (auto maybe_valid_types = object.get_array("valid-types"sv); maybe_valid_types.has_value() && !maybe_valid_types->is_empty()) {
  773. auto& valid_types = maybe_valid_types.value();
  774. for (auto& valid_type : valid_types.values()) {
  775. auto type_name = valid_type.as_string().split_view(' ').first();
  776. if (!type_name_is_enum(type_name))
  777. continue;
  778. auto type_generator = generator.fork();
  779. type_generator.set("type_name:snakecase", snake_casify(type_name));
  780. type_generator.append(R"~~~(
  781. if (keyword_to_@type_name:snakecase@(keyword).has_value())
  782. return true;
  783. )~~~");
  784. }
  785. }
  786. property_generator.append(R"~~~(
  787. return false;
  788. }
  789. )~~~");
  790. });
  791. generator.append(R"~~~(
  792. default:
  793. return false;
  794. }
  795. }
  796. Optional<ValueType> property_resolves_percentages_relative_to(PropertyID property_id)
  797. {
  798. switch (property_id) {
  799. )~~~");
  800. properties.for_each_member([&](auto& name, auto& value) {
  801. VERIFY(value.is_object());
  802. if (is_legacy_alias(value.as_object()))
  803. return;
  804. if (auto resolved_type = value.as_object().get_byte_string("percentages-resolve-to"sv); resolved_type.has_value()) {
  805. auto property_generator = generator.fork();
  806. property_generator.set("name:titlecase", title_casify(name));
  807. property_generator.set("resolved_type:titlecase", title_casify(resolved_type.value()));
  808. property_generator.append(R"~~~(
  809. case PropertyID::@name:titlecase@:
  810. return ValueType::@resolved_type:titlecase@;
  811. )~~~");
  812. }
  813. });
  814. generator.append(R"~~~(
  815. default:
  816. return {};
  817. }
  818. }
  819. size_t property_maximum_value_count(PropertyID property_id)
  820. {
  821. switch (property_id) {
  822. )~~~");
  823. properties.for_each_member([&](auto& name, auto& value) {
  824. VERIFY(value.is_object());
  825. if (is_legacy_alias(value.as_object()))
  826. return;
  827. if (value.as_object().has("max-values"sv)) {
  828. JsonValue max_values = value.as_object().get("max-values"sv).release_value();
  829. VERIFY(max_values.is_integer<size_t>());
  830. auto property_generator = generator.fork();
  831. property_generator.set("name:titlecase", title_casify(name));
  832. property_generator.set("max_values", MUST(String::formatted("{}", max_values.as_integer<size_t>())));
  833. property_generator.append(R"~~~(
  834. case PropertyID::@name:titlecase@:
  835. return @max_values@;
  836. )~~~");
  837. }
  838. });
  839. generator.append(R"~~~(
  840. default:
  841. return 1;
  842. }
  843. })~~~");
  844. generate_bounds_checking_function(properties, generator, "angle"sv, "Angle"sv, "Deg"sv);
  845. generate_bounds_checking_function(properties, generator, "flex"sv, "Flex"sv, "Fr"sv);
  846. generate_bounds_checking_function(properties, generator, "frequency"sv, "Frequency"sv, "Hertz"sv);
  847. generate_bounds_checking_function(properties, generator, "integer"sv, "i64"sv, {}, "value"sv);
  848. generate_bounds_checking_function(properties, generator, "length"sv, "Length"sv, {}, "value.raw_value()"sv);
  849. generate_bounds_checking_function(properties, generator, "number"sv, "double"sv, {}, "value"sv);
  850. generate_bounds_checking_function(properties, generator, "percentage"sv, "Percentage"sv, {}, "value.value()"sv);
  851. generate_bounds_checking_function(properties, generator, "resolution"sv, "Resolution"sv, "Dpi"sv);
  852. generate_bounds_checking_function(properties, generator, "time"sv, "Time"sv, "S"sv);
  853. generator.append(R"~~~(
  854. bool property_is_shorthand(PropertyID property_id)
  855. {
  856. switch (property_id) {
  857. )~~~");
  858. properties.for_each_member([&](auto& name, auto& value) {
  859. if (is_legacy_alias(value.as_object()))
  860. return;
  861. if (value.as_object().has("longhands"sv)) {
  862. auto property_generator = generator.fork();
  863. property_generator.set("name:titlecase", title_casify(name));
  864. property_generator.append(R"~~~(
  865. case PropertyID::@name:titlecase@:
  866. )~~~");
  867. }
  868. });
  869. generator.append(R"~~~(
  870. return true;
  871. default:
  872. return false;
  873. }
  874. }
  875. )~~~");
  876. generator.append(R"~~~(
  877. Vector<PropertyID> longhands_for_shorthand(PropertyID property_id)
  878. {
  879. switch (property_id) {
  880. )~~~");
  881. properties.for_each_member([&](auto& name, auto& value) {
  882. if (is_legacy_alias(value.as_object()))
  883. return;
  884. if (value.as_object().has("longhands"sv)) {
  885. auto longhands = value.as_object().get("longhands"sv);
  886. VERIFY(longhands.has_value() && longhands->is_array());
  887. auto longhand_values = longhands->as_array();
  888. auto property_generator = generator.fork();
  889. property_generator.set("name:titlecase", title_casify(name));
  890. StringBuilder builder;
  891. bool first = true;
  892. longhand_values.for_each([&](auto& longhand) {
  893. if (first)
  894. first = false;
  895. else
  896. builder.append(", "sv);
  897. builder.appendff("PropertyID::{}", title_casify(longhand.as_string()));
  898. return IterationDecision::Continue;
  899. });
  900. property_generator.set("longhands", builder.to_byte_string());
  901. property_generator.append(R"~~~(
  902. case PropertyID::@name:titlecase@:
  903. return { @longhands@ };
  904. )~~~");
  905. }
  906. });
  907. generator.append(R"~~~(
  908. default:
  909. return { };
  910. }
  911. }
  912. )~~~");
  913. generator.append(R"~~~(
  914. } // namespace Web::CSS
  915. )~~~");
  916. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  917. return {};
  918. }
  919. bool is_animatable_property(JsonObject& properties, StringView property_name)
  920. {
  921. auto property = properties.get_object(property_name);
  922. VERIFY(property.has_value());
  923. if (auto animation_type = property.value().get_byte_string("animation-type"sv); animation_type.has_value()) {
  924. return animation_type != "none";
  925. }
  926. if (!property.value().has("longhands"sv)) {
  927. dbgln("Property '{}' must specify either 'animation-type' or 'longhands'"sv, property_name);
  928. VERIFY_NOT_REACHED();
  929. }
  930. auto longhands = property.value().get_array("longhands"sv);
  931. VERIFY(longhands.has_value());
  932. for (auto const& subproperty_name : longhands->values()) {
  933. VERIFY(subproperty_name.is_string());
  934. if (is_animatable_property(properties, subproperty_name.as_string()))
  935. return true;
  936. }
  937. return false;
  938. }