Box.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGfx/Painter.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/HTMLBodyElement.h>
  11. #include <LibWeb/HTML/HTMLHtmlElement.h>
  12. #include <LibWeb/Layout/BlockContainer.h>
  13. #include <LibWeb/Layout/Box.h>
  14. #include <LibWeb/Layout/FormattingContext.h>
  15. #include <LibWeb/Painting/BackgroundPainting.h>
  16. #include <LibWeb/Painting/BorderPainting.h>
  17. #include <LibWeb/Painting/Paintable.h>
  18. #include <LibWeb/Painting/ShadowPainting.h>
  19. namespace Web::Layout {
  20. Box::Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  21. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  22. {
  23. }
  24. Box::Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  25. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  26. {
  27. }
  28. Box::~Box()
  29. {
  30. }
  31. // https://www.w3.org/TR/css-display-3/#out-of-flow
  32. bool Box::is_out_of_flow(FormattingContext const& formatting_context) const
  33. {
  34. // A box is out of flow if either:
  35. // 1. It is floated (which requires that floating is not inhibited).
  36. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  37. return true;
  38. // 2. It is "absolutely positioned".
  39. switch (computed_values().position()) {
  40. case CSS::Position::Absolute:
  41. case CSS::Position::Fixed:
  42. return true;
  43. case CSS::Position::Static:
  44. case CSS::Position::Relative:
  45. case CSS::Position::Sticky:
  46. break;
  47. }
  48. return false;
  49. }
  50. HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
  51. {
  52. // FIXME: It would be nice if we could confidently skip over hit testing
  53. // parts of the layout tree, but currently we can't just check
  54. // m_rect.contains() since inline text rects can't be trusted..
  55. HitTestResult result { paint_box()->absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
  56. for_each_child_in_paint_order([&](auto& child) {
  57. auto child_result = child.hit_test(position, type);
  58. if (child_result.layout_node)
  59. result = child_result;
  60. });
  61. return result;
  62. }
  63. void Box::set_needs_display()
  64. {
  65. if (!is_inline()) {
  66. browsing_context().set_needs_display(enclosing_int_rect(paint_box()->absolute_rect()));
  67. return;
  68. }
  69. Node::set_needs_display();
  70. }
  71. bool Box::is_body() const
  72. {
  73. return dom_node() && dom_node() == document().body();
  74. }
  75. RefPtr<Painting::Paintable> Box::create_paintable() const
  76. {
  77. return Painting::PaintableBox::create(*this);
  78. }
  79. Painting::PaintableBox const* Box::paint_box() const
  80. {
  81. return static_cast<Painting::PaintableBox const*>(Node::paintable());
  82. }
  83. }