2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-10-31 19:46:55 +00:00
|
|
|
* Copyright (c) 2021-2022, 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-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>
|
2022-03-10 23:13:37 +01:00
|
|
|
#include <LibWeb/Painting/PaintableBox.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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-26 11:56:12 +02:00
|
|
|
// https://www.w3.org/TR/css-overflow-3/#overflow-control
|
2023-03-26 10:38:05 -04:00
|
|
|
static bool overflow_value_makes_box_a_scroll_container(CSS::Overflow overflow)
|
2023-03-26 11:56:12 +02:00
|
|
|
{
|
|
|
|
switch (overflow) {
|
|
|
|
case CSS::Overflow::Clip:
|
|
|
|
case CSS::Overflow::Visible:
|
|
|
|
return false;
|
|
|
|
case CSS::Overflow::Auto:
|
|
|
|
case CSS::Overflow::Hidden:
|
|
|
|
case CSS::Overflow::Scroll:
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-27 01:01:20 +11:00
|
|
|
VERIFY_NOT_REACHED();
|
2023-03-26 11:56:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/css-overflow-3/#scroll-container
|
|
|
|
bool Box::is_scroll_container() const
|
|
|
|
{
|
2023-05-29 08:52:34 +02:00
|
|
|
// NOTE: This isn't in the spec, but we want the viewport to behave like a scroll container.
|
|
|
|
if (is_viewport())
|
|
|
|
return true;
|
|
|
|
|
2023-03-26 11:56:12 +02:00
|
|
|
return overflow_value_makes_box_a_scroll_container(computed_values().overflow_x())
|
|
|
|
|| overflow_value_makes_box_a_scroll_container(computed_values().overflow_y());
|
|
|
|
}
|
|
|
|
|
2023-01-23 17:04:24 +01:00
|
|
|
bool Box::is_scrollable() const
|
|
|
|
{
|
|
|
|
// FIXME: Support horizontal scroll as well (overflow-x)
|
2023-08-08 15:35:15 +02:00
|
|
|
return computed_values().overflow_y() == CSS::Overflow::Scroll || computed_values().overflow_y() == CSS::Overflow::Auto;
|
2023-01-23 17:04:24 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 15:53:01 +01:00
|
|
|
void Box::set_needs_display()
|
2019-10-15 16:48:38 +02:00
|
|
|
{
|
2023-04-20 16:00:42 +01:00
|
|
|
if (paintable_box())
|
|
|
|
browsing_context().set_needs_display(paintable_box()->absolute_rect());
|
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
|
|
|
|
2023-01-11 12:51:49 +01:00
|
|
|
JS::GCPtr<Painting::Paintable> Box::create_paintable() const
|
2021-10-28 17:08:42 +02:00
|
|
|
{
|
2022-03-10 15:50:57 +01:00
|
|
|
return Painting::PaintableBox::create(*this);
|
|
|
|
}
|
|
|
|
|
2023-08-07 00:51:05 +02:00
|
|
|
Painting::PaintableBox* Box::paintable_box()
|
|
|
|
{
|
|
|
|
return static_cast<Painting::PaintableBox*>(Node::paintable());
|
|
|
|
}
|
|
|
|
|
2023-04-20 16:00:42 +01:00
|
|
|
Painting::PaintableBox const* Box::paintable_box() const
|
2022-03-10 15:50:57 +01:00
|
|
|
{
|
|
|
|
return static_cast<Painting::PaintableBox const*>(Node::paintable());
|
2021-10-28 17:08:42 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 16:47:50 +01:00
|
|
|
Optional<float> Box::preferred_aspect_ratio() const
|
|
|
|
{
|
|
|
|
auto computed_aspect_ratio = computed_values().aspect_ratio();
|
|
|
|
if (computed_aspect_ratio.use_natural_aspect_ratio_if_available && natural_aspect_ratio().has_value())
|
|
|
|
return natural_aspect_ratio();
|
|
|
|
return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return ratio.value(); });
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:27:02 +01:00
|
|
|
}
|