2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.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
|
|
|
|
2024-12-20 12:35:12 -03:00
|
|
|
Box::Box(DOM::Document& document, DOM::Node* node, GC::Ref<CSS::ComputedProperties> style)
|
2022-03-09 19:53:41 -03:00
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(style))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-27 04:38:27 -03:00
|
|
|
Box::Box(DOM::Document& document, DOM::Node* node, NonnullOwnPtr<CSS::ComputedValues> computed_values)
|
2022-03-09 19:53:41 -03:00
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, move(computed_values))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Box::~Box()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-18 16:39:26 -03:00
|
|
|
Optional<CSSPixels> Box::natural_width() const
|
|
|
|
|
{
|
|
|
|
|
// https://drafts.csswg.org/css-contain-2/#containment-size
|
|
|
|
|
// Replaced elements must be treated as having a natural width and height of 0 and no natural aspect
|
|
|
|
|
// ratio.
|
|
|
|
|
if (dom_node() && dom_node()->is_element() && as<DOM::Element>(dom_node())->has_size_containment())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return m_natural_width;
|
|
|
|
|
}
|
|
|
|
|
Optional<CSSPixels> Box::natural_height() const
|
|
|
|
|
{
|
|
|
|
|
// https://drafts.csswg.org/css-contain-2/#containment-size
|
|
|
|
|
// Replaced elements must be treated as having a natural width and height of 0 and no natural aspect
|
|
|
|
|
// ratio.
|
|
|
|
|
if (dom_node() && dom_node()->is_element() && as<DOM::Element>(dom_node())->has_size_containment())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return m_natural_height;
|
|
|
|
|
}
|
|
|
|
|
Optional<CSSPixelFraction> Box::natural_aspect_ratio() const
|
|
|
|
|
{
|
|
|
|
|
// https://drafts.csswg.org/css-contain-2/#containment-size
|
|
|
|
|
// Replaced elements must be treated as having a natural width and height of 0 and no natural aspect
|
|
|
|
|
// ratio.
|
|
|
|
|
if (dom_node() && dom_node()->is_element() && as<DOM::Element>(dom_node())->has_size_containment())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return m_natural_aspect_ratio;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-10 14:02:03 -03:00
|
|
|
void Box::visit_edges(Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
|
|
|
|
visitor.visit(m_contained_abspos_children);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-14 12:01:23 -03:00
|
|
|
GC::Ptr<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()
|
|
|
|
|
{
|
2024-10-16 10:19:32 -03:00
|
|
|
return static_cast<Painting::PaintableBox*>(Node::first_paintable());
|
2023-08-06 19:51:05 -03:00
|
|
|
}
|
|
|
|
|
|
2023-04-20 12:00:42 -03:00
|
|
|
Painting::PaintableBox const* Box::paintable_box() const
|
2022-03-10 11:50:57 -03:00
|
|
|
{
|
2024-10-16 10:19:32 -03:00
|
|
|
return static_cast<Painting::PaintableBox const*>(Node::first_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();
|
2024-03-23 12:46:08 -03:00
|
|
|
|
|
|
|
|
if (!computed_aspect_ratio.preferred_ratio.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
auto ratio = computed_aspect_ratio.preferred_ratio.release_value();
|
|
|
|
|
if (ratio.is_degenerate())
|
|
|
|
|
return {};
|
|
|
|
|
|
2024-08-22 06:36:03 -03:00
|
|
|
return CSSPixelFraction(ratio.numerator(), ratio.denominator());
|
2023-06-08 12:47:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|