GenerateCSSPropertyID.cpp 39 KB

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