LayoutNode.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. auto position = style().position();
  67. if (position == CSS::Position::Absolute) {
  68. auto* ancestor = parent();
  69. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  70. ancestor = ancestor->parent();
  71. while (ancestor && (!is<LayoutBlock>(ancestor) || ancestor->is_anonymous()))
  72. ancestor = ancestor->containing_block();
  73. return to<LayoutBlock>(ancestor);
  74. }
  75. if (position == CSS::Position::Fixed)
  76. return &root();
  77. return nearest_block_ancestor();
  78. }
  79. void LayoutNode::paint(PaintContext& context, PaintPhase phase)
  80. {
  81. if (!is_visible())
  82. return;
  83. for_each_child([&](auto& child) {
  84. if (child.is_box() && to<LayoutBox>(child).stacking_context())
  85. return;
  86. child.paint(context, phase);
  87. });
  88. }
  89. HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position) const
  90. {
  91. HitTestResult result;
  92. for_each_child([&](auto& child) {
  93. auto child_result = child.hit_test(position);
  94. if (child_result.layout_node)
  95. result = child_result;
  96. });
  97. return result;
  98. }
  99. const Frame& LayoutNode::frame() const
  100. {
  101. ASSERT(document().frame());
  102. return *document().frame();
  103. }
  104. Frame& LayoutNode::frame()
  105. {
  106. ASSERT(document().frame());
  107. return *document().frame();
  108. }
  109. const Document& LayoutNode::document() const
  110. {
  111. if (is_anonymous())
  112. return parent()->document();
  113. return node()->document();
  114. }
  115. Document& LayoutNode::document()
  116. {
  117. if (is_anonymous())
  118. return parent()->document();
  119. // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
  120. return const_cast<Node*>(node())->document();
  121. }
  122. const LayoutDocument& LayoutNode::root() const
  123. {
  124. ASSERT(document().layout_node());
  125. return *document().layout_node();
  126. }
  127. LayoutDocument& LayoutNode::root()
  128. {
  129. ASSERT(document().layout_node());
  130. return *document().layout_node();
  131. }
  132. void LayoutNode::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  133. {
  134. for_each_child([&](auto& child) {
  135. child.split_into_lines(container, layout_mode);
  136. });
  137. }
  138. void LayoutNode::set_needs_display()
  139. {
  140. if (auto* block = containing_block()) {
  141. block->for_each_fragment([&](auto& fragment) {
  142. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  143. frame().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  144. }
  145. return IterationDecision::Continue;
  146. });
  147. }
  148. }
  149. float LayoutNode::font_size() const
  150. {
  151. // FIXME: This doesn't work right for relative font-sizes
  152. auto length = specified_style().length_or_fallback(CSS::PropertyID::FontSize, Length(10, Length::Type::Px));
  153. return length.raw_value();
  154. }
  155. Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
  156. {
  157. if (is_box())
  158. return to<LayoutBox>(*this).absolute_position();
  159. ASSERT(is_inline());
  160. Gfx::FloatPoint position;
  161. if (auto* block = containing_block()) {
  162. block->for_each_fragment([&](auto& fragment) {
  163. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  164. position = fragment.absolute_rect().location();
  165. return IterationDecision::Break;
  166. }
  167. return IterationDecision::Continue;
  168. });
  169. }
  170. return position;
  171. }
  172. bool LayoutNode::is_absolutely_positioned() const
  173. {
  174. if (!has_style())
  175. return false;
  176. auto position = style().position();
  177. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  178. }
  179. bool LayoutNode::is_fixed_position() const
  180. {
  181. if (!has_style())
  182. return false;
  183. auto position = style().position();
  184. return position == CSS::Position::Fixed;
  185. }
  186. LayoutNodeWithStyle::LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
  187. : LayoutNode(node)
  188. , m_specified_style(move(style))
  189. {
  190. m_has_style = true;
  191. apply_style(this->specified_style());
  192. }
  193. void LayoutNodeWithStyle::apply_style(const StyleProperties& specified_style)
  194. {
  195. auto& style = static_cast<MutableLayoutStyle&>(m_style);
  196. style.set_position(specified_style.position());
  197. style.set_text_align(specified_style.text_align());
  198. auto white_space = specified_style.white_space();
  199. if (white_space.has_value())
  200. style.set_white_space(white_space.value());
  201. style.set_z_index(specified_style.z_index());
  202. style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  203. style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  204. style.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  205. style.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  206. style.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  207. style.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  208. style.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom));
  209. style.set_margin(specified_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom));
  210. style.set_padding(specified_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom));
  211. }
  212. }