Node.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Demangle.h>
  7. #include <LibGfx/FontDatabase.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/Dump.h>
  11. #include <LibWeb/HTML/HTMLHtmlElement.h>
  12. #include <LibWeb/Layout/BlockBox.h>
  13. #include <LibWeb/Layout/FormattingContext.h>
  14. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  15. #include <LibWeb/Layout/Node.h>
  16. #include <LibWeb/Layout/TextNode.h>
  17. #include <LibWeb/Page/Frame.h>
  18. namespace Web::Layout {
  19. Node::Node(DOM::Document& document, DOM::Node* node)
  20. : m_document(document)
  21. , m_dom_node(node)
  22. {
  23. if (m_dom_node)
  24. m_dom_node->set_layout_node({}, this);
  25. }
  26. Node::~Node()
  27. {
  28. if (m_dom_node && m_dom_node->layout_node() == this)
  29. m_dom_node->set_layout_node({}, nullptr);
  30. }
  31. bool Node::can_contain_boxes_with_position_absolute() const
  32. {
  33. return computed_values().position() != CSS::Position::Static || is<InitialContainingBlockBox>(*this);
  34. }
  35. const BlockBox* Node::containing_block() const
  36. {
  37. auto nearest_block_ancestor = [this] {
  38. auto* ancestor = parent();
  39. while (ancestor && !is<BlockBox>(*ancestor))
  40. ancestor = ancestor->parent();
  41. return static_cast<const BlockBox*>(ancestor);
  42. };
  43. if (is<TextNode>(*this))
  44. return nearest_block_ancestor();
  45. auto position = computed_values().position();
  46. if (position == CSS::Position::Absolute) {
  47. auto* ancestor = parent();
  48. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  49. ancestor = ancestor->parent();
  50. while (ancestor && (!is<BlockBox>(*ancestor) || ancestor->is_anonymous()))
  51. ancestor = ancestor->containing_block();
  52. return static_cast<const BlockBox*>(ancestor);
  53. }
  54. if (position == CSS::Position::Fixed)
  55. return &root();
  56. return nearest_block_ancestor();
  57. }
  58. void Node::paint(PaintContext& context, PaintPhase phase)
  59. {
  60. if (!is_visible())
  61. return;
  62. before_children_paint(context, phase);
  63. for_each_child_in_paint_order([&](auto& child) {
  64. child.paint(context, phase);
  65. });
  66. after_children_paint(context, phase);
  67. }
  68. HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  69. {
  70. HitTestResult result;
  71. for_each_child_in_paint_order([&](auto& child) {
  72. auto child_result = child.hit_test(position, type);
  73. if (child_result.layout_node)
  74. result = child_result;
  75. });
  76. return result;
  77. }
  78. const Frame& Node::frame() const
  79. {
  80. VERIFY(document().frame());
  81. return *document().frame();
  82. }
  83. Frame& Node::frame()
  84. {
  85. VERIFY(document().frame());
  86. return *document().frame();
  87. }
  88. const InitialContainingBlockBox& Node::root() const
  89. {
  90. VERIFY(document().layout_node());
  91. return *document().layout_node();
  92. }
  93. InitialContainingBlockBox& Node::root()
  94. {
  95. VERIFY(document().layout_node());
  96. return *document().layout_node();
  97. }
  98. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  99. {
  100. for_each_child([&](auto& child) {
  101. child.split_into_lines(context, layout_mode);
  102. });
  103. }
  104. void Node::set_needs_display()
  105. {
  106. if (auto* block = containing_block()) {
  107. block->for_each_fragment([&](auto& fragment) {
  108. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  109. frame().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  110. }
  111. return IterationDecision::Continue;
  112. });
  113. }
  114. }
  115. Gfx::FloatPoint Node::box_type_agnostic_position() const
  116. {
  117. if (is<Box>(*this))
  118. return downcast<Box>(*this).absolute_position();
  119. VERIFY(is_inline());
  120. Gfx::FloatPoint position;
  121. if (auto* block = containing_block()) {
  122. block->for_each_fragment([&](auto& fragment) {
  123. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  124. position = fragment.absolute_rect().location();
  125. return IterationDecision::Break;
  126. }
  127. return IterationDecision::Continue;
  128. });
  129. }
  130. return position;
  131. }
  132. bool Node::is_floating() const
  133. {
  134. if (!has_style())
  135. return false;
  136. return computed_values().float_() != CSS::Float::None;
  137. }
  138. bool Node::is_positioned() const
  139. {
  140. return has_style() && computed_values().position() != CSS::Position::Static;
  141. }
  142. bool Node::is_absolutely_positioned() const
  143. {
  144. if (!has_style())
  145. return false;
  146. auto position = computed_values().position();
  147. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  148. }
  149. bool Node::is_fixed_position() const
  150. {
  151. if (!has_style())
  152. return false;
  153. auto position = computed_values().position();
  154. return position == CSS::Position::Fixed;
  155. }
  156. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  157. : Node(document, node)
  158. {
  159. m_has_style = true;
  160. apply_style(*specified_style);
  161. }
  162. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  163. : Node(document, node)
  164. , m_computed_values(move(computed_values))
  165. {
  166. m_has_style = true;
  167. m_font = Gfx::FontDatabase::default_font();
  168. }
  169. void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  170. {
  171. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  172. m_font = specified_style.font();
  173. m_line_height = specified_style.line_height(*this);
  174. {
  175. // FIXME: This doesn't work right for relative font-sizes
  176. auto length = specified_style.length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
  177. m_font_size = length.raw_value();
  178. }
  179. auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
  180. if (bgimage.has_value() && bgimage.value()->is_image()) {
  181. m_background_image = static_ptr_cast<CSS::ImageStyleValue>(bgimage.value());
  182. }
  183. auto background_repeat_x = specified_style.background_repeat_x();
  184. if (background_repeat_x.has_value())
  185. computed_values.set_background_repeat_x(background_repeat_x.value());
  186. auto background_repeat_y = specified_style.background_repeat_y();
  187. if (background_repeat_y.has_value())
  188. computed_values.set_background_repeat_y(background_repeat_y.value());
  189. computed_values.set_display(specified_style.display());
  190. auto flex_direction = specified_style.flex_direction();
  191. if (flex_direction.has_value())
  192. computed_values.set_flex_direction(flex_direction.value());
  193. auto position = specified_style.position();
  194. if (position.has_value())
  195. computed_values.set_position(position.value());
  196. auto text_align = specified_style.text_align();
  197. if (text_align.has_value())
  198. computed_values.set_text_align(text_align.value());
  199. auto white_space = specified_style.white_space();
  200. if (white_space.has_value())
  201. computed_values.set_white_space(white_space.value());
  202. auto float_ = specified_style.float_();
  203. if (float_.has_value())
  204. computed_values.set_float(float_.value());
  205. auto clear = specified_style.clear();
  206. if (clear.has_value())
  207. computed_values.set_clear(clear.value());
  208. auto overflow_x = specified_style.overflow_x();
  209. if (overflow_x.has_value())
  210. computed_values.set_overflow_x(overflow_x.value());
  211. auto overflow_y = specified_style.overflow_y();
  212. if (overflow_y.has_value())
  213. computed_values.set_overflow_y(overflow_y.value());
  214. auto cursor = specified_style.cursor();
  215. if (cursor.has_value())
  216. computed_values.set_cursor(cursor.value());
  217. auto text_decoration_line = specified_style.text_decoration_line();
  218. if (text_decoration_line.has_value())
  219. computed_values.set_text_decoration_line(text_decoration_line.value());
  220. auto text_transform = specified_style.text_transform();
  221. if (text_transform.has_value())
  222. computed_values.set_text_transform(text_transform.value());
  223. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  224. computed_values.set_list_style_type(list_style_type.value());
  225. computed_values.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, document(), Color::Black));
  226. computed_values.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document(), Color::Transparent));
  227. computed_values.set_z_index(specified_style.z_index());
  228. computed_values.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  229. computed_values.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  230. computed_values.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  231. computed_values.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  232. computed_values.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  233. computed_values.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  234. computed_values.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  235. computed_values.set_margin(specified_style.length_box(CSS::PropertyID::MarginLeft, CSS::PropertyID::MarginTop, CSS::PropertyID::MarginRight, CSS::PropertyID::MarginBottom, CSS::Length::make_px(0)));
  236. computed_values.set_padding(specified_style.length_box(CSS::PropertyID::PaddingLeft, CSS::PropertyID::PaddingTop, CSS::PropertyID::PaddingRight, CSS::PropertyID::PaddingBottom, CSS::Length::make_px(0)));
  237. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  238. border.color = specified_style.color_or_fallback(color_property, document(), Color::Transparent);
  239. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  240. if (border.line_style == CSS::LineStyle::None)
  241. border.width = 0;
  242. else
  243. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  244. };
  245. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  246. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  247. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  248. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  249. }
  250. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  251. {
  252. }
  253. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  254. {
  255. }
  256. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  257. {
  258. }
  259. bool Node::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned, int wheel_delta)
  260. {
  261. if (auto* containing_block = this->containing_block()) {
  262. if (!containing_block->is_scrollable())
  263. return false;
  264. auto new_offset = containing_block->scroll_offset();
  265. new_offset.translate_by(0, wheel_delta);
  266. containing_block->set_scroll_offset(new_offset);
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool Node::is_root_element() const
  272. {
  273. if (is_anonymous())
  274. return false;
  275. return is<HTML::HTMLHtmlElement>(*dom_node());
  276. }
  277. String Node::class_name() const
  278. {
  279. return demangle(typeid(*this).name());
  280. }
  281. bool Node::is_inline_block() const
  282. {
  283. return is_inline() && is<BlockBox>(*this);
  284. }
  285. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  286. {
  287. auto wrapper = adopt_ref(*new BlockBox(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  288. wrapper->m_font = m_font;
  289. wrapper->m_font_size = m_font_size;
  290. wrapper->m_line_height = m_line_height;
  291. wrapper->m_background_image = m_background_image;
  292. return wrapper;
  293. }
  294. }