Box.cpp 2.9 KB

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