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>
|
2025-04-18 20:14:53 -03:00
|
|
|
#include <LibWeb/Layout/TableWrapper.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
|
|
|
|
2026-06-06 09:03:41 -03:00
|
|
|
Box::Box(DOM::Document& document, DOM::Node* node, CSS::ComputedProperties const& style)
|
|
|
|
|
: NodeWithStyleAndBoxModelMetrics(document, node, style)
|
2022-03-09 19:53:41 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-11 03:31:07 -03:00
|
|
|
CSS::SizeWithAspectRatio Box::auto_content_box_size() const
|
2025-01-18 16:39:26 -03:00
|
|
|
{
|
|
|
|
|
// 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.
|
2025-05-09 16:34:47 -03:00
|
|
|
if (has_size_containment())
|
2026-01-11 03:31:07 -03:00
|
|
|
return { 0, 0, {} };
|
|
|
|
|
|
|
|
|
|
return compute_auto_content_box_size();
|
2025-01-18 16:39:26 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-07 08:38:05 -03:00
|
|
|
RefPtr<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);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-07 08:38:05 -03:00
|
|
|
RefPtr<Painting::PaintableBox> Box::paintable_box()
|
2023-08-06 19:51:05 -03:00
|
|
|
{
|
2026-05-07 08:38:05 -03:00
|
|
|
if (auto paintable = Node::first_paintable())
|
|
|
|
|
return static_cast<Painting::PaintableBox&>(*paintable);
|
|
|
|
|
return nullptr;
|
2023-08-06 19:51:05 -03:00
|
|
|
}
|
|
|
|
|
|
2026-05-07 08:38:05 -03:00
|
|
|
RefPtr<Painting::PaintableBox const> Box::paintable_box() const
|
2022-03-10 11:50:57 -03:00
|
|
|
{
|
2026-05-07 08:38:05 -03:00
|
|
|
if (auto paintable = Node::first_paintable())
|
|
|
|
|
return static_cast<Painting::PaintableBox const&>(*paintable);
|
|
|
|
|
return nullptr;
|
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
|
|
|
{
|
2026-01-11 03:31:07 -03:00
|
|
|
auto const& computed_aspect_ratio = computed_values().aspect_ratio();
|
|
|
|
|
|
|
|
|
|
// https://www.w3.org/TR/css-contain-2/#containment-size
|
|
|
|
|
if (!has_size_containment() && computed_aspect_ratio.use_natural_aspect_ratio_if_available) {
|
|
|
|
|
if (auto auto_size = auto_content_box_size(); auto_size.has_aspect_ratio())
|
|
|
|
|
return auto_size.aspect_ratio;
|
|
|
|
|
}
|
2024-03-23 12:46:08 -03:00
|
|
|
|
|
|
|
|
if (!computed_aspect_ratio.preferred_ratio.has_value())
|
|
|
|
|
return {};
|
|
|
|
|
|
2026-01-11 03:31:07 -03:00
|
|
|
auto ratio = computed_aspect_ratio.preferred_ratio.value();
|
2024-03-23 12:46:08 -03:00
|
|
|
if (ratio.is_degenerate())
|
|
|
|
|
return {};
|
|
|
|
|
|
2025-12-29 23:53:52 -03:00
|
|
|
auto fraction = CSSPixelFraction(ratio.numerator(), ratio.denominator());
|
|
|
|
|
// ratio.is_degenerate() operates on doubles while CSSPixelFraction uses CSSPixels, so we need to check again here.
|
|
|
|
|
if (fraction == 0)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return fraction;
|
2023-06-08 12:47:50 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|