KeyframeEffect.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Copyright (c) 2023-2024, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <LibJS/Runtime/Iterator.h>
  8. #include <LibWeb/Animations/Animation.h>
  9. #include <LibWeb/Animations/KeyframeEffect.h>
  10. #include <LibWeb/Bindings/KeyframeEffectPrototype.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/CSS/StyleComputer.h>
  13. #include <LibWeb/Layout/Node.h>
  14. #include <LibWeb/Painting/Paintable.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. namespace Web::Animations {
  17. JS_DEFINE_ALLOCATOR(KeyframeEffect);
  18. template<typename T>
  19. WebIDL::ExceptionOr<Variant<T, Vector<T>>> convert_value_to_maybe_list(JS::Realm& realm, JS::Value value, Function<WebIDL::ExceptionOr<T>(JS::Value)>& value_converter)
  20. {
  21. auto& vm = realm.vm();
  22. if (TRY(value.is_array(vm))) {
  23. Vector<T> offsets;
  24. auto iterator = TRY(JS::get_iterator(vm, value, JS::IteratorHint::Sync));
  25. auto values = TRY(JS::iterator_to_list(vm, iterator));
  26. for (auto const& element : values) {
  27. if (element.is_undefined()) {
  28. offsets.append({});
  29. } else {
  30. offsets.append(TRY(value_converter(element)));
  31. }
  32. }
  33. return offsets;
  34. }
  35. return TRY(value_converter(value));
  36. }
  37. enum class AllowLists {
  38. Yes,
  39. No,
  40. };
  41. template<AllowLists AL>
  42. using KeyframeType = Conditional<AL == AllowLists::Yes, BasePropertyIndexedKeyframe, BaseKeyframe>;
  43. // https://www.w3.org/TR/web-animations-1/#process-a-keyframe-like-object
  44. template<AllowLists AL>
  45. static WebIDL::ExceptionOr<KeyframeType<AL>> process_a_keyframe_like_object(JS::Realm& realm, JS::Value keyframe_input)
  46. {
  47. auto& vm = realm.vm();
  48. Function<WebIDL::ExceptionOr<Optional<double>>(JS::Value)> to_offset = [&vm](JS::Value value) -> WebIDL::ExceptionOr<Optional<double>> {
  49. if (value.is_undefined())
  50. return Optional<double> {};
  51. auto double_value = TRY(value.to_double(vm));
  52. if (isnan(double_value) || isinf(double_value))
  53. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid offset value: {}", TRY(value.to_string(vm)))) };
  54. return double_value;
  55. };
  56. Function<WebIDL::ExceptionOr<String>(JS::Value)> to_string = [&vm](JS::Value value) -> WebIDL::ExceptionOr<String> {
  57. return TRY(value.to_string(vm));
  58. };
  59. Function<WebIDL::ExceptionOr<Bindings::CompositeOperationOrAuto>(JS::Value)> to_composite_operation = [&vm](JS::Value value) -> WebIDL::ExceptionOr<Bindings::CompositeOperationOrAuto> {
  60. if (value.is_undefined())
  61. return Bindings::CompositeOperationOrAuto::Auto;
  62. auto string_value = TRY(value.to_string(vm));
  63. if (string_value == "replace")
  64. return Bindings::CompositeOperationOrAuto::Replace;
  65. if (string_value == "add")
  66. return Bindings::CompositeOperationOrAuto::Add;
  67. if (string_value == "accumulate")
  68. return Bindings::CompositeOperationOrAuto::Accumulate;
  69. if (string_value == "auto")
  70. return Bindings::CompositeOperationOrAuto::Auto;
  71. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Invalid composite value"sv };
  72. };
  73. // 1. Run the procedure to convert an ECMAScript value to a dictionary type with keyframe input as the ECMAScript
  74. // value, and the dictionary type depending on the value of the allow lists flag as follows:
  75. //
  76. // -> If allow lists is true, use the following dictionary type: <BasePropertyIndexedKeyframe>.
  77. // -> Otherwise, use the following dictionary type: <BaseKeyframe>.
  78. //
  79. // Store the result of this procedure as keyframe output.
  80. KeyframeType<AL> keyframe_output;
  81. if (keyframe_input.is_nullish())
  82. return keyframe_output;
  83. auto& keyframe_object = keyframe_input.as_object();
  84. auto offset = TRY(keyframe_object.get("offset"));
  85. auto easing = TRY(keyframe_object.get("easing"));
  86. if (easing.is_undefined())
  87. easing = JS::PrimitiveString::create(vm, "linear"_string);
  88. auto composite = TRY(keyframe_object.get("composite"));
  89. if (composite.is_undefined())
  90. composite = JS::PrimitiveString::create(vm, "auto"_string);
  91. if constexpr (AL == AllowLists::Yes) {
  92. keyframe_output.offset = TRY(convert_value_to_maybe_list(realm, offset, to_offset));
  93. keyframe_output.composite = TRY(convert_value_to_maybe_list(realm, composite, to_composite_operation));
  94. auto easing_maybe_list = TRY(convert_value_to_maybe_list(realm, easing, to_string));
  95. easing_maybe_list.visit(
  96. [&](String const& value) {
  97. keyframe_output.easing = EasingValue { value };
  98. },
  99. [&](Vector<String> const& values) {
  100. Vector<EasingValue> easing_values;
  101. for (auto& easing_value : values)
  102. easing_values.append(easing_value);
  103. keyframe_output.easing = move(easing_values);
  104. });
  105. } else {
  106. keyframe_output.offset = TRY(to_offset(offset));
  107. keyframe_output.easing = TRY(to_string(easing));
  108. keyframe_output.composite = TRY(to_composite_operation(composite));
  109. }
  110. // 2. Build up a list of animatable properties as follows:
  111. //
  112. // 1. Let animatable properties be a list of property names (including shorthand properties that have longhand
  113. // sub-properties that are animatable) that can be animated by the implementation.
  114. // 2. Convert each property name in animatable properties to the equivalent IDL attribute by applying the
  115. // animation property name to IDL attribute name algorithm.
  116. // 3. Let input properties be the result of calling the EnumerableOwnNames operation with keyframe input as the
  117. // object.
  118. // 4. Make up a new list animation properties that consists of all of the properties that are in both input
  119. // properties and animatable properties, or which are in input properties and conform to the
  120. // <custom-property-name> production.
  121. auto input_properties = TRY(keyframe_object.enumerable_own_property_names(JS::Object::PropertyKind::Key));
  122. Vector<String> animation_properties;
  123. Optional<JS::Value> all_value;
  124. for (auto const& input_property : input_properties) {
  125. if (!input_property.is_string())
  126. continue;
  127. auto name = input_property.as_string().utf8_string();
  128. if (name == "all"sv) {
  129. all_value = TRY(keyframe_object.get(JS::PropertyKey { name }));
  130. for (auto i = to_underlying(CSS::first_longhand_property_id); i <= to_underlying(CSS::last_longhand_property_id); ++i) {
  131. auto property = static_cast<CSS::PropertyID>(i);
  132. if (CSS::is_animatable_property(property))
  133. animation_properties.append(String { CSS::string_from_property_id(property) });
  134. }
  135. } else {
  136. // Handle the two special cases
  137. if (name == "cssFloat"sv || name == "cssOffset"sv) {
  138. animation_properties.append(name);
  139. } else if (name == "float"sv || name == "offset"sv) {
  140. // Ignore these property names
  141. } else if (auto property = CSS::property_id_from_camel_case_string(name); property.has_value()) {
  142. if (CSS::is_animatable_property(property.value()))
  143. animation_properties.append(name);
  144. }
  145. }
  146. }
  147. // 5. Sort animation properties in ascending order by the Unicode codepoints that define each property name.
  148. quick_sort(animation_properties);
  149. // 6. For each property name in animation properties,
  150. for (auto const& property_name : animation_properties) {
  151. // 1. Let raw value be the result of calling the [[Get]] internal method on keyframe input, with property name
  152. // as the property key and keyframe input as the receiver.
  153. // 2. Check the completion record of raw value.
  154. JS::PropertyKey key { property_name };
  155. auto raw_value = TRY(keyframe_object.has_property(key)) ? TRY(keyframe_object.get(key)) : *all_value;
  156. using PropertyValuesType = Conditional<AL == AllowLists::Yes, Vector<String>, String>;
  157. PropertyValuesType property_values;
  158. // 3. Convert raw value to a DOMString or sequence of DOMStrings property values as follows:
  159. // -> If allow lists is true,
  160. if constexpr (AL == AllowLists::Yes) {
  161. // Let property values be the result of converting raw value to IDL type (DOMString or sequence<DOMString>)
  162. // using the procedures defined for converting an ECMAScript value to an IDL value [WEBIDL].
  163. auto intermediate_property_values = TRY(convert_value_to_maybe_list(realm, raw_value, to_string));
  164. // If property values is a single DOMString, replace property values with a sequence of DOMStrings with the
  165. // original value of property values as the only element.
  166. if (intermediate_property_values.has<String>())
  167. property_values = Vector { intermediate_property_values.get<String>() };
  168. else
  169. property_values = intermediate_property_values.get<Vector<String>>();
  170. }
  171. // -> Otherwise,
  172. else {
  173. // Let property values be the result of converting raw value to a DOMString using the procedure for
  174. // converting an ECMAScript value to a DOMString [WEBIDL].
  175. property_values = TRY(raw_value.to_string(vm));
  176. }
  177. // 4. Calculate the normalized property name as the result of applying the IDL attribute name to animation
  178. // property name algorithm to property name.
  179. // Note: We do not need to do this, since we did not need to do the reverse step (animation property name to IDL
  180. // attribute name) in the steps above.
  181. // 5. Add a property to keyframe output with normalized property name as the property name, and property values
  182. // as the property value.
  183. if constexpr (AL == AllowLists::Yes) {
  184. keyframe_output.properties.set(property_name, property_values);
  185. } else {
  186. keyframe_output.unparsed_properties().set(property_name, property_values);
  187. }
  188. }
  189. return keyframe_output;
  190. }
  191. // https://www.w3.org/TR/web-animations-1/#compute-missing-keyframe-offsets
  192. static void compute_missing_keyframe_offsets(Vector<BaseKeyframe>& keyframes)
  193. {
  194. // 1. For each keyframe, in keyframes, let the computed keyframe offset of the keyframe be equal to its keyframe
  195. // offset value.
  196. for (auto& keyframe : keyframes)
  197. keyframe.computed_offset = keyframe.offset;
  198. // 2. If keyframes contains more than one keyframe and the computed keyframe offset of the first keyframe in
  199. // keyframes is null, set the computed keyframe offset of the first keyframe to 0.
  200. if (keyframes.size() > 1 && !keyframes[0].computed_offset.has_value())
  201. keyframes[0].computed_offset = 0.0;
  202. // 3. If the computed keyframe offset of the last keyframe in keyframes is null, set its computed keyframe offset
  203. // to 1.
  204. if (!keyframes.is_empty() && !keyframes.last().computed_offset.has_value())
  205. keyframes.last().computed_offset = 1.0;
  206. // 4. For each pair of keyframes A and B where:
  207. // - A appears before B in keyframes, and
  208. // - A and B have a computed keyframe offset that is not null, and
  209. // - all keyframes between A and B have a null computed keyframe offset,
  210. auto find_next_index_of_keyframe_with_computed_offset = [&](size_t starting_index) -> Optional<size_t> {
  211. for (size_t index = starting_index; index < keyframes.size(); index++) {
  212. if (keyframes[index].computed_offset.has_value())
  213. return index;
  214. }
  215. return {};
  216. };
  217. auto maybe_index_a = find_next_index_of_keyframe_with_computed_offset(0);
  218. if (!maybe_index_a.has_value())
  219. return;
  220. auto index_a = maybe_index_a.value();
  221. auto maybe_index_b = find_next_index_of_keyframe_with_computed_offset(index_a + 1);
  222. while (maybe_index_b.has_value()) {
  223. auto index_b = maybe_index_b.value();
  224. // calculate the computed keyframe offset of each keyframe between A and B as follows:
  225. for (size_t keyframe_index = index_a + 1; keyframe_index < index_b; keyframe_index++) {
  226. // 1. Let offsetk be the computed keyframe offset of a keyframe k.
  227. auto offset_a = keyframes[index_a].computed_offset.value();
  228. auto offset_b = keyframes[index_b].computed_offset.value();
  229. // 2. Let n be the number of keyframes between and including A and B minus 1.
  230. auto n = static_cast<double>(index_b - index_a);
  231. // 3. Let index refer to the position of keyframe in the sequence of keyframes between A and B such that the
  232. // first keyframe after A has an index of 1.
  233. auto index = static_cast<double>(keyframe_index - index_a);
  234. // 4. Set the computed keyframe offset of keyframe to offsetA + (offsetB − offsetA) × index / n.
  235. keyframes[keyframe_index].computed_offset = (offset_a + (offset_b - offset_a)) * index / n;
  236. }
  237. index_a = index_b;
  238. maybe_index_b = find_next_index_of_keyframe_with_computed_offset(index_b + 1);
  239. }
  240. }
  241. // https://www.w3.org/TR/web-animations-1/#loosely-sorted-by-offset
  242. static bool is_loosely_sorted_by_offset(Vector<BaseKeyframe> const& keyframes)
  243. {
  244. // The list of keyframes for a keyframe effect must be loosely sorted by offset which means that for each keyframe
  245. // in the list that has a keyframe offset that is not null, the offset is greater than or equal to the offset of the
  246. // previous keyframe in the list with a keyframe offset that is not null, if any.
  247. Optional<double> last_offset;
  248. for (auto const& keyframe : keyframes) {
  249. if (!keyframe.offset.has_value())
  250. continue;
  251. if (last_offset.has_value() && keyframe.offset.value() < last_offset.value())
  252. return false;
  253. last_offset = keyframe.offset;
  254. }
  255. return true;
  256. }
  257. // https://www.w3.org/TR/web-animations-1/#process-a-keyframes-argument
  258. static WebIDL::ExceptionOr<Vector<BaseKeyframe>> process_a_keyframes_argument(JS::Realm& realm, JS::GCPtr<JS::Object> object)
  259. {
  260. auto& vm = realm.vm();
  261. // 1. If object is null, return an empty sequence of keyframes.
  262. if (!object)
  263. return Vector<BaseKeyframe> {};
  264. // 2. Let processed keyframes be an empty sequence of keyframes.
  265. Vector<BaseKeyframe> processed_keyframes;
  266. Vector<EasingValue> unused_easings;
  267. // 3. Let method be the result of GetMethod(object, @@iterator).
  268. // 4. Check the completion record of method.
  269. auto method = TRY(JS::Value(object).get_method(vm, vm.well_known_symbol_iterator()));
  270. // 5. Perform the steps corresponding to the first matching condition from below,
  271. // -> If method is not undefined,
  272. if (method) {
  273. // 1. Let iter be GetIterator(object, method).
  274. // 2. Check the completion record of iter.
  275. auto iter = TRY(JS::get_iterator_from_method(vm, object, *method));
  276. // 3. Repeat:
  277. while (true) {
  278. // 1. Let next be IteratorStep(iter).
  279. // 2. Check the completion record of next.
  280. auto next = TRY(JS::iterator_step(vm, iter));
  281. // 3. If next is false abort this loop.
  282. if (!next)
  283. break;
  284. // 4. Let nextItem be IteratorValue(next).
  285. // 5. Check the completion record of nextItem.
  286. auto next_item = TRY(JS::iterator_value(vm, *next));
  287. // 6. If Type(nextItem) is not Undefined, Null or Object, then throw a TypeError and abort these steps.
  288. if (!next_item.is_nullish() && !next_item.is_object())
  289. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOrNull, next_item.to_string_without_side_effects());
  290. // 7. Append to processed keyframes the result of running the procedure to process a keyframe-like object
  291. // passing nextItem as the keyframe input and with the allow lists flag set to false.
  292. processed_keyframes.append(TRY(process_a_keyframe_like_object<AllowLists::No>(realm, next_item)));
  293. }
  294. }
  295. // -> Otherwise,
  296. else {
  297. // 1. Let property-indexed keyframe be the result of running the procedure to process a keyframe-like object
  298. // passing object as the keyframe input and with the allow lists flag set to true.
  299. auto property_indexed_keyframe = TRY(process_a_keyframe_like_object<AllowLists::Yes>(realm, object));
  300. // 2. For each member, m, in property-indexed keyframe, perform the following steps:
  301. for (auto const& [property_name, property_values] : property_indexed_keyframe.properties) {
  302. // 1. Let property name be the key for m.
  303. // 2. If property name is "composite", or "easing", or "offset", skip the remaining steps in this loop and
  304. // continue from the next member in property-indexed keyframe after m.
  305. // Note: This will never happen, since these fields have dedicated members on BasePropertyIndexedKeyframe
  306. // 3. Let property values be the value for m.
  307. // 4. Let property keyframes be an empty sequence of keyframes.
  308. Vector<BaseKeyframe> property_keyframes;
  309. // 5. For each value, v, in property values perform the following steps:
  310. for (auto const& value : property_values) {
  311. // 1. Let k be a new keyframe with a null keyframe offset.
  312. BaseKeyframe keyframe;
  313. // 2. Add the property-value pair, property name → v, to k.
  314. keyframe.unparsed_properties().set(property_name, value);
  315. // 3. Append k to property keyframes.
  316. property_keyframes.append(keyframe);
  317. }
  318. // 6. Apply the procedure to compute missing keyframe offsets to property keyframes.
  319. compute_missing_keyframe_offsets(property_keyframes);
  320. // 7. Add keyframes in property keyframes to processed keyframes.
  321. processed_keyframes.extend(move(property_keyframes));
  322. }
  323. // 3. Sort processed keyframes by the computed keyframe offset of each keyframe in increasing order.
  324. quick_sort(processed_keyframes, [](auto const& a, auto const& b) {
  325. return a.computed_offset.value() < b.computed_offset.value();
  326. });
  327. // 4. Merge adjacent keyframes in processed keyframes when they have equal computed keyframe offsets.
  328. // Note: The spec doesn't specify how to merge them, but WebKit seems to just override the properties of the
  329. // earlier keyframe with the properties of the later keyframe.
  330. for (int i = 0; i < static_cast<int>(processed_keyframes.size() - 1); i++) {
  331. auto& keyframe_a = processed_keyframes[i];
  332. auto& keyframe_b = processed_keyframes[i + 1];
  333. if (keyframe_a.computed_offset.value() == keyframe_b.computed_offset.value()) {
  334. keyframe_a.easing = keyframe_b.easing;
  335. keyframe_a.composite = keyframe_b.composite;
  336. for (auto const& [property_name, property_value] : keyframe_b.unparsed_properties())
  337. keyframe_a.unparsed_properties().set(property_name, property_value);
  338. processed_keyframes.remove(i + 1);
  339. i--;
  340. }
  341. }
  342. // 5. Let offsets be a sequence of nullable double values assigned based on the type of the "offset" member
  343. // of the property-indexed keyframe as follows:
  344. //
  345. // -> sequence<double?>,
  346. // The value of "offset" as-is.
  347. // -> double?,
  348. // A sequence of length one with the value of "offset" as its single item, i.e. « offset »,
  349. auto offsets = property_indexed_keyframe.offset.has<Optional<double>>()
  350. ? Vector { property_indexed_keyframe.offset.get<Optional<double>>() }
  351. : property_indexed_keyframe.offset.get<Vector<Optional<double>>>();
  352. // 6. Assign each value in offsets to the keyframe offset of the keyframe with corresponding position in
  353. // processed keyframes until the end of either sequence is reached.
  354. for (size_t i = 0; i < offsets.size() && i < processed_keyframes.size(); i++)
  355. processed_keyframes[i].offset = offsets[i];
  356. // 7. Let easings be a sequence of DOMString values assigned based on the type of the "easing" member of the
  357. // property-indexed keyframe as follows:
  358. //
  359. // -> sequence<DOMString>,
  360. // The value of "easing" as-is.
  361. // -> DOMString,
  362. // A sequence of length one with the value of "easing" as its single item, i.e. « easing »,
  363. auto easings = property_indexed_keyframe.easing.has<EasingValue>()
  364. ? Vector { property_indexed_keyframe.easing.get<EasingValue>() }
  365. : property_indexed_keyframe.easing.get<Vector<EasingValue>>();
  366. // 8. If easings is an empty sequence, let it be a sequence of length one containing the single value "linear",
  367. // i.e. « "linear" ».
  368. if (easings.is_empty())
  369. easings.append("linear"_string);
  370. // 9. If easings has fewer items than processed keyframes, repeat the elements in easings successively starting
  371. // from the beginning of the list until easings has as many items as processed keyframes.
  372. //
  373. // For example, if processed keyframes has five items, and easings is the sequence « "ease-in", "ease-out" »,
  374. // easings would be repeated to become « "ease-in", "ease-out", "ease-in", "ease-out", "ease-in" ».
  375. size_t num_easings = easings.size();
  376. size_t index = 0;
  377. while (easings.size() < processed_keyframes.size())
  378. easings.append(easings[index++ % num_easings]);
  379. // 10. If easings has more items than processed keyframes, store the excess items as unused easings.
  380. while (easings.size() > processed_keyframes.size())
  381. unused_easings.append(easings.take_last());
  382. // 11. Assign each value in easings to a property named "easing" on the keyframe with the corresponding position
  383. // in processed keyframes until the end of processed keyframes is reached.
  384. for (size_t i = 0; i < processed_keyframes.size(); i++)
  385. processed_keyframes[i].easing = easings[i];
  386. // 12. If the "composite" member of the property-indexed keyframe is not an empty sequence:
  387. auto composite_value = property_indexed_keyframe.composite;
  388. if (!composite_value.has<Vector<Bindings::CompositeOperationOrAuto>>() || !composite_value.get<Vector<Bindings::CompositeOperationOrAuto>>().is_empty()) {
  389. // 1. Let composite modes be a sequence of CompositeOperationOrAuto values assigned from the "composite"
  390. // member of property-indexed keyframe. If that member is a single CompositeOperationOrAuto value
  391. // operation, let composite modes be a sequence of length one, with the value of the "composite" as its
  392. // single item.
  393. auto composite_modes = composite_value.has<Bindings::CompositeOperationOrAuto>()
  394. ? Vector { composite_value.get<Bindings::CompositeOperationOrAuto>() }
  395. : composite_value.get<Vector<Bindings::CompositeOperationOrAuto>>();
  396. // 2. As with easings, if composite modes has fewer items than processed keyframes, repeat the elements in
  397. // composite modes successively starting from the beginning of the list until composite modes has as
  398. // many items as processed keyframes.
  399. size_t num_composite_modes = composite_modes.size();
  400. index = 0;
  401. while (composite_modes.size() < processed_keyframes.size())
  402. composite_modes.append(composite_modes[index++ % num_composite_modes]);
  403. // 3. Assign each value in composite modes that is not auto to the keyframe-specific composite operation on
  404. // the keyframe with the corresponding position in processed keyframes until the end of processed
  405. // keyframes is reached.
  406. for (size_t i = 0; i < processed_keyframes.size(); i++) {
  407. if (composite_modes[i] != Bindings::CompositeOperationOrAuto::Auto)
  408. processed_keyframes[i].composite = composite_modes[i];
  409. }
  410. }
  411. }
  412. // 6. If processed keyframes is not loosely sorted by offset, throw a TypeError and abort these steps.
  413. if (!is_loosely_sorted_by_offset(processed_keyframes))
  414. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Keyframes are not in ascending order based on offset"sv };
  415. // 7. If there exist any keyframe in processed keyframes whose keyframe offset is non-null and less than zero or
  416. // greater than one, throw a TypeError and abort these steps.
  417. for (size_t i = 0; i < processed_keyframes.size(); i++) {
  418. auto const& keyframe = processed_keyframes[i];
  419. if (!keyframe.offset.has_value())
  420. continue;
  421. auto offset = keyframe.offset.value();
  422. if (offset < 0.0 || offset > 1.0)
  423. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Keyframe {} has invalid offset value {}"sv, i, offset)) };
  424. }
  425. // 8. For each frame in processed keyframes, perform the following steps:
  426. for (auto& keyframe : processed_keyframes) {
  427. // 1. For each property-value pair in frame, parse the property value using the syntax specified for that
  428. // property.
  429. //
  430. // If the property value is invalid according to the syntax for the property, discard the property-value pair.
  431. // User agents that provide support for diagnosing errors in content SHOULD produce an appropriate warning
  432. // highlight
  433. BaseKeyframe::ParsedProperties parsed_properties;
  434. for (auto& [property_string, value_string] : keyframe.unparsed_properties()) {
  435. Optional<CSS::PropertyID> property_id;
  436. // Handle some special cases
  437. if (property_string == "cssFloat"sv) {
  438. property_id = CSS::PropertyID::Float;
  439. } else if (property_string == "cssOffset"sv) {
  440. // FIXME: Support CSS offset property
  441. } else if (property_string == "float"sv || property_string == "offset"sv) {
  442. // Ignore these properties
  443. } else if (auto property = CSS::property_id_from_camel_case_string(property_string); property.has_value()) {
  444. property_id = *property;
  445. }
  446. if (!property_id.has_value())
  447. continue;
  448. auto maybe_parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value_string);
  449. if (maybe_parser.is_error())
  450. continue;
  451. if (auto style_value = maybe_parser.release_value().parse_as_css_value(*property_id)) {
  452. // Handle 'initial' here so we don't have to get the default value of the property every frame in StyleComputer
  453. if (style_value->is_initial())
  454. style_value = CSS::property_initial_value(realm, *property_id);
  455. parsed_properties.set(*property_id, *style_value);
  456. }
  457. }
  458. keyframe.properties.set(move(parsed_properties));
  459. // 2. Let the timing function of frame be the result of parsing the "easing" property on frame using the CSS
  460. // syntax defined for the easing member of the EffectTiming dictionary.
  461. //
  462. // If parsing the "easing" property fails, throw a TypeError and abort this procedure.
  463. auto easing_string = keyframe.easing.get<String>();
  464. auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string);
  465. if (!easing_value)
  466. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) };
  467. keyframe.easing.set(NonnullRefPtr<CSS::StyleValue const> { *easing_value });
  468. }
  469. // 9. Parse each of the values in unused easings using the CSS syntax defined for easing member of the EffectTiming
  470. // interface, and if any of the values fail to parse, throw a TypeError and abort this procedure.
  471. for (auto& unused_easing : unused_easings) {
  472. auto easing_string = unused_easing.get<String>();
  473. auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string);
  474. if (!easing_value)
  475. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) };
  476. }
  477. return processed_keyframes;
  478. }
  479. // https://www.w3.org/TR/css-animations-2/#keyframe-processing
  480. void KeyframeEffect::generate_initial_and_final_frames(RefPtr<KeyFrameSet> keyframe_set, HashTable<CSS::PropertyID> const& animated_properties)
  481. {
  482. // 1. Find or create the initial keyframe, a keyframe with a keyframe offset of 0%, default timing function
  483. // as its keyframe timing function, and default composite as its keyframe composite.
  484. KeyFrameSet::ResolvedKeyFrame* initial_keyframe;
  485. if (auto existing_keyframe = keyframe_set->keyframes_by_key.find(0)) {
  486. initial_keyframe = existing_keyframe;
  487. } else {
  488. keyframe_set->keyframes_by_key.insert(0, {});
  489. initial_keyframe = keyframe_set->keyframes_by_key.find(0);
  490. }
  491. // 2. For any property in animated properties that is not otherwise present in a keyframe with an offset of
  492. // 0% or one that would be positioned earlier in the used keyframe order, add the computed value of that
  493. // property on element to initial keyframe’s keyframe values.
  494. for (auto property : animated_properties) {
  495. if (!initial_keyframe->properties.contains(property))
  496. initial_keyframe->properties.set(property, KeyFrameSet::UseInitial {});
  497. }
  498. // 3. If initial keyframe’s keyframe values is not empty, prepend initial keyframe to keyframes.
  499. // 4. Repeat for final keyframe, using an offset of 100%, considering keyframes positioned later in the used
  500. // keyframe order, and appending to keyframes.
  501. KeyFrameSet::ResolvedKeyFrame* final_keyframe;
  502. if (auto existing_keyframe = keyframe_set->keyframes_by_key.find(100 * AnimationKeyFrameKeyScaleFactor)) {
  503. final_keyframe = existing_keyframe;
  504. } else {
  505. keyframe_set->keyframes_by_key.insert(100 * AnimationKeyFrameKeyScaleFactor, {});
  506. final_keyframe = keyframe_set->keyframes_by_key.find(100 * AnimationKeyFrameKeyScaleFactor);
  507. }
  508. for (auto property : animated_properties) {
  509. if (!final_keyframe->properties.contains(property))
  510. final_keyframe->properties.set(property, KeyFrameSet::UseInitial {});
  511. }
  512. }
  513. // https://www.w3.org/TR/web-animations-1/#animation-composite-order
  514. int KeyframeEffect::composite_order(JS::NonnullGCPtr<KeyframeEffect> a, JS::NonnullGCPtr<KeyframeEffect> b)
  515. {
  516. // 1. Let the associated animation of an animation effect be the animation associated with the animation effect.
  517. auto a_animation = a->associated_animation();
  518. auto b_animation = b->associated_animation();
  519. // 2. Sort A and B by applying the following conditions in turn until the order is resolved,
  520. // 1. If A and B’s associated animations differ by class, sort by any inter-class composite order defined for
  521. // the corresponding classes.
  522. auto a_class = a_animation->animation_class();
  523. auto b_class = b_animation->animation_class();
  524. // From https://www.w3.org/TR/css-animations-2/#animation-composite-order:
  525. // "CSS Animations with an owning element have a later composite order than CSS Transitions but an earlier
  526. // composite order than animations without a specific animation class."
  527. if (a_class != b_class)
  528. return to_underlying(a_class) - to_underlying(b_class);
  529. // 2. If A and B are still not sorted, sort by any class-specific composite order defined by the common class of
  530. // A and B’s associated animations.
  531. if (auto order = a_animation->class_specific_composite_order(*b_animation); order.has_value())
  532. return order.value();
  533. // 3. If A and B are still not sorted, sort by the position of their associated animations in the global
  534. // animation list.
  535. return a_animation->global_animation_list_order() - b_animation->global_animation_list_order();
  536. }
  537. JS::NonnullGCPtr<KeyframeEffect> KeyframeEffect::create(JS::Realm& realm)
  538. {
  539. return realm.heap().allocate<KeyframeEffect>(realm, realm);
  540. }
  541. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-keyframeeffect
  542. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> KeyframeEffect::construct_impl(
  543. JS::Realm& realm,
  544. JS::Handle<DOM::Element> const& target,
  545. Optional<JS::Handle<JS::Object>> const& keyframes,
  546. Variant<double, KeyframeEffectOptions> options)
  547. {
  548. auto& vm = realm.vm();
  549. // 1. Create a new KeyframeEffect object, effect.
  550. auto effect = vm.heap().allocate<KeyframeEffect>(realm, realm);
  551. // 2. Set the target element of effect to target.
  552. effect->set_target(target);
  553. // 3. Set the target pseudo-selector to the result corresponding to the first matching condition from below.
  554. // If options is a KeyframeEffectOptions object with a pseudoElement property,
  555. if (options.has<KeyframeEffectOptions>()) {
  556. // Set the target pseudo-selector to the value of the pseudoElement property.
  557. //
  558. // When assigning this property, the error-handling defined for the pseudoElement setter on the interface is
  559. // applied. If the setter requires an exception to be thrown, this procedure must throw the same exception and
  560. // abort all further steps.
  561. TRY(effect->set_pseudo_element(options.get<KeyframeEffectOptions>().pseudo_element));
  562. }
  563. // Otherwise,
  564. else {
  565. // Set the target pseudo-selector to null.
  566. // Note: This is the default when constructed
  567. }
  568. // 4. Let timing input be the result corresponding to the first matching condition from below.
  569. KeyframeEffectOptions timing_input;
  570. // If options is a KeyframeEffectOptions object,
  571. if (options.has<KeyframeEffectOptions>()) {
  572. // Let timing input be options.
  573. timing_input = options.get<KeyframeEffectOptions>();
  574. }
  575. // Otherwise (if options is a double),
  576. else {
  577. // Let timing input be a new EffectTiming object with all members set to their default values and duration set
  578. // to options.
  579. timing_input.duration = options.get<double>();
  580. }
  581. // 5. Call the procedure to update the timing properties of an animation effect of effect from timing input.
  582. // If that procedure causes an exception to be thrown, propagate the exception and abort this procedure.
  583. TRY(effect->update_timing(timing_input.to_optional_effect_timing()));
  584. // 6. If options is a KeyframeEffectOptions object, assign the composite property of effect to the corresponding
  585. // value from options.
  586. //
  587. // When assigning this property, the error-handling defined for the corresponding setter on the KeyframeEffect
  588. // interface is applied. If the setter requires an exception to be thrown for the value specified by options,
  589. // this procedure must throw the same exception and abort all further steps.
  590. if (options.has<KeyframeEffectOptions>())
  591. effect->set_composite(options.get<KeyframeEffectOptions>().composite);
  592. // 7. Initialize the set of keyframes by performing the procedure defined for setKeyframes() passing keyframes as
  593. // the input.
  594. TRY(effect->set_keyframes(keyframes));
  595. return effect;
  596. }
  597. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-keyframeeffect-source
  598. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> KeyframeEffect::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<KeyframeEffect> source)
  599. {
  600. auto& vm = realm.vm();
  601. // 1. Create a new KeyframeEffect object, effect.
  602. auto effect = vm.heap().allocate<KeyframeEffect>(realm, realm);
  603. // 2. Set the following properties of effect using the corresponding values of source:
  604. // - effect target,
  605. effect->m_target_element = source->target();
  606. // - keyframes,
  607. effect->m_keyframes = source->m_keyframes;
  608. // - composite operation, and
  609. effect->set_composite(source->composite());
  610. // - all specified timing properties:
  611. // - start delay,
  612. effect->m_start_delay = source->m_start_delay;
  613. // - end delay,
  614. effect->m_end_delay = source->m_end_delay;
  615. // - fill mode,
  616. effect->m_fill_mode = source->m_fill_mode;
  617. // - iteration start,
  618. effect->m_iteration_start = source->m_iteration_start;
  619. // - iteration count,
  620. effect->m_iteration_count = source->m_iteration_count;
  621. // - iteration duration,
  622. effect->m_iteration_duration = source->m_iteration_duration;
  623. // - playback direction, and
  624. effect->m_playback_direction = source->m_playback_direction;
  625. // - timing function.
  626. effect->m_easing_function = source->m_easing_function;
  627. effect->m_timing_function = source->m_timing_function;
  628. return effect;
  629. }
  630. void KeyframeEffect::set_target(DOM::Element* target)
  631. {
  632. if (auto animation = this->associated_animation()) {
  633. if (m_target_element)
  634. m_target_element->disassociate_with_animation(*animation);
  635. if (target)
  636. target->associate_with_animation(*animation);
  637. }
  638. m_target_element = target;
  639. }
  640. Optional<String> KeyframeEffect::pseudo_element() const
  641. {
  642. if (!m_target_pseudo_selector.has_value())
  643. return {};
  644. return MUST(String::formatted("::{}", m_target_pseudo_selector->name()));
  645. }
  646. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-pseudoelement
  647. WebIDL::ExceptionOr<void> KeyframeEffect::set_pseudo_element(Optional<String> pseudo_element)
  648. {
  649. auto& realm = this->realm();
  650. // On setting, sets the target pseudo-selector of the animation effect to the provided value after applying the
  651. // following exceptions:
  652. // FIXME:
  653. // - If one of the legacy Selectors Level 2 single-colon selectors (':before', ':after', ':first-letter', or
  654. // ':first-line') is specified, the target pseudo-selector must be set to the equivalent two-colon selector
  655. // (e.g. '::before').
  656. if (pseudo_element.has_value()) {
  657. auto value = pseudo_element.value();
  658. if (value == ":before" || value == ":after" || value == ":first-letter" || value == ":first-line") {
  659. m_target_pseudo_selector = CSS::Selector::PseudoElement::from_string(MUST(value.substring_from_byte_offset(1)));
  660. return {};
  661. }
  662. }
  663. // - If the provided value is not null and is an invalid <pseudo-element-selector>, the user agent must throw a
  664. // DOMException with error name SyntaxError and leave the target pseudo-selector of this animation effect
  665. // unchanged.
  666. if (pseudo_element.has_value()) {
  667. if (pseudo_element->starts_with_bytes("::"sv)) {
  668. if (auto value = CSS::Selector::PseudoElement::from_string(MUST(pseudo_element->substring_from_byte_offset(2))); value.has_value()) {
  669. m_target_pseudo_selector = value;
  670. return {};
  671. }
  672. }
  673. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid pseudo-element selector: \"{}\"", pseudo_element.value())));
  674. }
  675. m_target_pseudo_selector = {};
  676. return {};
  677. }
  678. Optional<CSS::Selector::PseudoElement::Type> KeyframeEffect::pseudo_element_type() const
  679. {
  680. if (!m_target_pseudo_selector.has_value())
  681. return {};
  682. return m_target_pseudo_selector->type();
  683. }
  684. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-getkeyframes
  685. WebIDL::ExceptionOr<JS::MarkedVector<JS::Object*>> KeyframeEffect::get_keyframes()
  686. {
  687. if (m_keyframe_objects.size() != m_keyframes.size()) {
  688. auto& vm = this->vm();
  689. auto& realm = this->realm();
  690. // Recalculate the keyframe objects
  691. VERIFY(m_keyframe_objects.size() == 0);
  692. for (auto& keyframe : m_keyframes) {
  693. auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
  694. TRY(object->set(vm.names.offset, keyframe.offset.has_value() ? JS::Value(keyframe.offset.value()) : JS::js_null(), ShouldThrowExceptions::Yes));
  695. TRY(object->set(vm.names.computedOffset, JS::Value(keyframe.computed_offset.value()), ShouldThrowExceptions::Yes));
  696. auto easing_value = keyframe.easing.get<NonnullRefPtr<CSS::StyleValue const>>();
  697. TRY(object->set(vm.names.easing, JS::PrimitiveString::create(vm, easing_value->to_string()), ShouldThrowExceptions::Yes));
  698. if (keyframe.composite == Bindings::CompositeOperationOrAuto::Replace) {
  699. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "replace"sv), ShouldThrowExceptions::Yes));
  700. } else if (keyframe.composite == Bindings::CompositeOperationOrAuto::Add) {
  701. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "add"sv), ShouldThrowExceptions::Yes));
  702. } else if (keyframe.composite == Bindings::CompositeOperationOrAuto::Accumulate) {
  703. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "accumulate"sv), ShouldThrowExceptions::Yes));
  704. } else {
  705. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "auto"sv), ShouldThrowExceptions::Yes));
  706. }
  707. for (auto const& [id, value] : keyframe.parsed_properties()) {
  708. auto value_string = JS::PrimitiveString::create(vm, value->to_string());
  709. TRY(object->set(JS::PropertyKey(DeprecatedFlyString(CSS::camel_case_string_from_property_id(id))), value_string, ShouldThrowExceptions::Yes));
  710. }
  711. m_keyframe_objects.append(object);
  712. }
  713. }
  714. JS::MarkedVector<JS::Object*> keyframes { heap() };
  715. for (auto const& keyframe : m_keyframe_objects)
  716. keyframes.append(keyframe);
  717. return keyframes;
  718. }
  719. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-setkeyframes
  720. WebIDL::ExceptionOr<void> KeyframeEffect::set_keyframes(Optional<JS::Handle<JS::Object>> const& keyframe_object)
  721. {
  722. m_keyframe_objects.clear();
  723. m_keyframes = TRY(process_a_keyframes_argument(realm(), keyframe_object.has_value() ? JS::GCPtr { keyframe_object->ptr() } : JS::GCPtr<Object> {}));
  724. // FIXME: After processing the keyframe argument, we need to turn the set of keyframes into a set of computed
  725. // keyframes using the procedure outlined in the second half of
  726. // https://www.w3.org/TR/web-animations-1/#calculating-computed-keyframes. For now, just compute the
  727. // missing keyframe offsets
  728. compute_missing_keyframe_offsets(m_keyframes);
  729. auto keyframe_set = adopt_ref(*new KeyFrameSet);
  730. m_target_properties.clear();
  731. auto target = this->target();
  732. for (auto& keyframe : m_keyframes) {
  733. Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame resolved_keyframe;
  734. auto key = static_cast<u64>(keyframe.computed_offset.value() * 100 * AnimationKeyFrameKeyScaleFactor);
  735. for (auto [property_id, property_value] : keyframe.parsed_properties()) {
  736. if (property_value->is_unresolved() && target)
  737. property_value = CSS::Parser::Parser::resolve_unresolved_style_value(CSS::Parser::ParsingContext { target->document() }, *target, pseudo_element_type(), property_id, property_value->as_unresolved());
  738. CSS::StyleComputer::for_each_property_expanding_shorthands(property_id, property_value, CSS::StyleComputer::AllowUnresolved::Yes, [&](CSS::PropertyID shorthand_id, CSS::StyleValue const& shorthand_value) {
  739. m_target_properties.set(shorthand_id);
  740. resolved_keyframe.properties.set(shorthand_id, NonnullRefPtr<CSS::StyleValue const> { shorthand_value });
  741. });
  742. }
  743. keyframe_set->keyframes_by_key.insert(key, resolved_keyframe);
  744. }
  745. generate_initial_and_final_frames(keyframe_set, m_target_properties);
  746. m_key_frame_set = keyframe_set;
  747. return {};
  748. }
  749. KeyframeEffect::KeyframeEffect(JS::Realm& realm)
  750. : AnimationEffect(realm)
  751. {
  752. }
  753. void KeyframeEffect::initialize(JS::Realm& realm)
  754. {
  755. Base::initialize(realm);
  756. WEB_SET_PROTOTYPE_FOR_INTERFACE(KeyframeEffect);
  757. }
  758. void KeyframeEffect::visit_edges(Cell::Visitor& visitor)
  759. {
  760. Base::visit_edges(visitor);
  761. visitor.visit(m_target_element);
  762. visitor.visit(m_keyframe_objects);
  763. }
  764. static CSS::RequiredInvalidationAfterStyleChange compute_required_invalidation(HashMap<CSS::PropertyID, NonnullRefPtr<CSS::StyleValue const>> const& old_properties, HashMap<CSS::PropertyID, NonnullRefPtr<CSS::StyleValue const>> const& new_properties)
  765. {
  766. CSS::RequiredInvalidationAfterStyleChange invalidation;
  767. auto old_and_new_properties = MUST(Bitmap::create(to_underlying(CSS::last_property_id) + 1, 0));
  768. for (auto const& [property_id, _] : old_properties)
  769. old_and_new_properties.set(to_underlying(property_id), 1);
  770. for (auto const& [property_id, _] : new_properties)
  771. old_and_new_properties.set(to_underlying(property_id), 1);
  772. for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
  773. if (!old_and_new_properties.get(i))
  774. continue;
  775. auto property_id = static_cast<CSS::PropertyID>(i);
  776. auto old_value = old_properties.get(property_id).value_or({});
  777. auto new_value = new_properties.get(property_id).value_or({});
  778. if (!old_value && !new_value)
  779. continue;
  780. invalidation |= compute_property_invalidation(property_id, old_value, new_value);
  781. }
  782. return invalidation;
  783. }
  784. void KeyframeEffect::update_style_properties()
  785. {
  786. auto target = this->target();
  787. if (!target)
  788. return;
  789. if (pseudo_element_type().has_value()) {
  790. // StyleProperties are not saved for pseudo-elements so there is nothing to patch
  791. target->invalidate_style();
  792. return;
  793. }
  794. auto* style = target->computed_css_values();
  795. if (!style)
  796. return;
  797. auto animated_properties_before_update = style->animated_property_values();
  798. auto& document = target->document();
  799. document.style_computer().collect_animation_into(*target, pseudo_element_type(), *this, *style, CSS::StyleComputer::AnimationRefresh::Yes);
  800. // Traversal of the subtree is necessary to update the animated properties inherited from the target element.
  801. target->for_each_in_subtree_of_type<DOM::Element>([&](auto& element) {
  802. auto* element_style = element.computed_css_values();
  803. if (!element_style || !element.layout_node())
  804. return TraversalDecision::Continue;
  805. for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
  806. if (element_style->is_property_inherited(static_cast<CSS::PropertyID>(i))) {
  807. auto new_value = CSS::StyleComputer::get_inherit_value(document.realm(), static_cast<CSS::PropertyID>(i), &element);
  808. element_style->set_property(static_cast<CSS::PropertyID>(i), *new_value, nullptr, CSS::StyleProperties::Inherited::Yes);
  809. }
  810. }
  811. element.layout_node()->apply_style(*element_style);
  812. return TraversalDecision::Continue;
  813. });
  814. auto invalidation = compute_required_invalidation(animated_properties_before_update, style->animated_property_values());
  815. if (target->layout_node())
  816. target->layout_node()->apply_style(*style);
  817. if (invalidation.relayout)
  818. document.set_needs_layout();
  819. if (invalidation.rebuild_layout_tree)
  820. document.invalidate_layout();
  821. if (invalidation.repaint)
  822. document.set_needs_to_resolve_paint_only_properties();
  823. if (invalidation.rebuild_stacking_context_tree)
  824. document.invalidate_stacking_context_tree();
  825. }
  826. }