Node.cpp 13 KB

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