Truly independent web browser
LibWeb: Resolve min-width: auto to zero for elements without boxes
Andreas Kling7115cdd72d6a
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cppindex 71361d41e1d..ce5b63b0968 100644--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp@@ -689,6 +689,26 @@ static NonnullRefPtr<StyleValue const> resolve_unresolvable_anchor_functions(Proreturn computed_value;}+// NB: An element without a layout node may still generate a box whose layout is not up to date yet, so this only+// looks at whether the element's style places it outside the box tree.+static bool element_generates_no_box(Layout::BegunRead const& read, DOM::AbstractElement abstract_element, ComputedValues const& style)+{+ if (style.display().is_none() || style.display().is_contents())+ return true;+ // NB: content: none keeps ::before, ::after and ::marker out of the box tree, as normal (which computes to none+ // for them) does for ::before and ::after.+ if (auto pseudo_element = abstract_element.pseudo_element(); pseudo_element.has_value()) {+ bool is_before_or_after = first_is_one_of(*pseudo_element, PseudoElement::Before, PseudoElement::After);+ if (is_before_or_after && style.content_is_normal())+ return true;+ auto content = style.computed_content();+ if ((is_before_or_after || *pseudo_element == PseudoElement::Marker) && content->is_keyword() && content->to_keyword() == Keyword::None)+ return true;+ }+ auto style_record = abstract_element.style_record_identity();+ return !!style_record && has_flag(abstract_element.document().style_computer().style_engine().style_record_dependency_flags(read, style_record), StyleRecordDependencyFlag::InDisplayNoneSubtree);+}+// Brings style (and, when the property needs it, layout) up to date for computed-style property// access, and returns the layout node to read used values from (may be null). An empty Optional// means the element cannot expose computed style at all (disconnected, or no browsing context).@@ -910,10 +930,17 @@ Optional<StyleProperty> CSSStyleProperties::get_direct_property(PropertyNameAndIreturn resolve_touch_action_style_value(move(computed_value));case PropertyID::TransformOrigin:return style_value_for_transform_origin(computed_values->transform_origin(), {});- case PropertyID::MaxHeight:- case PropertyID::MaxWidth:case PropertyID::MinHeight:case PropertyID::MinWidth:+ // https://drafts.csswg.org/css-sizing-3/#valdef-width-auto+ // For backwards-compatibility, the resolved value of this keyword is zero for boxes of all [CSS2]+ // display types (block and inline boxes, inline blocks, and all table display types) when+ // 'aspect-ratio' is 'auto'. It also resolves to zero when no box is generated.+ if (computed_value->to_keyword() == Keyword::Auto && element_generates_no_box(read, abstract_element, *computed_values))+ return LengthStyleValue::create(Length::make_px(0));+ return resolve_unresolvable_anchor_functions(computed_property_id, move(computed_value));+ case PropertyID::MaxHeight:+ case PropertyID::MaxWidth:return resolve_unresolvable_anchor_functions(computed_property_id, move(computed_value));default:return computed_value;diff --git a/Tests/LibWeb/Text/expected/css/getComputedStyle-min-size-auto-pseudo-element-without-box.txt b/Tests/LibWeb/Text/expected/css/getComputedStyle-min-size-auto-pseudo-element-without-box.txtnew file mode 100644index 00000000000..5070aee3194--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/getComputedStyle-min-size-auto-pseudo-element-without-box.txt@@ -0,0 +1,5 @@+none::before: min-width=0px min-height=0px+normal::before: min-width=0px min-height=0px+after-without-content::after: min-width=0px min-height=0px+flex::before: min-width=0px min-height=0px+marker::marker: min-width=0px min-height=0pxdiff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.txtindex ac4ac6aea00..112ef6cada2 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-flexbox/getcomputedstyle/flexbox_computedstyle_min-auto-size.txt@@ -2,9 +2,8 @@ Harness status: OKFound 4 tests-2 Pass-2 Fail+4 PassPass Computed min-width/min-height of specified auto for flex item.-Fail Computed min-width/min-height of specified auto inside display:none which would otherwise have been a flex item.+Pass Computed min-width/min-height of specified auto inside display:none which would otherwise have been a flex item.Pass Computed min-width/min-height of specified auto for flex item inside display:contents.-Fail Computed min-width/min-height of specified auto with display:none which would otherwise have been a flex item.\ No newline at end of file+Pass Computed min-width/min-height of specified auto with display:none which would otherwise have been a flex item.\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-item-min-auto-size-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-item-min-auto-size-001.txtindex 2cbbf00f1be..f4577f66a47 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-item-min-auto-size-001.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-grid/grid-items/grid-item-min-auto-size-001.txt@@ -2,9 +2,8 @@ Harness status: OKFound 4 tests-2 Pass-2 Fail+4 PassPass Computed min-width/min-height of specified auto for grid item.-Fail Computed min-width/min-height of specified auto inside display:none which would otherwise have been a grid item.+Pass Computed min-width/min-height of specified auto inside display:none which would otherwise have been a grid item.Pass Computed min-width/min-height of specified auto for grid item inside display:contents.-Fail Computed min-width/min-height of specified auto with display:none which would otherwise have been a grid item.\ No newline at end of file+Pass Computed min-width/min-height of specified auto with display:none which would otherwise have been a grid item.\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/css/getComputedStyle-min-size-auto-pseudo-element-without-box.html b/Tests/LibWeb/Text/input/css/getComputedStyle-min-size-auto-pseudo-element-without-box.htmlnew file mode 100644index 00000000000..d0c11822487--- /dev/null+++ b/Tests/LibWeb/Text/input/css/getComputedStyle-min-size-auto-pseudo-element-without-box.html@@ -0,0 +1,28 @@+<!DOCTYPE html>+<style>+ .min-auto::before, .min-auto::after, .min-auto::marker {+ display: block;+ min-width: auto;+ min-height: auto;+ }+ #none::before { content: none; }+ #normal::before { content: normal; }+ #after-without-content::after { }+ #flex { display: flex; }+ #flex::before { content: none; }+ #marker::marker { content: none; }+</style>+<script src="../include.js"></script>+<div class="min-auto" id="none"></div>+<div class="min-auto" id="normal"></div>+<div class="min-auto" id="after-without-content"></div>+<div class="min-auto" id="flex"></div>+<ul><li class="min-auto" id="marker">item</li></ul>+<script>+ test(() => {+ for (const [id, pseudo] of [["none", "::before"], ["normal", "::before"], ["after-without-content", "::after"], ["flex", "::before"], ["marker", "::marker"]]) {+ const style = getComputedStyle(document.getElementById(id), pseudo);+ println(`${id}${pseudo}: min-width=${style.minWidth} min-height=${style.minHeight}`);+ }+ });+</script>