Node.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. return computed_values().opacity() < 1.0f;
  69. }
  70. HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  71. {
  72. HitTestResult result;
  73. for_each_child_in_paint_order([&](auto& child) {
  74. auto child_result = child.hit_test(position, type);
  75. if (child_result.layout_node)
  76. result = child_result;
  77. });
  78. return result;
  79. }
  80. const BrowsingContext& Node::browsing_context() const
  81. {
  82. VERIFY(document().browsing_context());
  83. return *document().browsing_context();
  84. }
  85. BrowsingContext& Node::browsing_context()
  86. {
  87. VERIFY(document().browsing_context());
  88. return *document().browsing_context();
  89. }
  90. const InitialContainingBlock& Node::root() const
  91. {
  92. VERIFY(document().layout_node());
  93. return *document().layout_node();
  94. }
  95. InitialContainingBlock& Node::root()
  96. {
  97. VERIFY(document().layout_node());
  98. return *document().layout_node();
  99. }
  100. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  101. {
  102. for_each_child([&](auto& child) {
  103. child.split_into_lines(context, layout_mode);
  104. });
  105. }
  106. void Node::set_needs_display()
  107. {
  108. if (auto* block = containing_block()) {
  109. block->for_each_fragment([&](auto& fragment) {
  110. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  111. browsing_context().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  112. }
  113. return IterationDecision::Continue;
  114. });
  115. }
  116. }
  117. Gfx::FloatPoint Node::box_type_agnostic_position() const
  118. {
  119. if (is<Box>(*this))
  120. return verify_cast<Box>(*this).absolute_position();
  121. VERIFY(is_inline());
  122. Gfx::FloatPoint position;
  123. if (auto* block = containing_block()) {
  124. block->for_each_fragment([&](auto& fragment) {
  125. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  126. position = fragment.absolute_rect().location();
  127. return IterationDecision::Break;
  128. }
  129. return IterationDecision::Continue;
  130. });
  131. }
  132. return position;
  133. }
  134. bool Node::is_floating() const
  135. {
  136. if (!has_style())
  137. return false;
  138. // flex-items don't float.
  139. if (is_flex_item())
  140. return false;
  141. return computed_values().float_() != CSS::Float::None;
  142. }
  143. bool Node::is_positioned() const
  144. {
  145. return has_style() && computed_values().position() != CSS::Position::Static;
  146. }
  147. bool Node::is_absolutely_positioned() const
  148. {
  149. if (!has_style())
  150. return false;
  151. auto position = computed_values().position();
  152. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  153. }
  154. bool Node::is_fixed_position() const
  155. {
  156. if (!has_style())
  157. return false;
  158. auto position = computed_values().position();
  159. return position == CSS::Position::Fixed;
  160. }
  161. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  162. : Node(document, node)
  163. {
  164. m_has_style = true;
  165. apply_style(*specified_style);
  166. }
  167. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  168. : Node(document, node)
  169. , m_computed_values(move(computed_values))
  170. {
  171. m_has_style = true;
  172. m_font = Gfx::FontDatabase::default_font();
  173. }
  174. void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  175. {
  176. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  177. m_font = specified_style.computed_font();
  178. m_line_height = specified_style.line_height(*this);
  179. {
  180. constexpr int default_font_size = 10;
  181. auto parent_font_size = parent() == nullptr ? default_font_size : parent()->font_size();
  182. auto length = specified_style.length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(default_font_size, CSS::Length::Type::Px));
  183. // FIXME: em sizes return 0 here, for some reason
  184. m_font_size = length.resolved_or_zero(*this, parent_font_size).to_px(*this);
  185. if (m_font_size == 0)
  186. m_font_size = default_font_size;
  187. }
  188. {
  189. auto attachments = specified_style.property(CSS::PropertyID::BackgroundAttachment);
  190. auto clips = specified_style.property(CSS::PropertyID::BackgroundClip);
  191. auto images = specified_style.property(CSS::PropertyID::BackgroundImage);
  192. auto origins = specified_style.property(CSS::PropertyID::BackgroundOrigin);
  193. auto positions = specified_style.property(CSS::PropertyID::BackgroundPosition);
  194. auto repeats = specified_style.property(CSS::PropertyID::BackgroundRepeat);
  195. auto sizes = specified_style.property(CSS::PropertyID::BackgroundSize);
  196. auto count_layers = [](auto maybe_style_value) -> size_t {
  197. if (maybe_style_value.has_value() && maybe_style_value.value()->is_value_list())
  198. return maybe_style_value.value()->as_value_list().size();
  199. else
  200. return 1;
  201. };
  202. auto value_for_layer = [](auto maybe_style_value, size_t layer_index) -> RefPtr<CSS::StyleValue> {
  203. if (!maybe_style_value.has_value())
  204. return nullptr;
  205. auto& style_value = maybe_style_value.value();
  206. if (style_value->is_value_list())
  207. return style_value->as_value_list().value_at(layer_index, true);
  208. return style_value;
  209. };
  210. size_t layer_count = 1;
  211. layer_count = max(layer_count, count_layers(attachments));
  212. layer_count = max(layer_count, count_layers(clips));
  213. layer_count = max(layer_count, count_layers(images));
  214. layer_count = max(layer_count, count_layers(origins));
  215. layer_count = max(layer_count, count_layers(positions));
  216. layer_count = max(layer_count, count_layers(repeats));
  217. layer_count = max(layer_count, count_layers(sizes));
  218. Vector<CSS::BackgroundLayerData> layers;
  219. layers.ensure_capacity(layer_count);
  220. for (size_t layer_index = 0; layer_index < layer_count; layer_index++) {
  221. CSS::BackgroundLayerData layer;
  222. // TODO: Other properties
  223. if (auto image_value = value_for_layer(images, layer_index); image_value && image_value->is_image()) {
  224. layer.image = image_value->as_image();
  225. layer.image->load_bitmap(document());
  226. }
  227. if (auto repeat_value = value_for_layer(repeats, layer_index); repeat_value && repeat_value->is_background_repeat()) {
  228. layer.repeat_x = repeat_value->as_background_repeat().repeat_x();
  229. layer.repeat_y = repeat_value->as_background_repeat().repeat_y();
  230. }
  231. layers.append(move(layer));
  232. }
  233. computed_values.set_background_layers(move(layers));
  234. }
  235. computed_values.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, *this, CSS::InitialValues::background_color()));
  236. // FIXME: Remove this
  237. auto bgimage = specified_style.property(CSS::PropertyID::BackgroundImage);
  238. if (bgimage.has_value() && bgimage.value()->is_image()) {
  239. m_background_image = bgimage.value()->as_image();
  240. m_background_image->load_bitmap(document());
  241. }
  242. computed_values.set_box_sizing(specified_style.box_sizing());
  243. // FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
  244. auto border_bottom_left_radius = specified_style.property(CSS::PropertyID::BorderBottomLeftRadius);
  245. if (border_bottom_left_radius.has_value())
  246. computed_values.set_border_bottom_left_radius(border_bottom_left_radius.value()->to_length());
  247. auto border_bottom_right_radius = specified_style.property(CSS::PropertyID::BorderBottomRightRadius);
  248. if (border_bottom_right_radius.has_value())
  249. computed_values.set_border_bottom_right_radius(border_bottom_right_radius.value()->to_length());
  250. auto border_top_left_radius = specified_style.property(CSS::PropertyID::BorderTopLeftRadius);
  251. if (border_top_left_radius.has_value())
  252. computed_values.set_border_top_left_radius(border_top_left_radius.value()->to_length());
  253. auto border_top_right_radius = specified_style.property(CSS::PropertyID::BorderTopRightRadius);
  254. if (border_top_right_radius.has_value())
  255. computed_values.set_border_top_right_radius(border_top_right_radius.value()->to_length());
  256. auto background_repeat = specified_style.background_repeat();
  257. if (background_repeat.has_value())
  258. computed_values.set_background_repeat(background_repeat.value());
  259. computed_values.set_display(specified_style.display());
  260. auto flex_direction = specified_style.flex_direction();
  261. if (flex_direction.has_value())
  262. computed_values.set_flex_direction(flex_direction.value());
  263. auto flex_wrap = specified_style.flex_wrap();
  264. if (flex_wrap.has_value())
  265. computed_values.set_flex_wrap(flex_wrap.value());
  266. auto flex_basis = specified_style.flex_basis();
  267. if (flex_basis.has_value())
  268. computed_values.set_flex_basis(flex_basis.value());
  269. computed_values.set_flex_grow(specified_style.flex_grow());
  270. computed_values.set_flex_shrink(specified_style.flex_shrink());
  271. auto justify_content = specified_style.justify_content();
  272. if (justify_content.has_value())
  273. computed_values.set_justify_content(justify_content.value());
  274. auto align_items = specified_style.align_items();
  275. if (align_items.has_value())
  276. computed_values.set_align_items(align_items.value());
  277. auto position = specified_style.position();
  278. if (position.has_value())
  279. computed_values.set_position(position.value());
  280. auto text_align = specified_style.text_align();
  281. if (text_align.has_value())
  282. computed_values.set_text_align(text_align.value());
  283. auto white_space = specified_style.white_space();
  284. if (white_space.has_value())
  285. computed_values.set_white_space(white_space.value());
  286. auto float_ = specified_style.float_();
  287. if (float_.has_value())
  288. computed_values.set_float(float_.value());
  289. auto clear = specified_style.clear();
  290. if (clear.has_value())
  291. computed_values.set_clear(clear.value());
  292. auto overflow_x = specified_style.overflow_x();
  293. if (overflow_x.has_value())
  294. computed_values.set_overflow_x(overflow_x.value());
  295. auto overflow_y = specified_style.overflow_y();
  296. if (overflow_y.has_value())
  297. computed_values.set_overflow_y(overflow_y.value());
  298. auto cursor = specified_style.cursor();
  299. if (cursor.has_value())
  300. computed_values.set_cursor(cursor.value());
  301. auto pointer_events = specified_style.pointer_events();
  302. if (pointer_events.has_value())
  303. computed_values.set_pointer_events(pointer_events.value());
  304. auto text_decoration_line = specified_style.text_decoration_line();
  305. if (text_decoration_line.has_value())
  306. computed_values.set_text_decoration_line(text_decoration_line.value());
  307. auto text_transform = specified_style.text_transform();
  308. if (text_transform.has_value())
  309. computed_values.set_text_transform(text_transform.value());
  310. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  311. computed_values.set_list_style_type(list_style_type.value());
  312. auto list_style_image = specified_style.property(CSS::PropertyID::ListStyleImage);
  313. if (list_style_image.has_value() && list_style_image.value()->is_image()) {
  314. m_list_style_image = list_style_image.value()->as_image();
  315. m_list_style_image->load_bitmap(document());
  316. }
  317. computed_values.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, *this, CSS::InitialValues::color()));
  318. computed_values.set_z_index(specified_style.z_index());
  319. computed_values.set_opacity(specified_style.opacity());
  320. if (computed_values.opacity() == 0)
  321. m_visible = false;
  322. if (auto width = specified_style.property(CSS::PropertyID::Width); width.has_value() && !width.value()->has_auto())
  323. m_has_definite_width = true;
  324. computed_values.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  325. computed_values.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  326. computed_values.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  327. if (auto height = specified_style.property(CSS::PropertyID::Height); height.has_value() && !height.value()->has_auto())
  328. m_has_definite_height = true;
  329. computed_values.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  330. computed_values.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  331. computed_values.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  332. computed_values.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  333. 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)));
  334. 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)));
  335. computed_values.set_box_shadow(specified_style.box_shadow());
  336. computed_values.set_transformations(specified_style.transformations());
  337. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  338. // FIXME: The default border color value is `currentcolor`, but since we can't resolve that easily,
  339. // we just manually grab the value from `color`. This makes it dependent on `color` being
  340. // specified first, so it's far from ideal.
  341. border.color = specified_style.color_or_fallback(color_property, *this, computed_values.color());
  342. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  343. if (border.line_style == CSS::LineStyle::None)
  344. border.width = 0;
  345. else
  346. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  347. };
  348. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  349. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  350. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  351. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  352. if (auto fill = specified_style.property(CSS::PropertyID::Fill); fill.has_value())
  353. computed_values.set_fill(fill.value()->to_color(*this));
  354. if (auto stroke = specified_style.property(CSS::PropertyID::Stroke); stroke.has_value())
  355. computed_values.set_stroke(stroke.value()->to_color(*this));
  356. if (auto stroke_width = specified_style.property(CSS::PropertyID::StrokeWidth); stroke_width.has_value()) {
  357. // FIXME: Converting to pixels isn't really correct - values should be in "user units"
  358. // https://svgwg.org/svg2-draft/coords.html#TermUserUnits
  359. if (stroke_width.value()->is_numeric())
  360. computed_values.set_stroke_width(CSS::Length::make_px(stroke_width.value()->to_number()));
  361. else
  362. computed_values.set_stroke_width(stroke_width.value()->to_length());
  363. }
  364. }
  365. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  366. {
  367. }
  368. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  369. {
  370. }
  371. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  372. {
  373. }
  374. bool Node::handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned, int wheel_delta)
  375. {
  376. if (auto* containing_block = this->containing_block()) {
  377. if (!containing_block->is_scrollable())
  378. return false;
  379. auto new_offset = containing_block->scroll_offset();
  380. new_offset.translate_by(0, wheel_delta);
  381. containing_block->set_scroll_offset(new_offset);
  382. return true;
  383. }
  384. return false;
  385. }
  386. bool Node::is_root_element() const
  387. {
  388. if (is_anonymous())
  389. return false;
  390. return is<HTML::HTMLHtmlElement>(*dom_node());
  391. }
  392. String Node::class_name() const
  393. {
  394. return demangle(typeid(*this).name());
  395. }
  396. bool Node::is_inline_block() const
  397. {
  398. return is_inline() && is<BlockContainer>(*this);
  399. }
  400. NonnullRefPtr<NodeWithStyle> NodeWithStyle::create_anonymous_wrapper() const
  401. {
  402. auto wrapper = adopt_ref(*new BlockContainer(const_cast<DOM::Document&>(document()), nullptr, m_computed_values.clone_inherited_values()));
  403. wrapper->m_font = m_font;
  404. wrapper->m_font_size = m_font_size;
  405. wrapper->m_line_height = m_line_height;
  406. wrapper->m_background_image = m_background_image;
  407. return wrapper;
  408. }
  409. }