2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-09-15 12:19:42 +01:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-02-10 08:25:35 +01:00
|
|
|
#include <LibGfx/Painter.h>
|
2020-03-07 10:32:51 +01:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2021-11-18 15:01:28 +01:00
|
|
|
#include <LibWeb/HTML/BrowsingContext.h>
|
2020-07-26 15:08:16 +02:00
|
|
|
#include <LibWeb/HTML/HTMLBodyElement.h>
|
2021-05-13 09:39:30 -04:00
|
|
|
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
2021-10-06 20:02:41 +02:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-11-22 15:53:01 +01:00
|
|
|
#include <LibWeb/Layout/Box.h>
|
2021-09-15 12:19:42 +01:00
|
|
|
#include <LibWeb/Layout/FormattingContext.h>
|
2021-09-19 15:58:26 +01:00
|
|
|
#include <LibWeb/Painting/BackgroundPainting.h>
|
2020-12-04 18:02:21 +01:00
|
|
|
#include <LibWeb/Painting/BorderPainting.h>
|
2022-03-10 11:26:01 +01:00
|
|
|
#include <LibWeb/Painting/Paintable.h>
|
2021-09-19 15:00:47 +01:00
|
|
|
#include <LibWeb/Painting/ShadowPainting.h>
|
2019-10-15 16:48:38 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
namespace Web::Layout {
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2022-03-09 23:53:41 +01:00
|
|
|
Box::Box(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(style))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::Box(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Box::~Box()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:26:01 +01:00
|
|
|
void Box::set_paint_box(OwnPtr<Painting::Paintable> paint_box)
|
2022-03-10 11:12:06 +01:00
|
|
|
{
|
|
|
|
m_paint_box = move(paint_box);
|
|
|
|
}
|
|
|
|
|
2021-09-15 12:19:42 +01:00
|
|
|
// https://www.w3.org/TR/css-display-3/#out-of-flow
|
|
|
|
bool Box::is_out_of_flow(FormattingContext const& formatting_context) const
|
|
|
|
{
|
|
|
|
// A box is out of flow if either:
|
|
|
|
|
|
|
|
// 1. It is floated (which requires that floating is not inhibited).
|
|
|
|
if (!formatting_context.inhibits_floating() && computed_values().float_() != CSS::Float::None)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// 2. It is "absolutely positioned".
|
|
|
|
switch (computed_values().position()) {
|
|
|
|
case CSS::Position::Absolute:
|
|
|
|
case CSS::Position::Fixed:
|
|
|
|
return true;
|
|
|
|
case CSS::Position::Static:
|
|
|
|
case CSS::Position::Relative:
|
|
|
|
case CSS::Position::Sticky:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
HitTestResult Box::hit_test(const Gfx::IntPoint& position, HitTestType type) const
|
2019-10-15 16:48:38 +02:00
|
|
|
{
|
|
|
|
// FIXME: It would be nice if we could confidently skip over hit testing
|
|
|
|
// parts of the layout tree, but currently we can't just check
|
|
|
|
// m_rect.contains() since inline text rects can't be trusted..
|
2022-03-09 23:53:41 +01:00
|
|
|
HitTestResult result { m_paint_box->absolute_border_box_rect().contains(position.x(), position.y()) ? this : nullptr };
|
2020-12-06 19:59:28 +01:00
|
|
|
for_each_child_in_paint_order([&](auto& child) {
|
2020-08-05 16:55:56 +02:00
|
|
|
auto child_result = child.hit_test(position, type);
|
2019-10-15 16:48:38 +02:00
|
|
|
if (child_result.layout_node)
|
|
|
|
result = child_result;
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
void Box::set_needs_display()
|
2019-10-15 16:48:38 +02:00
|
|
|
{
|
|
|
|
if (!is_inline()) {
|
2022-03-09 23:53:41 +01:00
|
|
|
browsing_context().set_needs_display(enclosing_int_rect(m_paint_box->absolute_rect()));
|
2019-10-15 16:48:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
Node::set_needs_display();
|
2019-10-15 16:48:38 +02:00
|
|
|
}
|
2019-10-19 11:49:46 +02:00
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
bool Box::is_body() const
|
2019-10-19 11:49:46 +02:00
|
|
|
{
|
2020-11-22 14:46:36 +01:00
|
|
|
return dom_node() && dom_node() == document().body();
|
2019-10-19 11:49:46 +02:00
|
|
|
}
|
2020-03-07 10:27:02 +01:00
|
|
|
|
2022-03-10 14:02:25 +01:00
|
|
|
OwnPtr<Painting::Paintable> Box::create_paintable() const
|
2021-10-28 17:08:42 +02:00
|
|
|
{
|
2022-03-10 14:02:25 +01:00
|
|
|
return Painting::Paintable::create(*this);
|
2021-10-28 17:08:42 +02:00
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|