Truly independent web browser
LibWeb: Preserve precision in resolved block margins
Andreas Kling6224eb92e472
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cppindex 87ac468a850..1a52bbc1c2e 100644--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp@@ -1197,6 +1197,42 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(Lreturn {};};+ // https://drafts.csswg.org/cssom/#resolved-value-special-case-property-like-height+ // If the property applies to the element or pseudo-element and the resolved value of the display property is not+ // none or contents, then the resolved value is the used value. Otherwise the resolved value is the computed value.+ auto resolved_value_for_margin = [&layout_node, &used_value_for_property](LengthPercentageOrAuto const& margin, Function<CSSPixels(Layout::Node const&)>&& used_value_getter) -> NonnullRefPtr<StyleValue const> {+ auto computed_value = [&] { return style_value_for_length_percentage_or_auto(margin); };+ auto maybe_used_value = used_value_for_property(move(used_value_getter));+ if (!maybe_used_value.has_value())+ return computed_value();+ auto const* containing_block = layout_node.containing_block();+ // NB: Positioned boxes can have position-try overrides, and grid/flex items can use a specialized basis.+ if (!layout_node.display().is_block_outside() || layout_node.is_absolutely_positioned()+ || (containing_block && (containing_block->display().is_grid_inside() || containing_block->display().is_flex_inside())))+ return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));+ auto resolved_length = [](double pixels) -> NonnullRefPtr<StyleValue const> {+ // NB: Retain fractional precision within the layout range, and preserve saturation outside it.+ if (pixels < CSSPixels::min_integer_value || pixels > CSSPixels::max_integer_value)+ pixels = CSSPixels::nearest_value_for(pixels).to_double();+ return LengthStyleValue::create(Length::make_px(pixels));+ };+ // INTEROP: Chrome and Safari exclude the adjustment that satisfies an over-constrained block's inline-size equation.+ if (margin.is_length())+ return resolved_length(margin.length().absolute_length_to_px_without_rounding());+ if (!margin.is_auto()) {+ if (containing_block && Painting::has_committed_box(*containing_block)) {+ auto size = Painting::content_size(*containing_block);+ auto basis = containing_block->writing_mode() == WritingMode::HorizontalTb ? size.width() : size.height();+ auto const& length_percentage = margin.length_percentage();+ if (length_percentage.is_percentage())+ return resolved_length(basis.to_double() * length_percentage.percentage().as_fraction());+ if (auto resolved = length_percentage.calculated()->resolve_length({ .percentage_basis = Length::make_px(basis) }); resolved.has_value())+ return resolved_length(resolved->absolute_length_to_px_without_rounding());+ }+ }+ return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));+ };+auto used_size_for_property = [&layout_node, &used_value_for_property]<typename ContentBoxGetter, typename BorderBoxGetter>(ContentBoxGetter content_box_getter, BorderBoxGetter border_box_getter) -> Optional<CSSPixels> {return used_value_for_property([&layout_node, content_box_getter, border_box_getter](Layout::Node const& box_layout_node) {if (layout_node.box_sizing() == BoxSizing::BorderBox)@@ -1397,21 +1433,13 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(Lreturn get_computed_value(property_id);}case PropertyID::MarginBottom:- if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.bottom; }); maybe_used_value.has_value())- return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));- return style_value_for_length_percentage_or_auto(layout_node.margin().bottom());+ return resolved_value_for_margin(layout_node.margin().bottom(), [](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.bottom; });case PropertyID::MarginLeft:- if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.left; }); maybe_used_value.has_value())- return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));- return style_value_for_length_percentage_or_auto(layout_node.margin().left());+ return resolved_value_for_margin(layout_node.margin().left(), [](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.left; });case PropertyID::MarginRight:- if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.right; }); maybe_used_value.has_value())- return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));- return style_value_for_length_percentage_or_auto(layout_node.margin().right());+ return resolved_value_for_margin(layout_node.margin().right(), [](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.right; });case PropertyID::MarginTop:- if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.top; }); maybe_used_value.has_value())- return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));- return style_value_for_length_percentage_or_auto(layout_node.margin().top());+ return resolved_value_for_margin(layout_node.margin().top(), [](auto const& box_layout_node) { return Painting::box_model(box_layout_node).margin.top; });case PropertyID::PaddingBottom:if (auto maybe_used_value = used_value_for_property([](auto const& box_layout_node) { return Painting::box_model(box_layout_node).padding.bottom; }); maybe_used_value.has_value())return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));diff --git a/Tests/LibWeb/Text/expected/css/calc-coverage.txt b/Tests/LibWeb/Text/expected/css/calc-coverage.txtindex e5a0bd782d5..135fd3363b8 100644--- a/Tests/LibWeb/Text/expected/css/calc-coverage.txt+++ b/Tests/LibWeb/Text/expected/css/calc-coverage.txt@@ -94,12 +94,12 @@ line-height: 'calc(2)' -> '32px'line-height: 'calc(2 * var(--n))' -> '64px'margin-bottom: 'calc(2px)' -> '2px'margin-bottom: 'calc(2px * var(--n))' -> '4px'-margin-left: 'calc(2%)' -> '15.671875px'-margin-left: 'calc(2% * var(--n))' -> '31.359375px'+margin-left: 'calc(2%)' -> '15.68px'+margin-left: 'calc(2% * var(--n))' -> '31.36px'margin-right: 'calc(2px)' -> '2px'margin-right: 'calc(2px * var(--n))' -> '4px'-margin-top: 'calc(2%)' -> '15.671875px'-margin-top: 'calc(2% * var(--n))' -> '31.359375px'+margin-top: 'calc(2%)' -> '15.68px'+margin-top: 'calc(2% * var(--n))' -> '31.36px'math-depth: 'calc(2)' -> '2'math-depth: 'calc(2 * var(--n))' -> '4'max-height: 'calc(2px)' -> '2px'diff --git a/Tests/LibWeb/Text/expected/css/em-length-computed-value-precision.txt b/Tests/LibWeb/Text/expected/css/em-length-computed-value-precision.txtindex abae7561c13..0d8d2b45735 100644--- a/Tests/LibWeb/Text/expected/css/em-length-computed-value-precision.txt+++ b/Tests/LibWeb/Text/expected/css/em-length-computed-value-precision.txt@@ -1,4 +1,4 @@-margin-left: 1.59375px+margin-left: 1.6pxpadding-left: 4.796875pxoutline-offset: 1.6pxletter-spacing: 1.6pxdiff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-box/parsing/margin-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-box/parsing/margin-computed.txtindex 8ab457ae646..90cadfc1120 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-box/parsing/margin-computed.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-box/parsing/margin-computed.txt@@ -2,13 +2,12 @@ Harness status: OKFound 8 tests-3 Pass-5 Fail-Fail Property margin value '10px'-Fail Property margin value '10px 20px 30px 40px'-Fail Property margin value 'calc(0.5em + 10px)'-Fail Property margin value '30%'+8 Pass+Pass Property margin value '10px'+Pass Property margin value '10px 20px 30px 40px'+Pass Property margin value 'calc(0.5em + 10px)'+Pass Property margin value '30%'Pass Property margin-top value '10px'-Fail Property margin-right value '20px'+Pass Property margin-right value '20px'Pass Property margin-bottom value '30px'Pass Property margin-left value '40px'\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-logical/inheritance.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-logical/inheritance.txtindex 7e6cf6bdc61..5c9bd3d651e 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-logical/inheritance.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-logical/inheritance.txt@@ -2,8 +2,8 @@ Harness status: OKFound 68 tests-65 Pass-3 Fail+66 Pass+2 FailPass Property block-size has initial value 0pxPass Property block-size does not inheritPass Property border-block-end-color has initial value rgb(0, 0, 255)@@ -53,7 +53,7 @@ Pass Property margin-block-end does not inheritPass Property margin-block-start has initial value 0pxPass Property margin-block-start does not inheritPass Property margin-inline-end has initial value 0px-Fail Property margin-inline-end does not inherit+Pass Property margin-inline-end does not inheritPass Property margin-inline-start has initial value 0pxPass Property margin-inline-start does not inheritPass Property max-block-size has initial value nonediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/cssom/getComputedStyle-margins-roundtrip.txt b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/getComputedStyle-margins-roundtrip.txtindex 72ebf7c5add..1811ff773d0 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/cssom/getComputedStyle-margins-roundtrip.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/getComputedStyle-margins-roundtrip.txt@@ -2,8 +2,8 @@ Harness status: OKFound 4 tests-4 Fail-Fail Fixed-length margin-left property value should roundtrip-Fail Fixed-length margin-right property value should roundtrip-Fail Fixed-length margin-top property value should roundtrip-Fail Fixed-length margin-bottom property value should roundtrip\ No newline at end of file+4 Pass+Pass Fixed-length margin-left property value should roundtrip+Pass Fixed-length margin-right property value should roundtrip+Pass Fixed-length margin-top property value should roundtrip+Pass Fixed-length margin-bottom property value should roundtrip\ No newline at end of file