Node.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/BrowsingContext.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. bool Node::establishes_stacking_context() const
  59. {
  60. if (!has_style())
  61. return false;
  62. if (dom_node() == document().root())
  63. return true;
  64. auto position = computed_values().position();
  65. if (position == CSS::Position::Absolute || position == CSS::Position::Relative || position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  66. return true;
  67. auto opacity = computed_values().opacity();
  68. if (opacity.has_value() && opacity.value() != 1.0f)
  69. return true;
  70. return false;
  71. }
  72. void Node::paint(PaintContext& context, PaintPhase phase)
  73. {
  74. if (!is_visible())
  75. return;
  76. before_children_paint(context, phase);
  77. for_each_child_in_paint_order([&](auto& child) {
  78. child.paint(context, phase);
  79. });
  80. after_children_paint(context, phase);
  81. }
  82. HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  83. {
  84. HitTestResult result;
  85. for_each_child_in_paint_order([&](auto& child) {
  86. auto child_result = child.hit_test(position, type);
  87. if (child_result.layout_node)
  88. result = child_result;
  89. });
  90. return result;
  91. }
  92. const BrowsingContext& Node::browsing_context() const
  93. {
  94. VERIFY(document().browsing_context());
  95. return *document().browsing_context();
  96. }
  97. BrowsingContext& Node::browsing_context()
  98. {
  99. VERIFY(document().browsing_context());
  100. return *document().browsing_context();
  101. }
  102. const InitialContainingBlockBox& Node::root() const
  103. {
  104. VERIFY(document().layout_node());
  105. return *document().layout_node();
  106. }
  107. InitialContainingBlockBox& Node::root()
  108. {
  109. VERIFY(document().layout_node());
  110. return *document().layout_node();
  111. }
  112. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  113. {
  114. for_each_child([&](auto& child) {
  115. child.split_into_lines(context, layout_mode);
  116. });
  117. }
  118. void Node::set_needs_display()
  119. {
  120. if (auto* block = containing_block()) {
  121. block->for_each_fragment([&](auto& fragment) {
  122. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  123. browsing_context().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  124. }
  125. return IterationDecision::Continue;
  126. });
  127. }
  128. }
  129. Gfx::FloatPoint Node::box_type_agnostic_position() const
  130. {
  131. if (is<Box>(*this))
  132. return verify_cast<Box>(*this).absolute_position();
  133. VERIFY(is_inline());
  134. Gfx::FloatPoint position;
  135. if (auto* block = containing_block()) {
  136. block->for_each_fragment([&](auto& fragment) {
  137. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  138. position = fragment.absolute_rect().location();
  139. return IterationDecision::Break;
  140. }
  141. return IterationDecision::Continue;
  142. });
  143. }
  144. return position;
  145. }
  146. bool Node::is_floating() const
  147. {
  148. if (!has_style())
  149. return false;
  150. // flex-items don't float.
  151. if (is_flex_item())
  152. return false;
  153. return computed_values().float_() != CSS::Float::None;
  154. }
  155. bool Node::is_positioned() const
  156. {
  157. return has_style() && computed_values().position() != CSS::Position::Static;
  158. }
  159. bool Node::is_absolutely_positioned() const
  160. {
  161. if (!has_style())
  162. return false;
  163. auto position = computed_values().position();
  164. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  165. }
  166. bool Node::is_fixed_position() const
  167. {
  168. if (!has_style())
  169. return false;
  170. auto position = computed_values().position();
  171. return position == CSS::Position::Fixed;
  172. }
  173. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  174. : Node(document, node)
  175. {
  176. m_has_style = true;
  177. apply_style(*specified_style);
  178. }
  179. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  180. : Node(document, node)
  181. , m_computed_values(move(computed_values))
  182. {
  183. m_has_style = true;
  184. m_font = Gfx::FontDatabase::default_font();
  185. }
  186. void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  187. {
  188. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  189. m_font = specified_style.font();
  190. m_line_height = specified_style.line_height(*this);
  191. {
  192. // FIXME: This doesn't work right for relative font-sizes
  193. auto length = specified_style.length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
  194. m_font_size = length.raw_value();
  195. }
  196. auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
  197. if (bgimage.has_value() && bgimage.value()->is_image()) {
  198. m_background_image = static_ptr_cast<CSS::ImageStyleValue>(bgimage.value());
  199. }
  200. auto border_bottom_left_radius = specified_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  201. if (border_bottom_left_radius.has_value())
  202. computed_values.set_border_bottom_left_radius(border_bottom_left_radius.value()->to_length());
  203. auto border_bottom_right_radius = specified_style.property(CSS::PropertyID::BorderBottomRightRadius);
  204. if (border_bottom_right_radius.has_value())
  205. computed_values.set_border_bottom_right_radius(border_bottom_right_radius.value()->to_length());
  206. auto border_top_left_radius = specified_style.property(CSS::PropertyID::BorderTopLeftRadius);
  207. if (border_top_left_radius.has_value())
  208. computed_values.set_border_top_left_radius(border_top_left_radius.value()->to_length());
  209. auto border_top_right_radius = specified_style.property(CSS::PropertyID::BorderTopRightRadius);
  210. if (border_top_right_radius.has_value())
  211. computed_values.set_border_top_right_radius(border_top_right_radius.value()->to_length());
  212. auto background_repeat_x = specified_style.background_repeat_x();
  213. if (background_repeat_x.has_value())
  214. computed_values.set_background_repeat_x(background_repeat_x.value());
  215. auto background_repeat_y = specified_style.background_repeat_y();
  216. if (background_repeat_y.has_value())
  217. computed_values.set_background_repeat_y(background_repeat_y.value());
  218. computed_values.set_display(specified_style.display());
  219. auto flex_direction = specified_style.flex_direction();
  220. if (flex_direction.has_value())
  221. computed_values.set_flex_direction(flex_direction.value());
  222. auto flex_wrap = specified_style.flex_wrap();
  223. if (flex_wrap.has_value())
  224. computed_values.set_flex_wrap(flex_wrap.value());
  225. auto flex_basis = specified_style.flex_basis();
  226. if (flex_basis.has_value())
  227. computed_values.set_flex_basis(flex_basis.value());
  228. computed_values.set_flex_grow_factor(specified_style.flex_grow_factor());
  229. computed_values.set_flex_shrink_factor(specified_style.flex_shrink_factor());
  230. auto justify_content = specified_style.justify_content();
  231. if (justify_content.has_value())
  232. computed_values.set_justify_content(justify_content.value());
  233. auto position = specified_style.position();
  234. if (position.has_value()) {
  235. computed_values.set_position(position.value());
  236. if (position.value() == CSS::Position::Absolute) {
  237. m_has_definite_width = true;
  238. m_has_definite_height = true;
  239. }
  240. }
  241. auto text_align = specified_style.text_align();
  242. if (text_align.has_value())
  243. computed_values.set_text_align(text_align.value());
  244. auto white_space = specified_style.white_space();
  245. if (white_space.has_value())
  246. computed_values.set_white_space(white_space.value());
  247. auto float_ = specified_style.float_();
  248. if (float_.has_value())
  249. computed_values.set_float(float_.value());
  250. auto clear = specified_style.clear();
  251. if (clear.has_value())
  252. computed_values.set_clear(clear.value());
  253. auto overflow_x = specified_style.overflow_x();
  254. if (overflow_x.has_value())
  255. computed_values.set_overflow_x(overflow_x.value());
  256. auto overflow_y = specified_style.overflow_y();
  257. if (overflow_y.has_value())
  258. computed_values.set_overflow_y(overflow_y.value());
  259. auto cursor = specified_style.cursor();
  260. if (cursor.has_value())
  261. computed_values.set_cursor(cursor.value());
  262. auto text_decoration_line = specified_style.text_decoration_line();
  263. if (text_decoration_line.has_value())
  264. computed_values.set_text_decoration_line(text_decoration_line.value());
  265. auto text_transform = specified_style.text_transform();
  266. if (text_transform.has_value())
  267. computed_values.set_text_transform(text_transform.value());
  268. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  269. computed_values.set_list_style_type(list_style_type.value());
  270. computed_values.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, document(), Color::Black));
  271. computed_values.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document(), Color::Transparent));
  272. computed_values.set_z_index(specified_style.z_index());
  273. computed_values.set_opacity(specified_style.opacity());
  274. if (computed_values.opacity() == 0)
  275. m_visible = false;
  276. if (auto width = specified_style.property(CSS::PropertyID::Width); width.has_value())
  277. m_has_definite_width = true;
  278. computed_values.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  279. computed_values.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  280. computed_values.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  281. if (auto height = specified_style.property(CSS::PropertyID::Height); height.has_value())
  282. m_has_definite_height = true;
  283. computed_values.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  284. computed_values.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  285. computed_values.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  286. computed_values.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  287. 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)));
  288. 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)));
  289. computed_values.set_box_shadow(specified_style.box_shadow());
  290. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  291. border.color = specified_style.color_or_fallback(color_property, document(), Color::Transparent);
  292. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  293. if (border.line_style == CSS::LineStyle::None)
  294. border.width = 0;
  295. else
  296. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  297. };
  298. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  299. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  300. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  301. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  302. }
  303. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  304. {
  305. }
  306. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  307. {
  308. }
  309. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  310. {
  311. }
  312. bool Node::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned, int wheel_delta)
  313. {
  314. if (auto* containing_block = this->containing_block()) {
  315. if (!containing_block->is_scrollable())
  316. return false;
  317. auto new_offset = containing_block->scroll_offset();
  318. new_offset.translate_by(0, wheel_delta);
  319. containing_block->set_scroll_offset(new_offset);
  320. return true;
  321. }
  322. return false;
  323. }
  324. bool Node::is_root_element() const
  325. {
  326. if (is_anonymous())
  327. return false;
  328. return is<HTML::HTMLHtmlElement>(*dom_node());
  329. }
  330. String Node::class_name() const
  331. {
  332. return demangle(typeid(*this).name());
  333. }
  334. bool Node::is_inline_block() const
  335. {
  336. return is_inline() && is<BlockBox>(*this);
  337. }
  338. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  339. {
  340. auto wrapper = adopt_ref(*new BlockBox(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  341. wrapper->m_font = m_font;
  342. wrapper->m_font_size = m_font_size;
  343. wrapper->m_line_height = m_line_height;
  344. wrapper->m_background_image = m_background_image;
  345. return wrapper;
  346. }
  347. }