Node.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 <LibGUI/Painter.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Element.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/ReplacedBox.h>
  37. #include <LibWeb/Layout/TextNode.h>
  38. #include <LibWeb/Page/Frame.h>
  39. #include <typeinfo>
  40. namespace Web::Layout {
  41. Node::Node(DOM::Document& document, DOM::Node* node)
  42. : m_document(document)
  43. , m_dom_node(node)
  44. {
  45. if (m_dom_node)
  46. m_dom_node->set_layout_node({}, this);
  47. }
  48. Node::~Node()
  49. {
  50. if (m_dom_node && m_dom_node->layout_node() == this)
  51. m_dom_node->set_layout_node({}, nullptr);
  52. }
  53. bool Node::can_contain_boxes_with_position_absolute() const
  54. {
  55. return computed_values().position() != CSS::Position::Static || is<InitialContainingBlockBox>(*this);
  56. }
  57. const BlockBox* Node::containing_block() const
  58. {
  59. auto nearest_block_ancestor = [this] {
  60. auto* ancestor = parent();
  61. while (ancestor && !is<BlockBox>(*ancestor))
  62. ancestor = ancestor->parent();
  63. return downcast<BlockBox>(ancestor);
  64. };
  65. if (is<TextNode>(*this))
  66. return nearest_block_ancestor();
  67. auto position = computed_values().position();
  68. if (position == CSS::Position::Absolute) {
  69. auto* ancestor = parent();
  70. while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
  71. ancestor = ancestor->parent();
  72. while (ancestor && (!is<BlockBox>(ancestor) || ancestor->is_anonymous()))
  73. ancestor = ancestor->containing_block();
  74. return downcast<BlockBox>(ancestor);
  75. }
  76. if (position == CSS::Position::Fixed)
  77. return &root();
  78. return nearest_block_ancestor();
  79. }
  80. void Node::paint(PaintContext& context, PaintPhase phase)
  81. {
  82. if (!is_visible())
  83. return;
  84. before_children_paint(context, phase);
  85. for_each_child_in_paint_order([&](auto& child) {
  86. child.paint(context, phase);
  87. });
  88. after_children_paint(context, phase);
  89. }
  90. HitTestResult Node::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  91. {
  92. HitTestResult result;
  93. for_each_child_in_paint_order([&](auto& child) {
  94. auto child_result = child.hit_test(position, type);
  95. if (child_result.layout_node)
  96. result = child_result;
  97. });
  98. return result;
  99. }
  100. const Frame& Node::frame() const
  101. {
  102. ASSERT(document().frame());
  103. return *document().frame();
  104. }
  105. Frame& Node::frame()
  106. {
  107. ASSERT(document().frame());
  108. return *document().frame();
  109. }
  110. const InitialContainingBlockBox& Node::root() const
  111. {
  112. ASSERT(document().layout_node());
  113. return *document().layout_node();
  114. }
  115. InitialContainingBlockBox& Node::root()
  116. {
  117. ASSERT(document().layout_node());
  118. return *document().layout_node();
  119. }
  120. void Node::split_into_lines(InlineFormattingContext& context, LayoutMode layout_mode)
  121. {
  122. for_each_child([&](auto& child) {
  123. child.split_into_lines(context, layout_mode);
  124. });
  125. }
  126. void Node::set_needs_display()
  127. {
  128. if (auto* block = containing_block()) {
  129. block->for_each_fragment([&](auto& fragment) {
  130. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  131. frame().set_needs_display(enclosing_int_rect(fragment.absolute_rect()));
  132. }
  133. return IterationDecision::Continue;
  134. });
  135. }
  136. }
  137. float Node::font_size() const
  138. {
  139. // FIXME: This doesn't work right for relative font-sizes
  140. auto length = specified_style().length_or_fallback(CSS::PropertyID::FontSize, CSS::Length(10, CSS::Length::Type::Px));
  141. return length.raw_value();
  142. }
  143. Gfx::FloatPoint Node::box_type_agnostic_position() const
  144. {
  145. if (is<Box>(*this))
  146. return downcast<Box>(*this).absolute_position();
  147. ASSERT(is_inline());
  148. Gfx::FloatPoint position;
  149. if (auto* block = containing_block()) {
  150. block->for_each_fragment([&](auto& fragment) {
  151. if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
  152. position = fragment.absolute_rect().location();
  153. return IterationDecision::Break;
  154. }
  155. return IterationDecision::Continue;
  156. });
  157. }
  158. return position;
  159. }
  160. bool Node::is_floating() const
  161. {
  162. if (!has_style())
  163. return false;
  164. return computed_values().float_() != CSS::Float::None;
  165. }
  166. bool Node::is_positioned() const
  167. {
  168. return has_style() && computed_values().position() != CSS::Position::Static;
  169. }
  170. bool Node::is_absolutely_positioned() const
  171. {
  172. if (!has_style())
  173. return false;
  174. auto position = computed_values().position();
  175. return position == CSS::Position::Absolute || position == CSS::Position::Fixed;
  176. }
  177. bool Node::is_fixed_position() const
  178. {
  179. if (!has_style())
  180. return false;
  181. auto position = computed_values().position();
  182. return position == CSS::Position::Fixed;
  183. }
  184. NodeWithStyle::NodeWithStyle(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> specified_style)
  185. : Node(document, node)
  186. , m_specified_style(move(specified_style))
  187. {
  188. m_has_style = true;
  189. apply_style(*m_specified_style);
  190. }
  191. void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
  192. {
  193. auto& computed_values = static_cast<CSS::MutableComputedValues&>(m_computed_values);
  194. m_font = specified_style.font();
  195. auto position = specified_style.position();
  196. if (position.has_value())
  197. computed_values.set_position(position.value());
  198. auto text_align = specified_style.text_align();
  199. if (text_align.has_value())
  200. computed_values.set_text_align(text_align.value());
  201. auto white_space = specified_style.white_space();
  202. if (white_space.has_value())
  203. computed_values.set_white_space(white_space.value());
  204. auto float_ = specified_style.float_();
  205. if (float_.has_value())
  206. computed_values.set_float(float_.value());
  207. auto clear = specified_style.clear();
  208. if (clear.has_value())
  209. computed_values.set_clear(clear.value());
  210. auto text_decoration_line = specified_style.text_decoration_line();
  211. if (text_decoration_line.has_value())
  212. computed_values.set_text_decoration_line(text_decoration_line.value());
  213. auto text_transform = specified_style.text_transform();
  214. if (text_transform.has_value())
  215. computed_values.set_text_transform(text_transform.value());
  216. if (auto list_style_type = specified_style.list_style_type(); list_style_type.has_value())
  217. computed_values.set_list_style_type(list_style_type.value());
  218. computed_values.set_color(specified_style.color_or_fallback(CSS::PropertyID::Color, document(), Color::Black));
  219. computed_values.set_background_color(specified_style.color_or_fallback(CSS::PropertyID::BackgroundColor, document(), Color::Transparent));
  220. computed_values.set_z_index(specified_style.z_index());
  221. computed_values.set_width(specified_style.length_or_fallback(CSS::PropertyID::Width, {}));
  222. computed_values.set_min_width(specified_style.length_or_fallback(CSS::PropertyID::MinWidth, {}));
  223. computed_values.set_max_width(specified_style.length_or_fallback(CSS::PropertyID::MaxWidth, {}));
  224. computed_values.set_height(specified_style.length_or_fallback(CSS::PropertyID::Height, {}));
  225. computed_values.set_min_height(specified_style.length_or_fallback(CSS::PropertyID::MinHeight, {}));
  226. computed_values.set_max_height(specified_style.length_or_fallback(CSS::PropertyID::MaxHeight, {}));
  227. computed_values.set_offset(specified_style.length_box(CSS::PropertyID::Left, CSS::PropertyID::Top, CSS::PropertyID::Right, CSS::PropertyID::Bottom, CSS::Length::make_auto()));
  228. 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)));
  229. 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)));
  230. auto do_border_style = [&](CSS::BorderData& border, CSS::PropertyID width_property, CSS::PropertyID color_property, CSS::PropertyID style_property) {
  231. border.width = specified_style.length_or_fallback(width_property, {}).resolved_or_zero(*this, 0).to_px(*this);
  232. border.color = specified_style.color_or_fallback(color_property, document(), Color::Transparent);
  233. border.line_style = specified_style.line_style(style_property).value_or(CSS::LineStyle::None);
  234. };
  235. do_border_style(computed_values.border_left(), CSS::PropertyID::BorderLeftWidth, CSS::PropertyID::BorderLeftColor, CSS::PropertyID::BorderLeftStyle);
  236. do_border_style(computed_values.border_top(), CSS::PropertyID::BorderTopWidth, CSS::PropertyID::BorderTopColor, CSS::PropertyID::BorderTopStyle);
  237. do_border_style(computed_values.border_right(), CSS::PropertyID::BorderRightWidth, CSS::PropertyID::BorderRightColor, CSS::PropertyID::BorderRightStyle);
  238. do_border_style(computed_values.border_bottom(), CSS::PropertyID::BorderBottomWidth, CSS::PropertyID::BorderBottomColor, CSS::PropertyID::BorderBottomStyle);
  239. }
  240. void Node::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  241. {
  242. }
  243. void Node::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  244. {
  245. }
  246. void Node::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned, unsigned)
  247. {
  248. }
  249. bool Node::is_root_element() const
  250. {
  251. if (is_anonymous())
  252. return false;
  253. return is<HTML::HTMLHtmlElement>(*dom_node());
  254. }
  255. String Node::class_name() const
  256. {
  257. return demangle(typeid(*this).name());
  258. }
  259. bool Node::is_inline_block() const
  260. {
  261. return is_inline() && is<BlockBox>(*this);
  262. }
  263. }