LayoutNode.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/Frame.h>
  30. #include <LibWeb/Layout/LayoutBlock.h>
  31. #include <LibWeb/Layout/LayoutDocument.h>
  32. #include <LibWeb/Layout/LayoutNode.h>
  33. #include <LibWeb/Layout/LayoutReplaced.h>
  34. namespace Web {
  35. LayoutNode::LayoutNode(const Node* node)
  36. : m_node(node)
  37. {
  38. if (m_node)
  39. m_node->set_layout_node({}, this);
  40. }
  41. LayoutNode::~LayoutNode()
  42. {
  43. if (m_node && m_node->layout_node() == this)
  44. m_node->set_layout_node({}, nullptr);
  45. }
  46. void LayoutNode::layout(LayoutMode layout_mode)
  47. {
  48. for_each_child([&](auto& child) {
  49. child.layout(layout_mode);
  50. });
  51. }
  52. bool LayoutNode::can_contain_boxes_with_position_absolute() const
  53. {
  54. return style().position() != CSS::Position::Static || is_root();
  55. }
  56. const LayoutBlock* LayoutNode::containing_block() const
  57. {
  58. auto nearest_block_ancestor = [this] {
  59. auto* ancestor = parent();
  60. while (ancestor && !is<LayoutBlock>(*ancestor))
  61. ancestor = ancestor->parent();
  62. return to<LayoutBlock>(ancestor);
  63. };
  64. if (is_text())
  65. return nearest_block_ancestor();
  66. if (is_absolutely_positioned()) {
  67. auto* ancestor = parent();
  68. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  69. ancestor = ancestor->parent();
  70. while (ancestor && (!is<LayoutBlock>(ancestor) || ancestor->is_anonymous()))
  71. ancestor = ancestor->containing_block();
  72. return to<LayoutBlock>(ancestor);
  73. }
  74. if (style().position() == CSS::Position::Fixed)
  75. return &root();
  76. return nearest_block_ancestor();
  77. }
  78. void LayoutNode::render(RenderingContext& context)
  79. {
  80. if (!is_visible())
  81. return;
  82. // TODO: render our border
  83. for_each_child([&](auto& child) {
  84. child.render(context);
  85. });
  86. }
  87. HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position) const
  88. {
  89. HitTestResult result;
  90. for_each_child([&](auto& child) {
  91. auto child_result = child.hit_test(position);
  92. if (child_result.layout_node)
  93. result = child_result;
  94. });
  95. return result;
  96. }
  97. const Document& LayoutNode::document() const
  98. {
  99. if (is_anonymous())
  100. return parent()->document();
  101. return node()->document();
  102. }
  103. Document& LayoutNode::document()
  104. {
  105. if (is_anonymous())
  106. return parent()->document();
  107. // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
  108. return const_cast<Node*>(node())->document();
  109. }
  110. const LayoutDocument& LayoutNode::root() const
  111. {
  112. ASSERT(document().layout_node());
  113. return *document().layout_node();
  114. }
  115. LayoutDocument& LayoutNode::root()
  116. {
  117. ASSERT(document().layout_node());
  118. return *document().layout_node();
  119. }
  120. void LayoutNode::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  121. {
  122. for_each_child([&](auto& child) {
  123. if (child.is_inline()) {
  124. child.split_into_lines(container, layout_mode);
  125. } else {
  126. // FIXME: Support block children of inlines.
  127. }
  128. });
  129. }
  130. void LayoutNode::set_needs_display()
  131. {
  132. auto* frame = document().frame();
  133. ASSERT(frame);
  134. if (auto* block = containing_block()) {
  135. block->for_each_fragment([&](auto& fragment) {
  136. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  137. const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  138. }
  139. return IterationDecision::Continue;
  140. });
  141. }
  142. }
  143. float LayoutNode::font_size() const
  144. {
  145. // FIXME: This doesn't work right for relative font-sizes
  146. auto length = style().length_or_fallback(CSS::PropertyID::FontSize, Length(10, Length::Type::Px));
  147. return length.raw_value();
  148. }
  149. Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
  150. {
  151. if (is_box())
  152. return to<LayoutBox>(*this).absolute_position();
  153. ASSERT(is_inline());
  154. Gfx::FloatPoint position;
  155. if (auto* block = containing_block()) {
  156. block->for_each_fragment([&](auto& fragment) {
  157. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  158. position = fragment.absolute_rect().location();
  159. return IterationDecision::Break;
  160. }
  161. return IterationDecision::Continue;
  162. });
  163. }
  164. return position;
  165. }
  166. bool LayoutNode::is_absolutely_positioned() const
  167. {
  168. return style().position() == CSS::Position::Absolute;
  169. }
  170. }