Node.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/FormattingContext.h>
  33. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  34. #include <LibWeb/Layout/Node.h>
  35. #include <LibWeb/Layout/ReplacedBox.h>
  36. #include <LibWeb/Page/Frame.h>
  37. namespace Web::Layout {
  38. Node::Node(DOM::Document& document, DOM::Node* node)
  39. : m_document(document)
  40. , m_dom_node(node)
  41. {
  42. if (m_dom_node)
  43. m_dom_node->set_layout_node({}, this);
  44. }
  45. Node::~Node()
  46. {
  47. if (m_dom_node && m_dom_node->layout_node() == this)
  48. m_dom_node->set_layout_node({}, nullptr);
  49. }
  50. bool Node::can_contain_boxes_with_position_absolute() const
  51. {
  52. return style().position() != CSS::Position::Static || is_initial_containing_block();
  53. }
  54. const BlockBox* Node::containing_block() const
  55. {
  56. auto nearest_block_ancestor = [this] {
  57. auto* ancestor = parent();
  58. while (ancestor && !is<BlockBox>(*ancestor))
  59. ancestor = ancestor->parent();
  60. return downcast<BlockBox>(ancestor);
  61. };
  62. if (is_text())
  63. return nearest_block_ancestor();
  64. auto position = style().position();
  65. if (position == CSS::Position::Absolute) {
  66. auto* ancestor = parent();
  67. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  68. ancestor = ancestor->parent();
  69. while (ancestor && (!is<BlockBox>(ancestor) || ancestor->is_anonymous()))
  70. ancestor = ancestor->containing_block();
  71. return downcast<BlockBox>(ancestor);
  72. }
  73. if (position == CSS::Position::Fixed)
  74. return &root();
  75. return nearest_block_ancestor();
  76. }
  77. void Node::paint(PaintContext& context, PaintPhase phase)
  78. {
  79. if (!is_visible())
  80. return;
  81. before_children_paint(context, phase);
  82. for_each_child_in_paint_order([&](auto& child) {
  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_in_paint_order([&](auto& child) {
  91. auto child_result = child.hit_test(position, type);
  92. if (child_result.layout_node)
  93. result = child_result;
  94. });
  95. return result;
  96. }
  97. const Frame& Node::frame() const
  98. {
  99. ASSERT(document().frame());
  100. return *document().frame();
  101. }
  102. Frame& Node::frame()
  103. {
  104. ASSERT(document().frame());
  105. return *document().frame();
  106. }
  107. const InitialContainingBlockBox& Node::root() const
  108. {
  109. ASSERT(document().layout_node());
  110. return *document().layout_node();
  111. }
  112. InitialContainingBlockBox& Node::root()
  113. {
  114. ASSERT(document().layout_node());
  115. return *document().layout_node();
  116. }
  117. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  118. {
  119. for_each_child([&](auto& child) {
  120. child.split_into_lines(context, layout_mode);
  121. });
  122. }
  123. void Node::set_needs_display()
  124. {
  125. if (auto* block = containing_block()) {
  126. block->for_each_fragment([&](auto& fragment) {
  127. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  128. frame().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  129. }
  130. return IterationDecision::Continue;
  131. });
  132. }
  133. }
  134. float Node::font_size() const
  135. {
  136. // FIXME: This doesn't work right for relative font-sizes
  137. auto length = specified_style().length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
  138. return length.raw_value();
  139. }
  140. Gfx::FloatPoint Node::box_type_agnostic_position() const
  141. {
  142. if (is_box())
  143. return downcast<Box>(*this).absolute_position();
  144. ASSERT(is_inline());
  145. Gfx::FloatPoint position;
  146. if (auto* block = containing_block()) {
  147. block->for_each_fragment([&](auto& fragment) {
  148. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  149. position = fragment.absolute_rect().location();
  150. return IterationDecision::Break;
  151. }
  152. return IterationDecision::Continue;
  153. });
  154. }
  155. return position;
  156. }
  157. bool Node::is_floating() const
  158. {
  159. if (!has_style())
  160. return false;
  161. return style().float_() != CSS::Float::None;
  162. }
  163. bool Node::is_positioned() const
  164. {
  165. return has_style() && style().position() != CSS::Position::Static;
  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. auto position = specified_style.position();
  192. if (position.has_value())
  193. style.set_position(position.value());
  194. auto text_align = specified_style.text_align();
  195. if (text_align.has_value())
  196. style.set_text_align(text_align.value());
  197. auto white_space = specified_style.white_space();
  198. if (white_space.has_value())
  199. style.set_white_space(white_space.value());
  200. auto float_ = specified_style.float_();
  201. if (float_.has_value())
  202. style.set_float(float_.value());
  203. auto clear = specified_style.clear();
  204. if (clear.has_value())
  205. style.set_clear(clear.value());
  206. auto text_decoration_line = specified_style.text_decoration_line();
  207. if (text_decoration_line.has_value())
  208. style.set_text_decoration_line(text_decoration_line.value());
  209. auto text_transform = specified_style.text_transform();
  210. if (text_transform.has_value())
  211. style.set_text_transform(text_transform.value());
  212. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  213. style.set_list_style_type(list_style_type.value());
  214. style.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, document(), Color::Transparent));
  215. style.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document(), Color::Transparent));
  216. style.set_z_index(specified_style.z_index());
  217. style.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  218. style.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  219. style.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  220. style.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  221. style.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  222. style.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  223. style.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  224. style.set_margin(specified_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom, CSS::Length::make_px(0)));
  225. style.set_padding(specified_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom, CSS::Length::make_px(0)));
  226. auto do_border_style = [&](BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  227. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  228. border.color = specified_style.color_or_fallback(color_property, document(), Color::Transparent);
  229. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  230. };
  231. do_border_style(style.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  232. do_border_style(style.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  233. do_border_style(style.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  234. do_border_style(style.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  235. }
  236. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  237. {
  238. }
  239. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  240. {
  241. }
  242. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  243. {
  244. }
  245. bool Node::is_root_element() const
  246. {
  247. if (is_anonymous())
  248. return false;
  249. return is<HTML::HTMLHtmlElement>(*dom_node());
  250. }
  251. }