mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 03:07:35 +00:00
LibWeb: Add Length::resolved()
overload for CSSPixels
Since we always pass the px value as an argument to resolved(), we can pass it directly as CSSPixels instead of wrapping it in Length. This approach allows us to avoid converting to a double, resulting in fewer precision issues.
This commit is contained in:
parent
5eb0f65cc0
commit
0fb571c1c2
14 changed files with 154 additions and 75 deletions
|
@ -164,14 +164,13 @@ void BlockFormattingContext::compute_width(Box const& box, AvailableSpace const&
|
|||
auto const& computed_values = box.computed_values();
|
||||
|
||||
auto width_of_containing_block = remaining_available_space.width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length_for_resolve = remaining_available_space.width.is_definite() ? CSS::Length::make_px(width_of_containing_block) : CSS::Length::make_px(0);
|
||||
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
|
||||
auto margin_left = CSS::Length::make_auto();
|
||||
auto margin_right = CSS::Length::make_auto();
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block).to_px(box);
|
||||
|
||||
auto& box_state = m_state.get_mutable(box);
|
||||
box_state.border_left = computed_values.border_left().width;
|
||||
|
@ -187,8 +186,8 @@ void BlockFormattingContext::compute_width(Box const& box, AvailableSpace const&
|
|||
|
||||
auto try_compute_width = [&](auto const& a_width) {
|
||||
CSS::Length width = a_width;
|
||||
margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
margin_left = computed_values.margin().left().resolved(box, width_of_containing_block);
|
||||
margin_right = computed_values.margin().right().resolved(box, width_of_containing_block);
|
||||
CSSPixels total_px = computed_values.border_left().width + computed_values.border_right().width;
|
||||
for (auto& value : { margin_left, CSS::Length::make_px(padding_left), width, CSS::Length::make_px(padding_right), margin_right }) {
|
||||
total_px += value.to_px(box);
|
||||
|
@ -290,12 +289,12 @@ void BlockFormattingContext::compute_width_for_floating_box(Box const& box, Avai
|
|||
auto& computed_values = box.computed_values();
|
||||
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(available_space.width.to_px_or_zero());
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block).to_px(box);
|
||||
|
||||
// If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
|
||||
if (margin_left.is_auto())
|
||||
|
@ -371,17 +370,14 @@ void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_n
|
|||
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(width_of_containing_block);
|
||||
if (!available_space.width.is_definite())
|
||||
width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(0);
|
||||
|
||||
// 10.3.4 Block-level, replaced elements in normal flow
|
||||
// The used value of 'width' is determined as for inline replaced elements. Then the rules for
|
||||
// non-replaced block-level elements are applied to determine the margins.
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve).to_px(box);
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block).to_px(box);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block).to_px(box);
|
||||
|
||||
// If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
|
||||
if (margin_left.is_auto())
|
||||
|
@ -407,12 +403,11 @@ CSSPixels BlockFormattingContext::compute_table_box_width_inside_table_wrapper(B
|
|||
auto const& computed_values = box.computed_values();
|
||||
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length_for_resolve = available_space.width.is_definite() ? CSS::Length::make_px(width_of_containing_block) : CSS::Length::make_px(0);
|
||||
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto margin_left = computed_values.margin().left().resolved(box, width_of_containing_block);
|
||||
auto margin_right = computed_values.margin().right().resolved(box, width_of_containing_block);
|
||||
|
||||
// If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
|
||||
if (margin_left.is_auto())
|
||||
|
|
|
@ -387,7 +387,6 @@ CSSPixels FormattingContext::tentative_width_for_replaced_element(Box const& box
|
|||
if (computed_width.is_percentage() && !m_state.get(*box.containing_block()).has_definite_width())
|
||||
return 0;
|
||||
|
||||
auto height_of_containing_block = CSS::Length::make_px(containing_block_height_for(box));
|
||||
auto computed_height = should_treat_height_as_auto(box, available_space) ? CSS::Size::make_auto() : box.computed_values().height();
|
||||
|
||||
CSSPixels used_width = calculate_inner_width(box, available_space.width, computed_width).to_px(box);
|
||||
|
@ -451,7 +450,6 @@ CSSPixels FormattingContext::compute_width_for_replaced_element(Box const& box,
|
|||
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
|
||||
|
||||
auto computed_width = should_treat_width_as_auto(box, available_space) ? CSS::Size::make_auto() : box.computed_values().width();
|
||||
auto computed_height = should_treat_height_as_auto(box, available_space) ? CSS::Size::make_auto() : box.computed_values().height();
|
||||
|
@ -565,7 +563,6 @@ CSSPixels FormattingContext::compute_height_for_replaced_element(Box const& box,
|
|||
void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box const& box, AvailableSpace const& available_space)
|
||||
{
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
|
||||
auto& computed_values = box.computed_values();
|
||||
auto zero_value = CSS::Length::make_px(0);
|
||||
|
||||
|
@ -582,8 +579,8 @@ void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_ele
|
|||
auto right = computed_values.inset().right().to_px(box, width_of_containing_block);
|
||||
|
||||
auto try_compute_width = [&](auto const& a_width) {
|
||||
margin_left = computed_values.margin().left().resolved(box, width_of_containing_block_as_length);
|
||||
margin_right = computed_values.margin().right().resolved(box, width_of_containing_block_as_length);
|
||||
margin_left = computed_values.margin().left().resolved(box, width_of_containing_block);
|
||||
margin_right = computed_values.margin().right().resolved(box, width_of_containing_block);
|
||||
|
||||
auto width = a_width;
|
||||
|
||||
|
@ -833,9 +830,7 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el
|
|||
auto height = box.computed_values().height();
|
||||
|
||||
auto width_of_containing_block = containing_block_width_for(box);
|
||||
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
|
||||
auto height_of_containing_block = available_space.height.to_px_or_zero();
|
||||
auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
|
||||
|
||||
enum class ClampToZero {
|
||||
No,
|
||||
|
@ -859,23 +854,23 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el
|
|||
};
|
||||
|
||||
auto solve_for_top = [&] {
|
||||
top = solve_for(top.resolved(box, height_of_containing_block_as_length));
|
||||
top = solve_for(top.resolved(box, height_of_containing_block));
|
||||
};
|
||||
|
||||
auto solve_for_bottom = [&] {
|
||||
bottom = solve_for(bottom.resolved(box, height_of_containing_block_as_length));
|
||||
bottom = solve_for(bottom.resolved(box, height_of_containing_block));
|
||||
};
|
||||
|
||||
auto solve_for_height = [&] {
|
||||
height = CSS::Size::make_length(solve_for(height.resolved(box, height_of_containing_block_as_length), ClampToZero::Yes));
|
||||
height = CSS::Size::make_length(solve_for(height.resolved(box, height_of_containing_block), ClampToZero::Yes));
|
||||
};
|
||||
|
||||
auto solve_for_margin_top = [&] {
|
||||
margin_top = solve_for(margin_top.resolved(box, width_of_containing_block_as_length));
|
||||
margin_top = solve_for(margin_top.resolved(box, width_of_containing_block));
|
||||
};
|
||||
|
||||
auto solve_for_margin_bottom = [&] {
|
||||
margin_bottom = solve_for(margin_bottom.resolved(box, width_of_containing_block_as_length));
|
||||
margin_bottom = solve_for(margin_bottom.resolved(box, width_of_containing_block));
|
||||
};
|
||||
|
||||
auto solve_for_margin_top_and_margin_bottom = [&] {
|
||||
|
@ -1093,11 +1088,6 @@ void FormattingContext::layout_absolutely_positioned_element(Box const& box, Ava
|
|||
auto& containing_block_state = m_state.get_mutable(*box.containing_block());
|
||||
auto& box_state = m_state.get_mutable(box);
|
||||
|
||||
auto width_of_containing_block = available_space.width.to_px_or_zero();
|
||||
auto height_of_containing_block = available_space.height.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
|
||||
auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
|
||||
|
||||
// The border computed values are not changed by the compute_height & width calculations below.
|
||||
// The spec only adjusts and computes sizes, insets and margins.
|
||||
box_state.border_left = box.computed_values().border_left().width;
|
||||
|
@ -1449,9 +1439,8 @@ CSSPixels FormattingContext::calculate_max_content_height(Layout::Box const& box
|
|||
CSS::Length FormattingContext::calculate_inner_width(Layout::Box const& box, AvailableSize const& available_width, CSS::Size const& width) const
|
||||
{
|
||||
auto width_of_containing_block = available_width.to_px_or_zero();
|
||||
auto width_of_containing_block_as_length_for_resolve = CSS::Length::make_px(width_of_containing_block);
|
||||
if (width.is_auto()) {
|
||||
return width.resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
return width.resolved(box, width_of_containing_block);
|
||||
}
|
||||
if (width.is_fit_content()) {
|
||||
return CSS::Length::make_px(calculate_fit_content_width(box, AvailableSpace { available_width, AvailableSize::make_indefinite() }));
|
||||
|
@ -1465,8 +1454,8 @@ CSS::Length FormattingContext::calculate_inner_width(Layout::Box const& box, Ava
|
|||
|
||||
auto& computed_values = box.computed_values();
|
||||
if (computed_values.box_sizing() == CSS::BoxSizing::BorderBox) {
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
auto const padding_left = computed_values.padding().left().resolved(box, width_of_containing_block);
|
||||
auto const padding_right = computed_values.padding().right().resolved(box, width_of_containing_block);
|
||||
|
||||
auto inner_width = width.to_px(box, width_of_containing_block)
|
||||
- computed_values.border_left().width
|
||||
|
@ -1476,7 +1465,7 @@ CSS::Length FormattingContext::calculate_inner_width(Layout::Box const& box, Ava
|
|||
return CSS::Length::make_px(max(inner_width, 0));
|
||||
}
|
||||
|
||||
return width.resolved(box, width_of_containing_block_as_length_for_resolve);
|
||||
return width.resolved(box, width_of_containing_block);
|
||||
}
|
||||
|
||||
CSS::Length FormattingContext::calculate_inner_height(Layout::Box const& box, AvailableSize const&, CSS::Size const& height) const
|
||||
|
@ -1489,14 +1478,13 @@ CSS::Length FormattingContext::calculate_inner_height(Layout::Box const& box, Av
|
|||
// If the box has position: absolute, then the containing block is formed by the padding edge of the ancestor
|
||||
height_of_containing_block += containing_block_state.padding_top + containing_block_state.padding_bottom;
|
||||
}
|
||||
auto height_of_containing_block_as_length_for_resolve = CSS::Length::make_px(height_of_containing_block);
|
||||
if (height.is_auto()) {
|
||||
return height.resolved(box, height_of_containing_block_as_length_for_resolve);
|
||||
return height.resolved(box, height_of_containing_block);
|
||||
}
|
||||
|
||||
auto& computed_values = box.computed_values();
|
||||
if (computed_values.box_sizing() == CSS::BoxSizing::BorderBox) {
|
||||
auto width_of_containing_block = CSS::Length::make_px(containing_block_width_for(box));
|
||||
auto width_of_containing_block = containing_block_width_for(box);
|
||||
|
||||
auto const padding_top = computed_values.padding().top().resolved(box, width_of_containing_block);
|
||||
auto const padding_bottom = computed_values.padding().bottom().resolved(box, width_of_containing_block);
|
||||
|
@ -1509,7 +1497,7 @@ CSS::Length FormattingContext::calculate_inner_height(Layout::Box const& box, Av
|
|||
return CSS::Length::make_px(max(inner_height, 0));
|
||||
}
|
||||
|
||||
return height.resolved(box, height_of_containing_block_as_length_for_resolve);
|
||||
return height.resolved(box, height_of_containing_block);
|
||||
}
|
||||
|
||||
CSSPixels FormattingContext::containing_block_width_for(NodeWithStyleAndBoxModelMetrics const& node) const
|
||||
|
|
|
@ -373,9 +373,7 @@ void LayoutState::UsedValues::set_node(NodeWithStyleAndBoxModelMetrics& node, Us
|
|||
if (size.calculated().contains_percentage()) {
|
||||
if (!containing_block_has_definite_size)
|
||||
return false;
|
||||
auto containing_block_size_as_length = width
|
||||
? CSS::Length::make_px(containing_block_used_values->content_width())
|
||||
: CSS::Length::make_px(containing_block_used_values->content_height());
|
||||
auto containing_block_size_as_length = width ? containing_block_used_values->content_width() : containing_block_used_values->content_height();
|
||||
resolved_definite_size = adjust_for_box_sizing(size.calculated().resolve_length_percentage(node, containing_block_size_as_length).value_or(CSS::Length::make_auto()).to_px(node), size, width);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -842,9 +842,7 @@ void TableFormattingContext::compute_table_height(LayoutMode layout_mode)
|
|||
span_width += m_columns[cell.column_index + i].used_width;
|
||||
|
||||
auto width_of_containing_block = m_state.get(*cell.box->containing_block()).content_width();
|
||||
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);
|
||||
auto height_of_containing_block = m_state.get(*cell.box->containing_block()).content_height();
|
||||
auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block);
|
||||
|
||||
cell_state.padding_top = cell.box->computed_values().padding().top().to_px(cell.box, width_of_containing_block);
|
||||
cell_state.padding_bottom = cell.box->computed_values().padding().bottom().to_px(cell.box, width_of_containing_block);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue