Node.cpp 12 KB

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