LayoutNode.cpp 9.5 KB

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