KeyframeEffect.cpp 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 composite = TRY(keyframe_object.get("composite"));
  85. if (composite.is_undefined())
  86. composite = JS::PrimitiveString::create(vm, "auto"_string);
  87. auto easing = TRY(keyframe_object.get("easing"));
  88. if (easing.is_undefined())
  89. easing = JS::PrimitiveString::create(vm, "linear"_string);
  90. auto offset = TRY(keyframe_object.get("offset"));
  91. if constexpr (AL == AllowLists::Yes) {
  92. keyframe_output.composite = TRY(convert_value_to_maybe_list(realm, composite, to_composite_operation));
  93. auto easing_maybe_list = TRY(convert_value_to_maybe_list(realm, easing, to_string));
  94. easing_maybe_list.visit(
  95. [&](String const& value) {
  96. keyframe_output.easing = EasingValue { value };
  97. },
  98. [&](Vector<String> const& values) {
  99. Vector<EasingValue> easing_values;
  100. for (auto& easing_value : values)
  101. easing_values.append(easing_value);
  102. keyframe_output.easing = move(easing_values);
  103. });
  104. keyframe_output.offset = TRY(convert_value_to_maybe_list(realm, offset, to_offset));
  105. } else {
  106. keyframe_output.composite = TRY(to_composite_operation(composite));
  107. keyframe_output.easing = TRY(to_string(easing));
  108. keyframe_output.offset = TRY(to_offset(offset));
  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 parser = CSS::Parser::Parser::create(CSS::Parser::ParsingContext(realm), value_string);
  449. if (auto style_value = parser.parse_as_css_value(*property_id)) {
  450. // Handle 'initial' here so we don't have to get the default value of the property every frame in StyleComputer
  451. if (style_value->is_initial())
  452. style_value = CSS::property_initial_value(realm, *property_id);
  453. parsed_properties.set(*property_id, *style_value);
  454. }
  455. }
  456. keyframe.properties.set(move(parsed_properties));
  457. // 2. Let the timing function of frame be the result of parsing the "easing" property on frame using the CSS
  458. // syntax defined for the easing member of the EffectTiming dictionary.
  459. //
  460. // If parsing the "easing" property fails, throw a TypeError and abort this procedure.
  461. auto easing_string = keyframe.easing.get<String>();
  462. auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string);
  463. if (!easing_value)
  464. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) };
  465. keyframe.easing.set(NonnullRefPtr<CSS::CSSStyleValue const> { *easing_value });
  466. }
  467. // 9. Parse each of the values in unused easings using the CSS syntax defined for easing member of the EffectTiming
  468. // interface, and if any of the values fail to parse, throw a TypeError and abort this procedure.
  469. for (auto& unused_easing : unused_easings) {
  470. auto easing_string = unused_easing.get<String>();
  471. auto easing_value = AnimationEffect::parse_easing_string(realm, easing_string);
  472. if (!easing_value)
  473. return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, MUST(String::formatted("Invalid animation easing value: \"{}\"", easing_string)) };
  474. }
  475. return processed_keyframes;
  476. }
  477. // https://www.w3.org/TR/css-animations-2/#keyframe-processing
  478. void KeyframeEffect::generate_initial_and_final_frames(RefPtr<KeyFrameSet> keyframe_set, HashTable<CSS::PropertyID> const& animated_properties)
  479. {
  480. // 1. Find or create the initial keyframe, a keyframe with a keyframe offset of 0%, default timing function
  481. // as its keyframe timing function, and default composite as its keyframe composite.
  482. KeyFrameSet::ResolvedKeyFrame* initial_keyframe;
  483. if (auto existing_keyframe = keyframe_set->keyframes_by_key.find(0)) {
  484. initial_keyframe = existing_keyframe;
  485. } else {
  486. keyframe_set->keyframes_by_key.insert(0, {});
  487. initial_keyframe = keyframe_set->keyframes_by_key.find(0);
  488. }
  489. // 2. For any property in animated properties that is not otherwise present in a keyframe with an offset of
  490. // 0% or one that would be positioned earlier in the used keyframe order, add the computed value of that
  491. // property on element to initial keyframe’s keyframe values.
  492. for (auto property : animated_properties) {
  493. if (!initial_keyframe->properties.contains(property))
  494. initial_keyframe->properties.set(property, KeyFrameSet::UseInitial {});
  495. }
  496. // 3. If initial keyframe’s keyframe values is not empty, prepend initial keyframe to keyframes.
  497. // 4. Repeat for final keyframe, using an offset of 100%, considering keyframes positioned later in the used
  498. // keyframe order, and appending to keyframes.
  499. KeyFrameSet::ResolvedKeyFrame* final_keyframe;
  500. if (auto existing_keyframe = keyframe_set->keyframes_by_key.find(100 * AnimationKeyFrameKeyScaleFactor)) {
  501. final_keyframe = existing_keyframe;
  502. } else {
  503. keyframe_set->keyframes_by_key.insert(100 * AnimationKeyFrameKeyScaleFactor, {});
  504. final_keyframe = keyframe_set->keyframes_by_key.find(100 * AnimationKeyFrameKeyScaleFactor);
  505. }
  506. for (auto property : animated_properties) {
  507. if (!final_keyframe->properties.contains(property))
  508. final_keyframe->properties.set(property, KeyFrameSet::UseInitial {});
  509. }
  510. }
  511. // https://www.w3.org/TR/web-animations-1/#animation-composite-order
  512. int KeyframeEffect::composite_order(JS::NonnullGCPtr<KeyframeEffect> a, JS::NonnullGCPtr<KeyframeEffect> b)
  513. {
  514. // 1. Let the associated animation of an animation effect be the animation associated with the animation effect.
  515. auto a_animation = a->associated_animation();
  516. auto b_animation = b->associated_animation();
  517. // 2. Sort A and B by applying the following conditions in turn until the order is resolved,
  518. // 1. If A and B’s associated animations differ by class, sort by any inter-class composite order defined for
  519. // the corresponding classes.
  520. auto a_class = a_animation->animation_class();
  521. auto b_class = b_animation->animation_class();
  522. // From https://www.w3.org/TR/css-animations-2/#animation-composite-order:
  523. // "CSS Animations with an owning element have a later composite order than CSS Transitions but an earlier
  524. // composite order than animations without a specific animation class."
  525. if (a_class != b_class)
  526. return to_underlying(a_class) - to_underlying(b_class);
  527. // 2. If A and B are still not sorted, sort by any class-specific composite order defined by the common class of
  528. // A and B’s associated animations.
  529. if (auto order = a_animation->class_specific_composite_order(*b_animation); order.has_value())
  530. return order.value();
  531. // 3. If A and B are still not sorted, sort by the position of their associated animations in the global
  532. // animation list.
  533. return a_animation->global_animation_list_order() - b_animation->global_animation_list_order();
  534. }
  535. JS::NonnullGCPtr<KeyframeEffect> KeyframeEffect::create(JS::Realm& realm)
  536. {
  537. return realm.heap().allocate<KeyframeEffect>(realm, realm);
  538. }
  539. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-keyframeeffect
  540. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> KeyframeEffect::construct_impl(
  541. JS::Realm& realm,
  542. JS::Handle<DOM::Element> const& target,
  543. Optional<JS::Handle<JS::Object>> const& keyframes,
  544. Variant<double, KeyframeEffectOptions> options)
  545. {
  546. auto& vm = realm.vm();
  547. // 1. Create a new KeyframeEffect object, effect.
  548. auto effect = vm.heap().allocate<KeyframeEffect>(realm, realm);
  549. // 2. Set the target element of effect to target.
  550. effect->set_target(target);
  551. // 3. Set the target pseudo-selector to the result corresponding to the first matching condition from below.
  552. // If options is a KeyframeEffectOptions object with a pseudoElement property,
  553. if (options.has<KeyframeEffectOptions>()) {
  554. // Set the target pseudo-selector to the value of the pseudoElement property.
  555. //
  556. // When assigning this property, the error-handling defined for the pseudoElement setter on the interface is
  557. // applied. If the setter requires an exception to be thrown, this procedure must throw the same exception and
  558. // abort all further steps.
  559. TRY(effect->set_pseudo_element(options.get<KeyframeEffectOptions>().pseudo_element));
  560. }
  561. // Otherwise,
  562. else {
  563. // Set the target pseudo-selector to null.
  564. // Note: This is the default when constructed
  565. }
  566. // 4. Let timing input be the result corresponding to the first matching condition from below.
  567. KeyframeEffectOptions timing_input;
  568. // If options is a KeyframeEffectOptions object,
  569. if (options.has<KeyframeEffectOptions>()) {
  570. // Let timing input be options.
  571. timing_input = options.get<KeyframeEffectOptions>();
  572. }
  573. // Otherwise (if options is a double),
  574. else {
  575. // Let timing input be a new EffectTiming object with all members set to their default values and duration set
  576. // to options.
  577. timing_input.duration = options.get<double>();
  578. }
  579. // 5. Call the procedure to update the timing properties of an animation effect of effect from timing input.
  580. // If that procedure causes an exception to be thrown, propagate the exception and abort this procedure.
  581. TRY(effect->update_timing(timing_input.to_optional_effect_timing()));
  582. // 6. If options is a KeyframeEffectOptions object, assign the composite property of effect to the corresponding
  583. // value from options.
  584. //
  585. // When assigning this property, the error-handling defined for the corresponding setter on the KeyframeEffect
  586. // interface is applied. If the setter requires an exception to be thrown for the value specified by options,
  587. // this procedure must throw the same exception and abort all further steps.
  588. if (options.has<KeyframeEffectOptions>())
  589. effect->set_composite(options.get<KeyframeEffectOptions>().composite);
  590. // 7. Initialize the set of keyframes by performing the procedure defined for setKeyframes() passing keyframes as
  591. // the input.
  592. TRY(effect->set_keyframes(keyframes));
  593. return effect;
  594. }
  595. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-keyframeeffect-source
  596. WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyframeEffect>> KeyframeEffect::construct_impl(JS::Realm& realm, JS::NonnullGCPtr<KeyframeEffect> source)
  597. {
  598. auto& vm = realm.vm();
  599. // 1. Create a new KeyframeEffect object, effect.
  600. auto effect = vm.heap().allocate<KeyframeEffect>(realm, realm);
  601. // 2. Set the following properties of effect using the corresponding values of source:
  602. // - effect target,
  603. effect->m_target_element = source->target();
  604. // - keyframes,
  605. effect->m_keyframes = source->m_keyframes;
  606. // - composite operation, and
  607. effect->set_composite(source->composite());
  608. // - all specified timing properties:
  609. // - start delay,
  610. effect->m_start_delay = source->m_start_delay;
  611. // - end delay,
  612. effect->m_end_delay = source->m_end_delay;
  613. // - fill mode,
  614. effect->m_fill_mode = source->m_fill_mode;
  615. // - iteration start,
  616. effect->m_iteration_start = source->m_iteration_start;
  617. // - iteration count,
  618. effect->m_iteration_count = source->m_iteration_count;
  619. // - iteration duration,
  620. effect->m_iteration_duration = source->m_iteration_duration;
  621. // - playback direction, and
  622. effect->m_playback_direction = source->m_playback_direction;
  623. // - timing function.
  624. effect->m_timing_function = source->m_timing_function;
  625. return effect;
  626. }
  627. void KeyframeEffect::set_target(DOM::Element* target)
  628. {
  629. if (auto animation = this->associated_animation()) {
  630. if (m_target_element)
  631. m_target_element->disassociate_with_animation(*animation);
  632. if (target)
  633. target->associate_with_animation(*animation);
  634. }
  635. m_target_element = target;
  636. }
  637. Optional<String> KeyframeEffect::pseudo_element() const
  638. {
  639. if (!m_target_pseudo_selector.has_value())
  640. return {};
  641. return MUST(String::formatted("::{}", m_target_pseudo_selector->name()));
  642. }
  643. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-pseudoelement
  644. WebIDL::ExceptionOr<void> KeyframeEffect::set_pseudo_element(Optional<String> pseudo_element)
  645. {
  646. auto& realm = this->realm();
  647. // On setting, sets the target pseudo-selector of the animation effect to the provided value after applying the
  648. // following exceptions:
  649. // FIXME:
  650. // - If one of the legacy Selectors Level 2 single-colon selectors (':before', ':after', ':first-letter', or
  651. // ':first-line') is specified, the target pseudo-selector must be set to the equivalent two-colon selector
  652. // (e.g. '::before').
  653. if (pseudo_element.has_value()) {
  654. auto value = pseudo_element.value();
  655. if (value == ":before" || value == ":after" || value == ":first-letter" || value == ":first-line") {
  656. m_target_pseudo_selector = CSS::Selector::PseudoElement::from_string(MUST(value.substring_from_byte_offset(1)));
  657. return {};
  658. }
  659. }
  660. // - If the provided value is not null and is an invalid <pseudo-element-selector>, the user agent must throw a
  661. // DOMException with error name SyntaxError and leave the target pseudo-selector of this animation effect
  662. // unchanged.
  663. if (pseudo_element.has_value()) {
  664. if (pseudo_element->starts_with_bytes("::"sv)) {
  665. if (auto value = CSS::Selector::PseudoElement::from_string(MUST(pseudo_element->substring_from_byte_offset(2))); value.has_value()) {
  666. m_target_pseudo_selector = value;
  667. return {};
  668. }
  669. }
  670. return WebIDL::SyntaxError::create(realm, MUST(String::formatted("Invalid pseudo-element selector: \"{}\"", pseudo_element.value())));
  671. }
  672. m_target_pseudo_selector = {};
  673. return {};
  674. }
  675. Optional<CSS::Selector::PseudoElement::Type> KeyframeEffect::pseudo_element_type() const
  676. {
  677. if (!m_target_pseudo_selector.has_value())
  678. return {};
  679. return m_target_pseudo_selector->type();
  680. }
  681. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-getkeyframes
  682. WebIDL::ExceptionOr<JS::MarkedVector<JS::Object*>> KeyframeEffect::get_keyframes()
  683. {
  684. if (m_keyframe_objects.size() != m_keyframes.size()) {
  685. auto& vm = this->vm();
  686. auto& realm = this->realm();
  687. // Recalculate the keyframe objects
  688. VERIFY(m_keyframe_objects.size() == 0);
  689. for (auto& keyframe : m_keyframes) {
  690. auto object = JS::Object::create(realm, realm.intrinsics().object_prototype());
  691. TRY(object->set(vm.names.offset, keyframe.offset.has_value() ? JS::Value(keyframe.offset.value()) : JS::js_null(), ShouldThrowExceptions::Yes));
  692. TRY(object->set(vm.names.computedOffset, JS::Value(keyframe.computed_offset.value()), ShouldThrowExceptions::Yes));
  693. auto easing_value = keyframe.easing.get<NonnullRefPtr<CSS::CSSStyleValue const>>();
  694. TRY(object->set(vm.names.easing, JS::PrimitiveString::create(vm, easing_value->to_string()), ShouldThrowExceptions::Yes));
  695. if (keyframe.composite == Bindings::CompositeOperationOrAuto::Replace) {
  696. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "replace"sv), ShouldThrowExceptions::Yes));
  697. } else if (keyframe.composite == Bindings::CompositeOperationOrAuto::Add) {
  698. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "add"sv), ShouldThrowExceptions::Yes));
  699. } else if (keyframe.composite == Bindings::CompositeOperationOrAuto::Accumulate) {
  700. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "accumulate"sv), ShouldThrowExceptions::Yes));
  701. } else {
  702. TRY(object->set(vm.names.composite, JS::PrimitiveString::create(vm, "auto"sv), ShouldThrowExceptions::Yes));
  703. }
  704. for (auto const& [id, value] : keyframe.parsed_properties()) {
  705. auto value_string = JS::PrimitiveString::create(vm, value->to_string());
  706. TRY(object->set(JS::PropertyKey(DeprecatedFlyString(CSS::camel_case_string_from_property_id(id))), value_string, ShouldThrowExceptions::Yes));
  707. }
  708. m_keyframe_objects.append(object);
  709. }
  710. }
  711. JS::MarkedVector<JS::Object*> keyframes { heap() };
  712. for (auto const& keyframe : m_keyframe_objects)
  713. keyframes.append(keyframe);
  714. return keyframes;
  715. }
  716. // https://www.w3.org/TR/web-animations-1/#dom-keyframeeffect-setkeyframes
  717. WebIDL::ExceptionOr<void> KeyframeEffect::set_keyframes(Optional<JS::Handle<JS::Object>> const& keyframe_object)
  718. {
  719. m_keyframe_objects.clear();
  720. m_keyframes = TRY(process_a_keyframes_argument(realm(), keyframe_object.has_value() ? JS::GCPtr { keyframe_object->ptr() } : JS::GCPtr<Object> {}));
  721. // FIXME: After processing the keyframe argument, we need to turn the set of keyframes into a set of computed
  722. // keyframes using the procedure outlined in the second half of
  723. // https://www.w3.org/TR/web-animations-1/#calculating-computed-keyframes. For now, just compute the
  724. // missing keyframe offsets
  725. compute_missing_keyframe_offsets(m_keyframes);
  726. auto keyframe_set = adopt_ref(*new KeyFrameSet);
  727. m_target_properties.clear();
  728. auto target = this->target();
  729. for (auto& keyframe : m_keyframes) {
  730. Animations::KeyframeEffect::KeyFrameSet::ResolvedKeyFrame resolved_keyframe;
  731. auto key = static_cast<u64>(keyframe.computed_offset.value() * 100 * AnimationKeyFrameKeyScaleFactor);
  732. for (auto [property_id, property_value] : keyframe.parsed_properties()) {
  733. if (property_value->is_unresolved() && target)
  734. property_value = CSS::Parser::Parser::resolve_unresolved_style_value(CSS::Parser::ParsingContext { target->document() }, *target, pseudo_element_type(), property_id, property_value->as_unresolved());
  735. CSS::StyleComputer::for_each_property_expanding_shorthands(property_id, property_value, CSS::StyleComputer::AllowUnresolved::Yes, [&](CSS::PropertyID shorthand_id, CSS::CSSStyleValue const& shorthand_value) {
  736. m_target_properties.set(shorthand_id);
  737. resolved_keyframe.properties.set(shorthand_id, NonnullRefPtr<CSS::CSSStyleValue const> { shorthand_value });
  738. });
  739. }
  740. keyframe_set->keyframes_by_key.insert(key, resolved_keyframe);
  741. }
  742. generate_initial_and_final_frames(keyframe_set, m_target_properties);
  743. m_key_frame_set = keyframe_set;
  744. return {};
  745. }
  746. KeyframeEffect::KeyframeEffect(JS::Realm& realm)
  747. : AnimationEffect(realm)
  748. {
  749. }
  750. void KeyframeEffect::initialize(JS::Realm& realm)
  751. {
  752. Base::initialize(realm);
  753. WEB_SET_PROTOTYPE_FOR_INTERFACE(KeyframeEffect);
  754. }
  755. void KeyframeEffect::visit_edges(Cell::Visitor& visitor)
  756. {
  757. Base::visit_edges(visitor);
  758. visitor.visit(m_target_element);
  759. visitor.visit(m_keyframe_objects);
  760. }
  761. static CSS::RequiredInvalidationAfterStyleChange compute_required_invalidation(HashMap<CSS::PropertyID, NonnullRefPtr<CSS::CSSStyleValue const>> const& old_properties, HashMap<CSS::PropertyID, NonnullRefPtr<CSS::CSSStyleValue const>> const& new_properties)
  762. {
  763. CSS::RequiredInvalidationAfterStyleChange invalidation;
  764. auto old_and_new_properties = MUST(Bitmap::create(to_underlying(CSS::last_property_id) + 1, 0));
  765. for (auto const& [property_id, _] : old_properties)
  766. old_and_new_properties.set(to_underlying(property_id), 1);
  767. for (auto const& [property_id, _] : new_properties)
  768. old_and_new_properties.set(to_underlying(property_id), 1);
  769. for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
  770. if (!old_and_new_properties.get(i))
  771. continue;
  772. auto property_id = static_cast<CSS::PropertyID>(i);
  773. auto old_value = old_properties.get(property_id).value_or({});
  774. auto new_value = new_properties.get(property_id).value_or({});
  775. if (!old_value && !new_value)
  776. continue;
  777. invalidation |= compute_property_invalidation(property_id, old_value, new_value);
  778. }
  779. return invalidation;
  780. }
  781. void KeyframeEffect::update_style_properties()
  782. {
  783. auto target = this->target();
  784. if (!target)
  785. return;
  786. Optional<CSS::StyleProperties&> style = {};
  787. if (!pseudo_element_type().has_value())
  788. style = target->computed_css_values();
  789. else
  790. style = target->pseudo_element_computed_css_values(pseudo_element_type().value());
  791. if (!style.has_value())
  792. return;
  793. auto animated_properties_before_update = style->animated_property_values();
  794. auto& document = target->document();
  795. document.style_computer().collect_animation_into(*target, pseudo_element_type(), *this, *style, CSS::StyleComputer::AnimationRefresh::Yes);
  796. // Traversal of the subtree is necessary to update the animated properties inherited from the target element.
  797. target->for_each_in_subtree_of_type<DOM::Element>([&](auto& element) {
  798. auto element_style = element.computed_css_values();
  799. if (!element_style.has_value() || !element.layout_node())
  800. return TraversalDecision::Continue;
  801. for (auto i = to_underlying(CSS::first_property_id); i <= to_underlying(CSS::last_property_id); ++i) {
  802. if (element_style->is_property_inherited(static_cast<CSS::PropertyID>(i))) {
  803. auto new_value = CSS::StyleComputer::get_inherit_value(document.realm(), static_cast<CSS::PropertyID>(i), &element);
  804. element_style->set_property(static_cast<CSS::PropertyID>(i), *new_value, CSS::StyleProperties::Inherited::Yes);
  805. }
  806. }
  807. element.layout_node()->apply_style(*element_style);
  808. return TraversalDecision::Continue;
  809. });
  810. auto invalidation = compute_required_invalidation(animated_properties_before_update, style->animated_property_values());
  811. if (!pseudo_element_type().has_value()) {
  812. if (target->layout_node())
  813. target->layout_node()->apply_style(*style);
  814. } else {
  815. auto pseudo_element_node = target->get_pseudo_element_node(pseudo_element_type().value());
  816. if (auto* node_with_style = dynamic_cast<Layout::NodeWithStyle*>(pseudo_element_node.ptr())) {
  817. node_with_style->apply_style(*style);
  818. }
  819. }
  820. if (invalidation.relayout)
  821. document.set_needs_layout();
  822. if (invalidation.rebuild_layout_tree)
  823. document.invalidate_layout_tree();
  824. if (invalidation.repaint)
  825. document.set_needs_to_resolve_paint_only_properties();
  826. if (invalidation.rebuild_stacking_context_tree)
  827. document.invalidate_stacking_context_tree();
  828. }
  829. }