Box.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/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-display-3/#out-of-flow
  27. bool Box::is_out_of_flow(FormattingContext const& formatting_context) const
  28. {
  29. // A box is out of flow if either:
  30. // 1. It is floated (which requires that floating is not inhibited).
  31. if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
  32. return true;
  33. // 2. It is "absolutely positioned".
  34. switch (computed_values().position()) {
  35. case CSS::Position::Absolute:
  36. case CSS::Position::Fixed:
  37. return true;
  38. case CSS::Position::Static:
  39. case CSS::Position::Relative:
  40. case CSS::Position::Sticky:
  41. break;
  42. }
  43. return false;
  44. }
  45. void Box::set_needs_display()
  46. {
  47. if (!is_inline()) {
  48. browsing_context().set_needs_display(enclosing_int_rect(paint_box()->absolute_rect()));
  49. return;
  50. }
  51. Node::set_needs_display();
  52. }
  53. bool Box::is_body() const
  54. {
  55. return dom_node() && dom_node() == document().body();
  56. }
  57. RefPtr<Painting::Paintable> Box::create_paintable() const
  58. {
  59. return Painting::PaintableBox::create(*this);
  60. }
  61. Painting::PaintableBox const* Box::paint_box() const
  62. {
  63. return static_cast<Painting::PaintableBox const*>(Node::paintable());
  64. }
  65. }