Node.cpp 9.3 KB

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