GenerateCSSPropertyID.cpp 39 KB

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