| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com> | 
					
						
							| 
									
										
										
										
											2025-05-26 10:34:26 -07:00
										 |  |  |  * Copyright (c) 2025, Manuel Zahariev <manuel@duck.com> | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-20 11:32:17 +01:00
										 |  |  | #include <LibWeb/CSS/ComputedProperties.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  | #include <LibWeb/CSS/StyleInvalidation.h>
 | 
					
						
							| 
									
										
										
										
											2025-01-27 21:09:12 +01:00
										 |  |  | #include <LibWeb/CSS/StyleValues/CSSKeywordValue.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-14 11:10:54 +01:00
										 |  |  | RequiredInvalidationAfterStyleChange compute_property_invalidation(CSS::PropertyID property_id, RefPtr<CSSStyleValue const> const& old_value, RefPtr<CSSStyleValue const> const& new_value) | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     RequiredInvalidationAfterStyleChange invalidation; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool const property_value_changed = (!old_value || !new_value) || *old_value != *new_value; | 
					
						
							|  |  |  |     if (!property_value_changed) | 
					
						
							|  |  |  |         return invalidation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-20 12:03:44 +02:00
										 |  |  |     // NOTE: If the computed CSS display, position, content, or content-visibility property changes, we have to rebuild the entire layout tree.
 | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |     //       In the future, we should figure out ways to rebuild a smaller part of the tree.
 | 
					
						
							| 
									
										
										
										
											2025-05-20 12:03:44 +02:00
										 |  |  |     if (AK::first_is_one_of(property_id, CSS::PropertyID::Display, CSS::PropertyID::Position, CSS::PropertyID::Content, CSS::PropertyID::ContentVisibility)) { | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |         return RequiredInvalidationAfterStyleChange::full(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-08 17:54:04 +01:00
										 |  |  |     // NOTE: If the text-transform property changes, it may affect layout. Furthermore, since the
 | 
					
						
							|  |  |  |     //       Layout::TextNode caches the post-transform text, we have to update the layout tree.
 | 
					
						
							|  |  |  |     if (property_id == CSS::PropertyID::TextTransform) { | 
					
						
							|  |  |  |         invalidation.rebuild_layout_tree = true; | 
					
						
							|  |  |  |         invalidation.relayout = true; | 
					
						
							|  |  |  |         invalidation.repaint = true; | 
					
						
							|  |  |  |         return invalidation; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |     // NOTE: If one of the overflow properties change, we rebuild the entire layout tree.
 | 
					
						
							|  |  |  |     //       This ensures that overflow propagation from root/body to viewport happens correctly.
 | 
					
						
							|  |  |  |     //       In the future, we can make this invalidation narrower.
 | 
					
						
							|  |  |  |     if (property_id == CSS::PropertyID::OverflowX || property_id == CSS::PropertyID::OverflowY) { | 
					
						
							|  |  |  |         return RequiredInvalidationAfterStyleChange::full(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-26 10:34:26 -07:00
										 |  |  |     if (AK::first_is_one_of(property_id, CSS::PropertyID::CounterReset, CSS::PropertyID::CounterSet, CSS::PropertyID::CounterIncrement)) { | 
					
						
							|  |  |  |         invalidation.rebuild_layout_tree = property_value_changed; | 
					
						
							|  |  |  |         return invalidation; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |     // OPTIMIZATION: Special handling for CSS `visibility`:
 | 
					
						
							|  |  |  |     if (property_id == CSS::PropertyID::Visibility) { | 
					
						
							|  |  |  |         // We don't need to relayout if the visibility changes from visible to hidden or vice versa. Only collapse requires relayout.
 | 
					
						
							| 
									
										
										
										
											2024-08-14 14:06:03 +01:00
										 |  |  |         if ((old_value && old_value->to_keyword() == CSS::Keyword::Collapse) != (new_value && new_value->to_keyword() == CSS::Keyword::Collapse)) | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |             invalidation.relayout = true; | 
					
						
							|  |  |  |         // Of course, we still have to repaint on any visibility change.
 | 
					
						
							|  |  |  |         invalidation.repaint = true; | 
					
						
							|  |  |  |     } else if (CSS::property_affects_layout(property_id)) { | 
					
						
							|  |  |  |         invalidation.relayout = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (property_id == CSS::PropertyID::Opacity && old_value && new_value) { | 
					
						
							|  |  |  |         // OPTIMIZATION: An element creates a stacking context when its opacity changes from 1 to less than 1
 | 
					
						
							|  |  |  |         //               and stops to create one when opacity returns to 1. So stacking context tree rebuild is
 | 
					
						
							|  |  |  |         //               not required for opacity changes within the range below 1.
 | 
					
						
							| 
									
										
										
										
											2024-12-20 11:32:17 +01:00
										 |  |  |         auto old_value_opacity = CSS::ComputedProperties::resolve_opacity_value(*old_value); | 
					
						
							|  |  |  |         auto new_value_opacity = CSS::ComputedProperties::resolve_opacity_value(*new_value); | 
					
						
							| 
									
										
										
										
											2024-03-19 12:02:06 +01:00
										 |  |  |         if (old_value_opacity != new_value_opacity && (old_value_opacity == 1 || new_value_opacity == 1)) { | 
					
						
							|  |  |  |             invalidation.rebuild_stacking_context_tree = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } else if (CSS::property_affects_stacking_context(property_id)) { | 
					
						
							|  |  |  |         invalidation.rebuild_stacking_context_tree = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     invalidation.repaint = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return invalidation; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |