mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
LibWeb: Relax restrictions on LayoutState::get() input type
Instead of only letting NodeWithStyleAndBoxModelMetrics (and subclasses) have used values in the layout state, we now allow any NodeWithStyle.
This commit is contained in:
parent
9577cd853a
commit
1434721247
Notes:
sideshowbarker
2024-07-17 03:45:48 +09:00
Author: https://github.com/awesomekling
Commit: 1434721247
Pull-request: https://github.com/SerenityOS/serenity/pull/20947
2 changed files with 32 additions and 29 deletions
|
@ -23,45 +23,45 @@ LayoutState::~LayoutState()
|
|||
{
|
||||
}
|
||||
|
||||
LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
|
||||
LayoutState::UsedValues& LayoutState::get_mutable(NodeWithStyle const& node)
|
||||
{
|
||||
if (auto* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
|
||||
if (auto* used_values = used_values_per_layout_node.get(&node).value_or(nullptr))
|
||||
return *used_values;
|
||||
|
||||
for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
|
||||
if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr)) {
|
||||
if (auto* ancestor_used_values = ancestor->used_values_per_layout_node.get(&node).value_or(nullptr)) {
|
||||
auto cow_used_values = adopt_own(*new UsedValues(*ancestor_used_values));
|
||||
auto* cow_used_values_ptr = cow_used_values.ptr();
|
||||
used_values_per_layout_node.set(&box, move(cow_used_values));
|
||||
used_values_per_layout_node.set(&node, move(cow_used_values));
|
||||
return *cow_used_values_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
|
||||
auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
|
||||
|
||||
auto new_used_values = adopt_own(*new UsedValues);
|
||||
auto* new_used_values_ptr = new_used_values.ptr();
|
||||
new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
|
||||
used_values_per_layout_node.set(&box, move(new_used_values));
|
||||
new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
|
||||
used_values_per_layout_node.set(&node, move(new_used_values));
|
||||
return *new_used_values_ptr;
|
||||
}
|
||||
|
||||
LayoutState::UsedValues const& LayoutState::get(NodeWithStyleAndBoxModelMetrics const& box) const
|
||||
LayoutState::UsedValues const& LayoutState::get(NodeWithStyle const& node) const
|
||||
{
|
||||
if (auto const* used_values = used_values_per_layout_node.get(&box).value_or(nullptr))
|
||||
if (auto const* used_values = used_values_per_layout_node.get(&node).value_or(nullptr))
|
||||
return *used_values;
|
||||
|
||||
for (auto const* ancestor = m_parent; ancestor; ancestor = ancestor->m_parent) {
|
||||
if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&box).value_or(nullptr))
|
||||
if (auto const* ancestor_used_values = ancestor->used_values_per_layout_node.get(&node).value_or(nullptr))
|
||||
return *ancestor_used_values;
|
||||
}
|
||||
|
||||
auto const* containing_block_used_values = box.is_viewport() ? nullptr : &get(*box.containing_block());
|
||||
auto const* containing_block_used_values = node.is_viewport() ? nullptr : &get(*node.containing_block());
|
||||
|
||||
auto new_used_values = adopt_own(*new UsedValues);
|
||||
auto* new_used_values_ptr = new_used_values.ptr();
|
||||
new_used_values->set_node(const_cast<NodeWithStyleAndBoxModelMetrics&>(box), containing_block_used_values);
|
||||
const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&box, move(new_used_values));
|
||||
new_used_values->set_node(const_cast<NodeWithStyle&>(node), containing_block_used_values);
|
||||
const_cast<LayoutState*>(this)->used_values_per_layout_node.set(&node, move(new_used_values));
|
||||
return *new_used_values_ptr;
|
||||
}
|
||||
|
||||
|
@ -139,7 +139,7 @@ void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines
|
|||
// Regular boxes (not line box fragments):
|
||||
for (auto& it : used_values_per_layout_node) {
|
||||
auto& used_values = *it.value;
|
||||
auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
|
||||
auto& node = const_cast<NodeWithStyle&>(used_values.node());
|
||||
|
||||
if (!node.is_box())
|
||||
continue;
|
||||
|
@ -163,8 +163,8 @@ void LayoutState::resolve_relative_positions(Vector<Painting::PaintableWithLines
|
|||
offset = used_values.offset;
|
||||
}
|
||||
// Apply relative position inset if appropriate.
|
||||
if (node.computed_values().position() == CSS::Position::Relative) {
|
||||
auto& inset = node.box_model().inset;
|
||||
if (node.computed_values().position() == CSS::Position::Relative && is<NodeWithStyleAndBoxModelMetrics>(node)) {
|
||||
auto& inset = static_cast<NodeWithStyleAndBoxModelMetrics const&>(node).box_model().inset;
|
||||
offset.translate_by(inset.left, inset.top);
|
||||
}
|
||||
paintable.set_offset(offset);
|
||||
|
@ -232,13 +232,16 @@ void LayoutState::commit(Box& root)
|
|||
|
||||
for (auto& it : used_values_per_layout_node) {
|
||||
auto& used_values = *it.value;
|
||||
auto& node = const_cast<NodeWithStyleAndBoxModelMetrics&>(used_values.node());
|
||||
auto& node = const_cast<NodeWithStyle&>(used_values.node());
|
||||
|
||||
// Transfer box model metrics.
|
||||
node.box_model().inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
|
||||
node.box_model().padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
|
||||
node.box_model().border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
|
||||
node.box_model().margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
|
||||
if (is<NodeWithStyleAndBoxModelMetrics>(node)) {
|
||||
// Transfer box model metrics.
|
||||
auto& box_model = static_cast<NodeWithStyleAndBoxModelMetrics&>(node).box_model();
|
||||
box_model.inset = { used_values.inset_top, used_values.inset_right, used_values.inset_bottom, used_values.inset_left };
|
||||
box_model.padding = { used_values.padding_top, used_values.padding_right, used_values.padding_bottom, used_values.padding_left };
|
||||
box_model.border = { used_values.border_top, used_values.border_right, used_values.border_bottom, used_values.border_left };
|
||||
box_model.margin = { used_values.margin_top, used_values.margin_right, used_values.margin_bottom, used_values.margin_left };
|
||||
}
|
||||
|
||||
auto paintable = node.create_paintable();
|
||||
|
||||
|
@ -296,7 +299,7 @@ void LayoutState::commit(Box& root)
|
|||
}
|
||||
}
|
||||
|
||||
void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, UsedValues const* containing_block_used_values)
|
||||
void LayoutState::UsedValues::set_node(NodeWithStyle& node, UsedValues const* containing_block_used_values)
|
||||
{
|
||||
m_node = &node;
|
||||
|
||||
|
|
|
@ -41,8 +41,8 @@ struct LayoutState {
|
|||
}
|
||||
|
||||
struct UsedValues {
|
||||
NodeWithStyleAndBoxModelMetrics const& node() const { return *m_node; }
|
||||
void set_node(NodeWithStyleAndBoxModelMetrics&, UsedValues const* containing_block_used_values);
|
||||
NodeWithStyle const& node() const { return *m_node; }
|
||||
void set_node(NodeWithStyle&, UsedValues const* containing_block_used_values);
|
||||
|
||||
CSSPixels content_width() const { return m_content_width; }
|
||||
CSSPixels content_height() const { return m_content_height; }
|
||||
|
@ -134,7 +134,7 @@ struct LayoutState {
|
|||
CSSPixels border_top_collapsed() const { return use_collapsing_borders_model() ? round(border_top / 2) : border_top; }
|
||||
CSSPixels border_bottom_collapsed() const { return use_collapsing_borders_model() ? round(border_bottom / 2) : border_bottom; }
|
||||
|
||||
JS::GCPtr<Layout::NodeWithStyleAndBoxModelMetrics> m_node { nullptr };
|
||||
JS::GCPtr<Layout::NodeWithStyle> m_node { nullptr };
|
||||
|
||||
CSSPixels m_content_width { 0 };
|
||||
CSSPixels m_content_height { 0 };
|
||||
|
@ -152,10 +152,10 @@ struct LayoutState {
|
|||
void commit(Box& root);
|
||||
|
||||
// NOTE: get_mutable() will CoW the UsedValues if it's inherited from an ancestor state;
|
||||
UsedValues& get_mutable(NodeWithStyleAndBoxModelMetrics const&);
|
||||
UsedValues& get_mutable(NodeWithStyle const&);
|
||||
|
||||
// NOTE: get() will not CoW the UsedValues.
|
||||
UsedValues const& get(NodeWithStyleAndBoxModelMetrics const&) const;
|
||||
UsedValues const& get(NodeWithStyle const&) const;
|
||||
|
||||
HashMap<Layout::Node const*, NonnullOwnPtr<UsedValues>> used_values_per_layout_node;
|
||||
|
||||
|
@ -169,7 +169,7 @@ struct LayoutState {
|
|||
HashMap<CSSPixels, Optional<CSSPixels>> max_content_height;
|
||||
};
|
||||
|
||||
HashMap<JS::GCPtr<NodeWithStyleAndBoxModelMetrics const>, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
|
||||
HashMap<JS::GCPtr<NodeWithStyle const>, NonnullOwnPtr<IntrinsicSizes>> mutable intrinsic_sizes;
|
||||
|
||||
LayoutState const* m_parent { nullptr };
|
||||
LayoutState const& m_root;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue