Box.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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/HTMLHtmlElement.h>
  10. #include <LibWeb/Layout/BlockContainer.h>
  11. #include <LibWeb/Layout/Box.h>
  12. #include <LibWeb/Layout/FormattingContext.h>
  13. #include <LibWeb/Painting/PaintableBox.h>
  14. namespace Web::Layout {
  15. Box::Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  16. : NodeWithStyleAndBoxModelMetrics(document, node, move(style))
  17. {
  18. }
  19. Box::Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  20. : NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
  21. {
  22. }
  23. Box::~Box()
  24. {
  25. }
  26. // https://www.w3.org/TR/css-overflow-3/#overflow-control
  27. static bool overflow_value_makes_box_a_scroll_container(CSS::Overflow overflow)
  28. {
  29. switch (overflow) {
  30. case CSS::Overflow::Clip:
  31. case CSS::Overflow::Visible:
  32. return false;
  33. case CSS::Overflow::Auto:
  34. case CSS::Overflow::Hidden:
  35. case CSS::Overflow::Scroll:
  36. return true;
  37. }
  38. VERIFY_NOT_REACHED();
  39. }
  40. // https://www.w3.org/TR/css-overflow-3/#scroll-container
  41. bool Box::is_scroll_container() const
  42. {
  43. // NOTE: This isn't in the spec, but we want the viewport to behave like a scroll container.
  44. if (is_viewport())
  45. return true;
  46. return overflow_value_makes_box_a_scroll_container(computed_values().overflow_x())
  47. || overflow_value_makes_box_a_scroll_container(computed_values().overflow_y());
  48. }
  49. bool Box::is_user_scrollable() const
  50. {
  51. // FIXME: Support horizontal scroll as well (overflow-x)
  52. return computed_values().overflow_y() == CSS::Overflow::Scroll || computed_values().overflow_y() == CSS::Overflow::Auto;
  53. }
  54. void Box::set_needs_display()
  55. {
  56. if (!navigable())
  57. return;
  58. if (paintable_box())
  59. navigable()->set_needs_display(paintable_box()->absolute_rect());
  60. }
  61. bool Box::is_body() const
  62. {
  63. return dom_node() && dom_node() == document().body();
  64. }
  65. JS::GCPtr<Painting::Paintable> Box::create_paintable() const
  66. {
  67. return Painting::PaintableBox::create(*this);
  68. }
  69. Painting::PaintableBox* Box::paintable_box()
  70. {
  71. return static_cast<Painting::PaintableBox*>(Node::paintable());
  72. }
  73. Painting::PaintableBox const* Box::paintable_box() const
  74. {
  75. return static_cast<Painting::PaintableBox const*>(Node::paintable());
  76. }
  77. Optional<CSSPixelFraction> Box::preferred_aspect_ratio() const
  78. {
  79. auto computed_aspect_ratio = computed_values().aspect_ratio();
  80. if (computed_aspect_ratio.use_natural_aspect_ratio_if_available && natural_aspect_ratio().has_value())
  81. return natural_aspect_ratio();
  82. return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return CSSPixelFraction(CSSPixels(ratio.numerator()), CSSPixels(ratio.denominator())); });
  83. }
  84. }