Node.cpp 9.5 KB

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