LayoutNode.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(DOM::Document& document, const DOM::Node* node)
  36. : m_document(document)
  37. , m_node(node)
  38. {
  39. if (m_node)
  40. m_node->set_layout_node({}, this);
  41. }
  42. LayoutNode::~LayoutNode()
  43. {
  44. if (m_node && m_node->layout_node() == this)
  45. m_node->set_layout_node({}, nullptr);
  46. }
  47. void LayoutNode::layout(LayoutMode layout_mode)
  48. {
  49. for_each_child([&](auto& child) {
  50. child.layout(layout_mode);
  51. });
  52. }
  53. bool LayoutNode::can_contain_boxes_with_position_absolute() const
  54. {
  55. return style().position() != CSS::Position::Static || is_root();
  56. }
  57. const LayoutBlock* LayoutNode::containing_block() const
  58. {
  59. auto nearest_block_ancestor = [this] {
  60. auto* ancestor = parent();
  61. while (ancestor && !is<LayoutBlock>(*ancestor))
  62. ancestor = ancestor->parent();
  63. return downcast<LayoutBlock>(ancestor);
  64. };
  65. if (is_text())
  66. return nearest_block_ancestor();
  67. auto position = style().position();
  68. if (position == CSS::Position::Absolute) {
  69. auto* ancestor = parent();
  70. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  71. ancestor = ancestor->parent();
  72. while (ancestor && (!is<LayoutBlock>(ancestor) || ancestor->is_anonymous()))
  73. ancestor = ancestor->containing_block();
  74. return downcast<LayoutBlock>(ancestor);
  75. }
  76. if (position == CSS::Position::Fixed)
  77. return &root();
  78. return nearest_block_ancestor();
  79. }
  80. void LayoutNode::paint(PaintContext& context, PaintPhase phase)
  81. {
  82. if (!is_visible())
  83. return;
  84. for_each_child([&](auto& child) {
  85. if (child.is_box() && downcast<LayoutBox>(child).stacking_context())
  86. return;
  87. child.paint(context, phase);
  88. });
  89. }
  90. HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position) const
  91. {
  92. HitTestResult result;
  93. for_each_child([&](auto& child) {
  94. // Skip over children that establish their own stacking context.
  95. // The outer loop who called us will take care of those.
  96. if (is<LayoutBox>(child) && downcast<LayoutBox>(child).stacking_context())
  97. return;
  98. auto child_result = child.hit_test(position);
  99. if (child_result.layout_node)
  100. result = child_result;
  101. });
  102. return result;
  103. }
  104. const Frame& LayoutNode::frame() const
  105. {
  106. ASSERT(document().frame());
  107. return *document().frame();
  108. }
  109. Frame& LayoutNode::frame()
  110. {
  111. ASSERT(document().frame());
  112. return *document().frame();
  113. }
  114. const LayoutDocument& LayoutNode::root() const
  115. {
  116. ASSERT(document().layout_node());
  117. return *document().layout_node();
  118. }
  119. LayoutDocument& LayoutNode::root()
  120. {
  121. ASSERT(document().layout_node());
  122. return *document().layout_node();
  123. }
  124. void LayoutNode::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  125. {
  126. for_each_child([&](auto& child) {
  127. child.split_into_lines(container, layout_mode);
  128. });
  129. }
  130. void LayoutNode::set_needs_display()
  131. {
  132. if (auto* block = containing_block()) {
  133. block->for_each_fragment([&](auto& fragment) {
  134. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  135. frame().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  136. }
  137. return IterationDecision::Continue;
  138. });
  139. }
  140. }
  141. float LayoutNode::font_size() const
  142. {
  143. // FIXME: This doesn't work right for relative font-sizes
  144. auto length = specified_style().length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
  145. return length.raw_value();
  146. }
  147. Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
  148. {
  149. if (is_box())
  150. return downcast<LayoutBox>(*this).absolute_position();
  151. ASSERT(is_inline());
  152. Gfx::FloatPoint position;
  153. if (auto* block = containing_block()) {
  154. block->for_each_fragment([&](auto& fragment) {
  155. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  156. position = fragment.absolute_rect().location();
  157. return IterationDecision::Break;
  158. }
  159. return IterationDecision::Continue;
  160. });
  161. }
  162. return position;
  163. }
  164. bool LayoutNode::is_floating() const
  165. {
  166. if (!has_style())
  167. return false;
  168. return style().float_() != CSS::Float::None;
  169. }
  170. bool LayoutNode::is_absolutely_positioned() const
  171. {
  172. if (!has_style())
  173. return false;
  174. auto position = style().position();
  175. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  176. }
  177. bool LayoutNode::is_fixed_position() const
  178. {
  179. if (!has_style())
  180. return false;
  181. auto position = style().position();
  182. return position == CSS::Position::Fixed;
  183. }
  184. LayoutNodeWithStyle::LayoutNodeWithStyle(DOM::Document& document, const DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  185. : LayoutNode(document, node)
  186. , m_specified_style(move(specified_style))
  187. {
  188. m_has_style = true;
  189. apply_style(*m_specified_style);
  190. }
  191. void LayoutNodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  192. {
  193. auto& style = static_cast<MutableLayoutStyle&>(m_style);
  194. style.set_position(specified_style.position());
  195. style.set_text_align(specified_style.text_align());
  196. auto white_space = specified_style.white_space();
  197. if (white_space.has_value())
  198. style.set_white_space(white_space.value());
  199. auto float_ = specified_style.float_();
  200. if (float_.has_value())
  201. style.set_float(float_.value());
  202. style.set_z_index(specified_style.z_index());
  203. style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  204. style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  205. style.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  206. style.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  207. style.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  208. style.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  209. style.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom));
  210. style.set_margin(specified_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom));
  211. style.set_padding(specified_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom));
  212. style.border_left().width = specified_style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  213. style.border_top().width = specified_style.length_or_fallback(CSS::PropertyID::BorderTopWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  214. style.border_right().width = specified_style.length_or_fallback(CSS::PropertyID::BorderRightWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  215. style.border_bottom().width = specified_style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  216. style.border_left().color = specified_style.color_or_fallback(CSS::PropertyID::BorderLeftColor, document(), Color::Transparent);
  217. style.border_top().color = specified_style.color_or_fallback(CSS::PropertyID::BorderTopColor, document(), Color::Transparent);
  218. style.border_right().color = specified_style.color_or_fallback(CSS::PropertyID::BorderRightColor, document(), Color::Transparent);
  219. style.border_bottom().color = specified_style.color_or_fallback(CSS::PropertyID::BorderBottomColor, document(), Color::Transparent);
  220. }
  221. }