LayoutNode.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/Layout/LayoutBlock.h>
  30. #include <LibWeb/Layout/LayoutDocument.h>
  31. #include <LibWeb/Layout/LayoutNode.h>
  32. #include <LibWeb/Layout/LayoutReplaced.h>
  33. #include <LibWeb/Page/Frame.h>
  34. namespace Web {
  35. LayoutNode::LayoutNode(DOM::Document& document, 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. before_children_paint(context, phase);
  85. for_each_child([&](auto& child) {
  86. if (child.is_box() && downcast<LayoutBox>(child).stacking_context())
  87. return;
  88. child.paint(context, phase);
  89. });
  90. after_children_paint(context, phase);
  91. }
  92. HitTestResult LayoutNode::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  93. {
  94. HitTestResult result;
  95. for_each_child([&](auto& child) {
  96. // Skip over children that establish their own stacking context.
  97. // The outer loop who called us will take care of those.
  98. if (is<LayoutBox>(child) && downcast<LayoutBox>(child).stacking_context())
  99. return;
  100. auto child_result = child.hit_test(position, type);
  101. if (child_result.layout_node)
  102. result = child_result;
  103. });
  104. return result;
  105. }
  106. const Frame& LayoutNode::frame() const
  107. {
  108. ASSERT(document().frame());
  109. return *document().frame();
  110. }
  111. Frame& LayoutNode::frame()
  112. {
  113. ASSERT(document().frame());
  114. return *document().frame();
  115. }
  116. const LayoutDocument& LayoutNode::root() const
  117. {
  118. ASSERT(document().layout_node());
  119. return *document().layout_node();
  120. }
  121. LayoutDocument& LayoutNode::root()
  122. {
  123. ASSERT(document().layout_node());
  124. return *document().layout_node();
  125. }
  126. void LayoutNode::split_into_lines(LayoutBlock& container, LayoutMode layout_mode)
  127. {
  128. for_each_child([&](auto& child) {
  129. child.split_into_lines(container, layout_mode);
  130. });
  131. }
  132. void LayoutNode::set_needs_display()
  133. {
  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. 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 = specified_style().length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::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 downcast<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_floating() const
  167. {
  168. if (!has_style())
  169. return false;
  170. return style().float_() != CSS::Float::None;
  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(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  187. : LayoutNode(document, node)
  188. , m_specified_style(move(specified_style))
  189. {
  190. m_has_style = true;
  191. apply_style(*m_specified_style);
  192. }
  193. void LayoutNodeWithStyle::apply_style(const CSS::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. auto float_ = specified_style.float_();
  202. if (float_.has_value())
  203. style.set_float(float_.value());
  204. style.set_z_index(specified_style.z_index());
  205. style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  206. style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  207. style.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  208. style.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  209. style.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  210. style.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  211. style.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom));
  212. style.set_margin(specified_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom));
  213. style.set_padding(specified_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom));
  214. style.border_left().width = specified_style.length_or_fallback(CSS::PropertyID::BorderLeftWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  215. style.border_top().width = specified_style.length_or_fallback(CSS::PropertyID::BorderTopWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  216. style.border_right().width = specified_style.length_or_fallback(CSS::PropertyID::BorderRightWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  217. style.border_bottom().width = specified_style.length_or_fallback(CSS::PropertyID::BorderBottomWidth, {}).resolved_or_zero(*this, 0).to_px(*this);
  218. style.border_left().color = specified_style.color_or_fallback(CSS::PropertyID::BorderLeftColor, document(), Color::Transparent);
  219. style.border_top().color = specified_style.color_or_fallback(CSS::PropertyID::BorderTopColor, document(), Color::Transparent);
  220. style.border_right().color = specified_style.color_or_fallback(CSS::PropertyID::BorderRightColor, document(), Color::Transparent);
  221. style.border_bottom().color = specified_style.color_or_fallback(CSS::PropertyID::BorderBottomColor, document(), Color::Transparent);
  222. }
  223. void LayoutNode::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  224. {
  225. }
  226. void LayoutNode::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  227. {
  228. }
  229. void LayoutNode::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  230. {
  231. }
  232. }