LayoutNode.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Painter.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/DOM/Element.h>
  29. #include <LibWeb/Frame.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutNode.h>
  32. namespace Web {
  33. LayoutNode::LayoutNode(const Node* node)
  34. : m_node(node)
  35. {
  36. if (m_node)
  37. m_node->set_layout_node({}, this);
  38. }
  39. LayoutNode::~LayoutNode()
  40. {
  41. if (m_node && m_node->layout_node() == this)
  42. m_node->set_layout_node({}, nullptr);
  43. }
  44. void LayoutNode::layout(LayoutMode layout_mode)
  45. {
  46. for_each_child([&](auto& child) {
  47. child.layout(layout_mode);
  48. });
  49. }
  50. const LayoutBlock* LayoutNode::containing_block() const
  51. {
  52. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  53. if (is<LayoutBlock>(*ancestor))
  54. return to<LayoutBlock>(ancestor);
  55. }
  56. return nullptr;
  57. }
  58. void LayoutNode::render(RenderingContext& context)
  59. {
  60. if (!is_visible())
  61. return;
  62. // TODO: render our border
  63. for_each_child([&](auto& child) {
  64. child.render(context);
  65. });
  66. }
  67. HitTestResult LayoutNode::hit_test(const Gfx::Point& position) const
  68. {
  69. HitTestResult result;
  70. for_each_child([&](auto& child) {
  71. auto child_result = child.hit_test(position);
  72. if (child_result.layout_node)
  73. result = child_result;
  74. });
  75. return result;
  76. }
  77. const Document& LayoutNode::document() const
  78. {
  79. if (is_anonymous())
  80. return parent()->document();
  81. return node()->document();
  82. }
  83. Document& LayoutNode::document()
  84. {
  85. if (is_anonymous())
  86. return parent()->document();
  87. // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
  88. return const_cast<Node*>(node())->document();
  89. }
  90. const LayoutDocument& LayoutNode::root() const
  91. {
  92. ASSERT(document().layout_node());
  93. return *document().layout_node();
  94. }
  95. LayoutDocument& LayoutNode::root()
  96. {
  97. ASSERT(document().layout_node());
  98. return *document().layout_node();
  99. }
  100. void LayoutNode::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  101. {
  102. for_each_child([&](auto& child) {
  103. if (child.is_inline()) {
  104. child.split_into_lines(container, layout_mode);
  105. } else {
  106. // FIXME: Support block children of inlines.
  107. }
  108. });
  109. }
  110. void LayoutNode::set_needs_display()
  111. {
  112. auto* frame = document().frame();
  113. ASSERT(frame);
  114. if (auto* block = containing_block()) {
  115. block->for_each_fragment([&](auto& fragment) {
  116. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  117. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(fragment.rect()));
  118. }
  119. return IterationDecision::Continue;
  120. });
  121. }
  122. }
  123. Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
  124. {
  125. if (is_box())
  126. return to<LayoutBox>(*this).position();
  127. ASSERT(is_inline());
  128. Gfx::FloatPoint position;
  129. if (auto* block = containing_block()) {
  130. block->for_each_fragment([&](auto& fragment) {
  131. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  132. position = fragment.rect().location();
  133. return IterationDecision::Break;
  134. }
  135. return IterationDecision::Continue;
  136. });
  137. }
  138. return position;
  139. }
  140. }