2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-10-31 16:46:55 -03:00
|
|
|
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2021-05-13 10:39:30 -03:00
|
|
|
#include <LibWeb/HTML/HTMLHtmlElement.h>
|
2021-10-06 15:02:41 -03:00
|
|
|
#include <LibWeb/Layout/BlockContainer.h>
|
2020-11-22 11:53:01 -03:00
|
|
|
#include <LibWeb/Layout/Box.h>
|
2021-09-15 08:19:42 -03:00
|
|
|
#include <LibWeb/Layout/FormattingContext.h>
|
2022-03-10 19:13:37 -03:00
|
|
|
#include <LibWeb/Painting/PaintableBox.h>
|
2019-10-15 11:48:38 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
namespace Web::Layout {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2022-03-09 19:53:41 -03: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 06:56:12 -03:00
|
|
|
// https://www.w3.org/TR/css-overflow-3/#overflow-control
|
2023-03-26 11:38:05 -03:00
|
|
|
static bool overflow_value_makes_box_a_scroll_container(CSS::Overflow overflow)
|
2023-03-26 06:56:12 -03: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-26 11:01:20 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2023-03-26 06:56:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/css-overflow-3/#scroll-container
|
|
|
|
|
bool Box::is_scroll_container() const
|
|
|
|
|
{
|
2023-05-29 03:52:34 -03: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 06:56:12 -03: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-08-08 10:43:48 -03:00
|
|
|
bool Box::is_user_scrollable() const
|
2023-01-23 13:04:24 -03:00
|
|
|
{
|
|
|
|
|
// FIXME: Support horizontal scroll as well (overflow-x)
|
2023-08-08 10:35:15 -03:00
|
|
|
return computed_values().overflow_y() == CSS::Overflow::Scroll || computed_values().overflow_y() == CSS::Overflow::Auto;
|
2023-01-23 13:04:24 -03:00
|
|
|
}
|
|
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
void Box::set_needs_display()
|
2019-10-15 11:48:38 -03:00
|
|
|
{
|
2023-08-22 11:00:42 -03:00
|
|
|
if (!navigable())
|
|
|
|
|
return;
|
|
|
|
|
|
2023-04-20 12:00:42 -03:00
|
|
|
if (paintable_box())
|
2023-08-22 11:00:42 -03:00
|
|
|
navigable()->set_needs_display(paintable_box()->absolute_rect());
|
2019-10-15 11:48:38 -03:00
|
|
|
}
|
2019-10-19 06:49:46 -03:00
|
|
|
|
2020-11-22 11:53:01 -03:00
|
|
|
bool Box::is_body() const
|
2019-10-19 06:49:46 -03:00
|
|
|
{
|
2020-11-22 10:46:36 -03:00
|
|
|
return dom_node() && dom_node() == document().body();
|
2019-10-19 06:49:46 -03:00
|
|
|
}
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2023-01-11 08:51:49 -03:00
|
|
|
JS::GCPtr<Painting::Paintable> Box::create_paintable() const
|
2021-10-28 12:08:42 -03:00
|
|
|
{
|
2022-03-10 11:50:57 -03:00
|
|
|
return Painting::PaintableBox::create(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-06 19:51:05 -03:00
|
|
|
Painting::PaintableBox* Box::paintable_box()
|
|
|
|
|
{
|
|
|
|
|
return static_cast<Painting::PaintableBox*>(Node::paintable());
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-20 12:00:42 -03:00
|
|
|
Painting::PaintableBox const* Box::paintable_box() const
|
2022-03-10 11:50:57 -03:00
|
|
|
{
|
|
|
|
|
return static_cast<Painting::PaintableBox const*>(Node::paintable());
|
2021-10-28 12:08:42 -03:00
|
|
|
}
|
|
|
|
|
|
2023-09-03 19:33:58 -03:00
|
|
|
Optional<CSSPixelFraction> Box::preferred_aspect_ratio() const
|
2023-06-08 12:47:50 -03:00
|
|
|
{
|
|
|
|
|
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();
|
2023-09-03 19:33:58 -03:00
|
|
|
return computed_aspect_ratio.preferred_ratio.map([](CSS::Ratio const& ratio) { return CSSPixelFraction(CSSPixels(ratio.numerator()), CSSPixels(ratio.denominator())); });
|
2023-06-08 12:47:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|