InitialContainingBlock.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Range.h>
  7. #include <LibWeb/Dump.h>
  8. #include <LibWeb/Layout/InitialContainingBlock.h>
  9. #include <LibWeb/Painting/PaintableBox.h>
  10. #include <LibWeb/Painting/StackingContext.h>
  11. namespace Web::Layout {
  12. InitialContainingBlock::InitialContainingBlock(DOM::Document& document, NonnullRefPtr<CSS::StyleProperties> style)
  13. : BlockContainer(document, &document, move(style))
  14. {
  15. }
  16. InitialContainingBlock::~InitialContainingBlock() = default;
  17. JS::GCPtr<Selection::Selection> InitialContainingBlock::selection() const
  18. {
  19. return const_cast<DOM::Document&>(document()).get_selection();
  20. }
  21. void InitialContainingBlock::build_stacking_context_tree_if_needed()
  22. {
  23. if (paint_box()->stacking_context())
  24. return;
  25. build_stacking_context_tree();
  26. }
  27. void InitialContainingBlock::build_stacking_context_tree()
  28. {
  29. const_cast<Painting::PaintableWithLines*>(paint_box())->set_stacking_context(make<Painting::StackingContext>(*this, nullptr));
  30. for_each_in_subtree_of_type<Box>([&](Box& box) {
  31. if (!box.paint_box())
  32. return IterationDecision::Continue;
  33. const_cast<Painting::PaintableBox*>(box.paint_box())->invalidate_stacking_context();
  34. if (!box.establishes_stacking_context()) {
  35. VERIFY(!box.paint_box()->stacking_context());
  36. return IterationDecision::Continue;
  37. }
  38. auto* parent_context = const_cast<Painting::PaintableBox*>(box.paint_box())->enclosing_stacking_context();
  39. VERIFY(parent_context);
  40. const_cast<Painting::PaintableBox*>(box.paint_box())->set_stacking_context(make<Painting::StackingContext>(box, parent_context));
  41. return IterationDecision::Continue;
  42. });
  43. const_cast<Painting::PaintableWithLines*>(paint_box())->stacking_context()->sort();
  44. }
  45. void InitialContainingBlock::paint_all_phases(PaintContext& context)
  46. {
  47. build_stacking_context_tree_if_needed();
  48. context.painter().fill_rect(context.enclosing_device_rect(paint_box()->absolute_rect()).to_type<int>(), document().background_color(context.palette()));
  49. context.painter().translate(-context.device_viewport_rect().location().to_type<int>());
  50. paint_box()->stacking_context()->paint(context);
  51. }
  52. void InitialContainingBlock::recompute_selection_states()
  53. {
  54. // 1. Start by resetting the selection state of all layout nodes to None.
  55. for_each_in_inclusive_subtree([&](auto& layout_node) {
  56. layout_node.set_selection_state(SelectionState::None);
  57. return IterationDecision::Continue;
  58. });
  59. // 2. If there is no active Selection or selected Range, return.
  60. auto selection = document().get_selection();
  61. if (!selection)
  62. return;
  63. auto range = selection->range();
  64. if (!range)
  65. return;
  66. auto* start_container = range->start_container();
  67. auto* end_container = range->end_container();
  68. // 3. If the selection starts and ends in the same text node, mark it as StartAndEnd and return.
  69. if (start_container == end_container && is<DOM::Text>(*start_container)) {
  70. if (auto* layout_node = start_container->layout_node()) {
  71. layout_node->set_selection_state(SelectionState::StartAndEnd);
  72. }
  73. return;
  74. }
  75. // 4. Mark the selection start node as Start (if text) or Full (if anything else).
  76. if (auto* layout_node = start_container->layout_node()) {
  77. if (is<DOM::Text>(*start_container))
  78. layout_node->set_selection_state(SelectionState::Start);
  79. else
  80. layout_node->set_selection_state(SelectionState::Full);
  81. }
  82. // 5. Mark the selection end node as End (if text) or Full (if anything else).
  83. if (auto* layout_node = end_container->layout_node()) {
  84. if (is<DOM::Text>(*end_container))
  85. layout_node->set_selection_state(SelectionState::End);
  86. else
  87. layout_node->set_selection_state(SelectionState::Full);
  88. }
  89. // 6. Mark the nodes between start node and end node (in tree order) as Full.
  90. for (auto* node = start_container->next_in_pre_order(); node && node != end_container; node = node->next_in_pre_order()) {
  91. if (auto* layout_node = node->layout_node())
  92. layout_node->set_selection_state(SelectionState::Full);
  93. }
  94. }
  95. }