| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  |  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-07 13:46:20 +01:00
										 |  |  | #include <LibGfx/Painter.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/Dump.h>
 | 
					
						
							| 
									
										
										
										
											2021-11-18 15:01:28 +01:00
										 |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | #include <LibWeb/Layout/InitialContainingBlock.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-18 21:39:27 +02:00
										 |  |  | #include <LibWeb/Painting/StackingContext.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  | namespace Web::Layout { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style) | 
					
						
							| 
									
										
										
										
											2021-10-06 20:02:41 +02:00
										 |  |  |     : BlockContainer(document, &document, move(style)) | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | InitialContainingBlock::~InitialContainingBlock() | 
					
						
							| 
									
										
										
										
											2019-06-15 22:49:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-06-16 21:35:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | void InitialContainingBlock::build_stacking_context_tree() | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (stacking_context()) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     set_stacking_context(make<StackingContext>(*this, nullptr)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 18:38:10 +01:00
										 |  |  |     for_each_in_inclusive_subtree_of_type<Box>([&](Box& box) { | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  |         if (&box == this) | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         if (!box.establishes_stacking_context()) { | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |             VERIFY(!box.stacking_context()); | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         auto* parent_context = box.enclosing_stacking_context(); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(parent_context); | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  |         box.set_stacking_context(make<StackingContext>(box, parent_context)); | 
					
						
							|  |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | void InitialContainingBlock::paint_all_phases(PaintContext& context) | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-18 02:58:01 +02:00
										 |  |  |     context.painter().fill_rect(enclosing_int_rect(absolute_rect()), context.palette().base()); | 
					
						
							| 
									
										
										
										
											2021-05-13 09:39:30 -04:00
										 |  |  |     context.painter().translate(-context.viewport_rect().location()); | 
					
						
							| 
									
										
										
										
											2021-05-07 19:03:25 +03:00
										 |  |  |     stacking_context()->paint(context); | 
					
						
							| 
									
										
										
										
											2020-06-15 17:29:35 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | HitTestResult InitialContainingBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const | 
					
						
							| 
									
										
										
										
											2020-07-01 19:02:28 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-08-05 16:55:56 +02:00
										 |  |  |     return stacking_context()->hit_test(position, type); | 
					
						
							| 
									
										
										
										
											2020-07-01 19:02:28 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | void InitialContainingBlock::recompute_selection_states() | 
					
						
							| 
									
										
										
										
											2020-08-21 17:50:41 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     SelectionState state = SelectionState::None; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     auto selection = this->selection().normalized(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-06 18:38:10 +01:00
										 |  |  |     for_each_in_inclusive_subtree([&](auto& layout_node) { | 
					
						
							| 
									
										
										
										
											2020-08-21 17:50:41 +02:00
										 |  |  |         if (!selection.is_valid()) { | 
					
						
							|  |  |  |             // Everything gets SelectionState::None.
 | 
					
						
							|  |  |  |         } else if (&layout_node == selection.start().layout_node && &layout_node == selection.end().layout_node) { | 
					
						
							|  |  |  |             state = SelectionState::StartAndEnd; | 
					
						
							|  |  |  |         } else if (&layout_node == selection.start().layout_node) { | 
					
						
							|  |  |  |             state = SelectionState::Start; | 
					
						
							|  |  |  |         } else if (&layout_node == selection.end().layout_node) { | 
					
						
							|  |  |  |             state = SelectionState::End; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (state == SelectionState::Start) | 
					
						
							|  |  |  |                 state = SelectionState::Full; | 
					
						
							|  |  |  |             else if (state == SelectionState::End || state == SelectionState::StartAndEnd) | 
					
						
							|  |  |  |                 state = SelectionState::None; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         layout_node.set_selection_state(state); | 
					
						
							|  |  |  |         return IterationDecision::Continue; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | void InitialContainingBlock::set_selection(const LayoutRange& selection) | 
					
						
							| 
									
										
										
										
											2020-08-21 17:54:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_selection = selection; | 
					
						
							|  |  |  |     recompute_selection_states(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-08 11:27:46 +02:00
										 |  |  | void InitialContainingBlock::set_selection_end(const LayoutPosition& position) | 
					
						
							| 
									
										
										
										
											2020-08-21 17:54:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     m_selection.set_end(position); | 
					
						
							|  |  |  |     recompute_selection_states(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |