Node.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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/BlockContainer.h>
  13. #include <LibWeb/Layout/FormattingContext.h>
  14. #include <LibWeb/Layout/InitialContainingBlock.h>
  15. #include <LibWeb/Layout/Node.h>
  16. #include <LibWeb/Layout/TextNode.h>
  17. #include <LibWeb/Page/BrowsingContext.h>
  18. #include <typeinfo>
  19. namespace Web::Layout {
  20. Node::Node(DOM::Document& document, DOM::Node* node)
  21. : m_document(document)
  22. , m_dom_node(node)
  23. {
  24. if (m_dom_node)
  25. m_dom_node->set_layout_node({}, this);
  26. }
  27. Node::~Node()
  28. {
  29. if (m_dom_node && m_dom_node->layout_node() == this)
  30. m_dom_node->set_layout_node({}, nullptr);
  31. }
  32. bool Node::can_contain_boxes_with_position_absolute() const
  33. {
  34. return computed_values().position() != CSS::Position::Static || is<InitialContainingBlock>(*this);
  35. }
  36. const BlockContainer* Node::containing_block() const
  37. {
  38. auto nearest_block_ancestor = [this] {
  39. auto* ancestor = parent();
  40. while (ancestor && !is<BlockContainer>(*ancestor))
  41. ancestor = ancestor->parent();
  42. return static_cast<const BlockContainer*>(ancestor);
  43. };
  44. if (is<TextNode>(*this))
  45. return nearest_block_ancestor();
  46. auto position = computed_values().position();
  47. if (position == CSS::Position::Absolute) {
  48. auto* ancestor = parent();
  49. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  50. ancestor = ancestor->parent();
  51. while (ancestor && (!is<BlockContainer>(*ancestor) || ancestor->is_anonymous()))
  52. ancestor = ancestor->containing_block();
  53. return static_cast<const BlockContainer*>(ancestor);
  54. }
  55. if (position == CSS::Position::Fixed)
  56. return &root();
  57. return nearest_block_ancestor();
  58. }
  59. bool Node::establishes_stacking_context() const
  60. {
  61. if (!has_style())
  62. return false;
  63. if (dom_node() == &document().root())
  64. return true;
  65. auto position = computed_values().position();
  66. if (position == CSS::Position::Absolute || position == CSS::Position::Relative || position == CSS::Position::Fixed || position == CSS::Position::Sticky)
  67. return true;
  68. auto opacity = computed_values().opacity();
  69. if (opacity.has_value() && opacity.value() != 1.0f)
  70. return true;
  71. return false;
  72. }
  73. HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  74. {
  75. HitTestResult result;
  76. for_each_child_in_paint_order([&](auto& child) {
  77. auto child_result = child.hit_test(position, type);
  78. if (child_result.layout_node)
  79. result = child_result;
  80. });
  81. return result;
  82. }
  83. const BrowsingContext& Node::browsing_context() const
  84. {
  85. VERIFY(document().browsing_context());
  86. return *document().browsing_context();
  87. }
  88. BrowsingContext& Node::browsing_context()
  89. {
  90. VERIFY(document().browsing_context());
  91. return *document().browsing_context();
  92. }
  93. const InitialContainingBlock& Node::root() const
  94. {
  95. VERIFY(document().layout_node());
  96. return *document().layout_node();
  97. }
  98. InitialContainingBlock& Node::root()
  99. {
  100. VERIFY(document().layout_node());
  101. return *document().layout_node();
  102. }
  103. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  104. {
  105. for_each_child([&](auto& child) {
  106. child.split_into_lines(context, layout_mode);
  107. });
  108. }
  109. void Node::set_needs_display()
  110. {
  111. if (auto* block = containing_block()) {
  112. block->for_each_fragment([&](auto& fragment) {
  113. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  114. browsing_context().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  115. }
  116. return IterationDecision::Continue;
  117. });
  118. }
  119. }
  120. Gfx::FloatPoint Node::box_type_agnostic_position() const
  121. {
  122. if (is<Box>(*this))
  123. return verify_cast<Box>(*this).absolute_position();
  124. VERIFY(is_inline());
  125. Gfx::FloatPoint position;
  126. if (auto* block = containing_block()) {
  127. block->for_each_fragment([&](auto& fragment) {
  128. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  129. position = fragment.absolute_rect().location();
  130. return IterationDecision::Break;
  131. }
  132. return IterationDecision::Continue;
  133. });
  134. }
  135. return position;
  136. }
  137. bool Node::is_floating() const
  138. {
  139. if (!has_style())
  140. return false;
  141. // flex-items don't float.
  142. if (is_flex_item())
  143. return false;
  144. return computed_values().float_() != CSS::Float::None;
  145. }
  146. bool Node::is_positioned() const
  147. {
  148. return has_style() && computed_values().position() != CSS::Position::Static;
  149. }
  150. bool Node::is_absolutely_positioned() const
  151. {
  152. if (!has_style())
  153. return false;
  154. auto position = computed_values().position();
  155. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  156. }
  157. bool Node::is_fixed_position() const
  158. {
  159. if (!has_style())
  160. return false;
  161. auto position = computed_values().position();
  162. return position == CSS::Position::Fixed;
  163. }
  164. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  165. : Node(document, node)
  166. {
  167. m_has_style = true;
  168. apply_style(*specified_style);
  169. }
  170. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  171. : Node(document, node)
  172. , m_computed_values(move(computed_values))
  173. {
  174. m_has_style = true;
  175. m_font = Gfx::FontDatabase::default_font();
  176. }
  177. void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  178. {
  179. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  180. m_font = specified_style.computed_font();
  181. m_line_height = specified_style.line_height(*this);
  182. {
  183. constexpr int default_font_size = 10;
  184. auto parent_font_size = parent() == nullptr ? default_font_size : parent()->font_size();
  185. auto length = specified_style.length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(default_font_size, CSS::Length::Type::Px));
  186. // FIXME: em sizes return 0 here, for some reason
  187. m_font_size = length.resolved_or_zero(*this, parent_font_size).to_px(*this);
  188. if (m_font_size == 0)
  189. m_font_size = default_font_size;
  190. }
  191. auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
  192. if (bgimage.has_value() && bgimage.value()->is_image()) {
  193. m_background_image = bgimage.value()->as_image();
  194. }
  195. computed_values.set_box_sizing(specified_style.box_sizing());
  196. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  197. auto border_bottom_left_radius = specified_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  198. if (border_bottom_left_radius.has_value())
  199. computed_values.set_border_bottom_left_radius(border_bottom_left_radius.value()->to_length());
  200. auto border_bottom_right_radius = specified_style.property(CSS::PropertyID::BorderBottomRightRadius);
  201. if (border_bottom_right_radius.has_value())
  202. computed_values.set_border_bottom_right_radius(border_bottom_right_radius.value()->to_length());
  203. auto border_top_left_radius = specified_style.property(CSS::PropertyID::BorderTopLeftRadius);
  204. if (border_top_left_radius.has_value())
  205. computed_values.set_border_top_left_radius(border_top_left_radius.value()->to_length());
  206. auto border_top_right_radius = specified_style.property(CSS::PropertyID::BorderTopRightRadius);
  207. if (border_top_right_radius.has_value())
  208. computed_values.set_border_top_right_radius(border_top_right_radius.value()->to_length());
  209. auto background_repeat_x = specified_style.background_repeat_x();
  210. if (background_repeat_x.has_value())
  211. computed_values.set_background_repeat_x(background_repeat_x.value());
  212. auto background_repeat_y = specified_style.background_repeat_y();
  213. if (background_repeat_y.has_value())
  214. computed_values.set_background_repeat_y(background_repeat_y.value());
  215. computed_values.set_display(specified_style.display());
  216. auto flex_direction = specified_style.flex_direction();
  217. if (flex_direction.has_value())
  218. computed_values.set_flex_direction(flex_direction.value());
  219. auto flex_wrap = specified_style.flex_wrap();
  220. if (flex_wrap.has_value())
  221. computed_values.set_flex_wrap(flex_wrap.value());
  222. auto flex_basis = specified_style.flex_basis();
  223. if (flex_basis.has_value())
  224. computed_values.set_flex_basis(flex_basis.value());
  225. computed_values.set_flex_grow(specified_style.flex_grow());
  226. computed_values.set_flex_shrink(specified_style.flex_shrink());
  227. auto justify_content = specified_style.justify_content();
  228. if (justify_content.has_value())
  229. computed_values.set_justify_content(justify_content.value());
  230. auto align_items = specified_style.align_items();
  231. if (align_items.has_value())
  232. computed_values.set_align_items(align_items.value());
  233. auto position = specified_style.position();
  234. if (position.has_value())
  235. computed_values.set_position(position.value());
  236. auto text_align = specified_style.text_align();
  237. if (text_align.has_value())
  238. computed_values.set_text_align(text_align.value());
  239. auto white_space = specified_style.white_space();
  240. if (white_space.has_value())
  241. computed_values.set_white_space(white_space.value());
  242. auto float_ = specified_style.float_();
  243. if (float_.has_value())
  244. computed_values.set_float(float_.value());
  245. auto clear = specified_style.clear();
  246. if (clear.has_value())
  247. computed_values.set_clear(clear.value());
  248. auto overflow_x = specified_style.overflow_x();
  249. if (overflow_x.has_value())
  250. computed_values.set_overflow_x(overflow_x.value());
  251. auto overflow_y = specified_style.overflow_y();
  252. if (overflow_y.has_value())
  253. computed_values.set_overflow_y(overflow_y.value());
  254. auto cursor = specified_style.cursor();
  255. if (cursor.has_value())
  256. computed_values.set_cursor(cursor.value());
  257. auto pointer_events = specified_style.pointer_events();
  258. if (pointer_events.has_value())
  259. computed_values.set_pointer_events(pointer_events.value());
  260. auto text_decoration_line = specified_style.text_decoration_line();
  261. if (text_decoration_line.has_value())
  262. computed_values.set_text_decoration_line(text_decoration_line.value());
  263. auto text_transform = specified_style.text_transform();
  264. if (text_transform.has_value())
  265. computed_values.set_text_transform(text_transform.value());
  266. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  267. computed_values.set_list_style_type(list_style_type.value());
  268. computed_values.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  269. computed_values.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  270. computed_values.set_z_index(specified_style.z_index());
  271. computed_values.set_opacity(specified_style.opacity());
  272. if (computed_values.opacity() == 0)
  273. m_visible = false;
  274. if (auto width = specified_style.property(CSS::PropertyID::Width); width.has_value() && !width.value()->has_auto())
  275. m_has_definite_width = true;
  276. computed_values.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  277. computed_values.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  278. computed_values.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  279. if (auto height = specified_style.property(CSS::PropertyID::Height); height.has_value() && !height.value()->has_auto())
  280. m_has_definite_height = true;
  281. computed_values.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  282. computed_values.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  283. computed_values.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  284. computed_values.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  285. 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)));
  286. 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)));
  287. computed_values.set_box_shadow(specified_style.box_shadow());
  288. computed_values.set_transformations(specified_style.transformations());
  289. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  290. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  291. // we just manually grab the value from `color`. This makes it dependent on `color` being
  292. // specified first, so it's far from ideal.
  293. border.color = specified_style.color_or_fallback(color_property, *this, computed_values.color());
  294. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  295. if (border.line_style == CSS::LineStyle::None)
  296. border.width = 0;
  297. else
  298. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  299. };
  300. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  301. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  302. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  303. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  304. if (auto fill = specified_style.property(CSS::PropertyID::Fill); fill.has_value())
  305. computed_values.set_fill(fill.value()->to_color(*this));
  306. if (auto stroke = specified_style.property(CSS::PropertyID::Stroke); stroke.has_value())
  307. computed_values.set_stroke(stroke.value()->to_color(*this));
  308. if (auto stroke_width = specified_style.property(CSS::PropertyID::StrokeWidth); stroke_width.has_value()) {
  309. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  310. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  311. if (stroke_width.value()->is_numeric())
  312. computed_values.set_stroke_width(CSS::Length::make_px(stroke_width.value()->to_number()));
  313. else
  314. computed_values.set_stroke_width(stroke_width.value()->to_length());
  315. }
  316. }
  317. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  318. {
  319. }
  320. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  321. {
  322. }
  323. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  324. {
  325. }
  326. bool Node::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned, int wheel_delta)
  327. {
  328. if (auto* containing_block = this->containing_block()) {
  329. if (!containing_block->is_scrollable())
  330. return false;
  331. auto new_offset = containing_block->scroll_offset();
  332. new_offset.translate_by(0, wheel_delta);
  333. containing_block->set_scroll_offset(new_offset);
  334. return true;
  335. }
  336. return false;
  337. }
  338. bool Node::is_root_element() const
  339. {
  340. if (is_anonymous())
  341. return false;
  342. return is<HTML::HTMLHtmlElement>(*dom_node());
  343. }
  344. String Node::class_name() const
  345. {
  346. return demangle(typeid(*this).name());
  347. }
  348. bool Node::is_inline_block() const
  349. {
  350. return is_inline() && is<BlockContainer>(*this);
  351. }
  352. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  353. {
  354. auto wrapper = adopt_ref(*new BlockContainer(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  355. wrapper->m_font = m_font;
  356. wrapper->m_font_size = m_font_size;
  357. wrapper->m_line_height = m_line_height;
  358. wrapper->m_background_image = m_background_image;
  359. return wrapper;
  360. }
  361. }