Fix sizing of replaced elements with min/max sizes (#32777)

If a (min/max)-(height/width) property is set, we still need to respect
the intrinsic ratio of the element if it exists. The previous code was
simply clamping the element size after doing the sizing calculations
once, but this leads to an incorrect aspect ratio.

Signed-off-by: valadaptive <valadaptive@protonmail.com>
This commit is contained in:
valadaptive 2024-07-18 03:07:13 -04:00 committed by GitHub
parent 1b1f79305e
commit f6c9714286
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 232 additions and 200 deletions

View file

@ -17,6 +17,7 @@ use servo_arc::Arc as ServoArc;
use style::properties::ComputedValues;
use style::servo::url::ComputedUrl;
use style::values::computed::image::Image as ComputedImage;
use style::values::generics::length::GenericLengthPercentageOrAuto;
use style::values::CSSFloat;
use style::Zero;
use url::Url;
@ -385,15 +386,11 @@ impl ReplacedContent {
mode,
)
};
let clamp = |inline_size: Au, block_size: Au| LogicalVec2 {
inline: inline_size.clamp_between_extremums(min_box_size.inline, max_box_size.inline),
block: block_size.clamp_between_extremums(min_box_size.block, max_box_size.block),
};
// https://drafts.csswg.org/css2/visudet.html#min-max-widths
// https://drafts.csswg.org/css2/visudet.html#min-max-heights
match (box_size.inline, box_size.block) {
let get_tentative_size = |LogicalVec2 { inline, block }| -> LogicalVec2<Au> {
match (inline, block) {
(AuOrAuto::LengthPercentage(inline), AuOrAuto::LengthPercentage(block)) => {
clamp(inline, block)
LogicalVec2 { inline, block }
},
(AuOrAuto::LengthPercentage(inline), AuOrAuto::Auto) => {
let block = if let Some(i_over_b) = intrinsic_ratio {
@ -403,7 +400,7 @@ impl ReplacedContent {
} else {
default_object_size().block
};
clamp(inline, block)
LogicalVec2 { inline, block }
},
(AuOrAuto::Auto, AuOrAuto::LengthPercentage(block)) => {
let inline = if let Some(i_over_b) = intrinsic_ratio {
@ -413,7 +410,7 @@ impl ReplacedContent {
} else {
default_object_size().inline
};
clamp(inline, block)
LogicalVec2 { inline, block }
},
(AuOrAuto::Auto, AuOrAuto::Auto) => {
let inline_size =
@ -451,16 +448,24 @@ impl ReplacedContent {
} else {
default_object_size().block
};
let i_over_b = if let Some(i_over_b) = intrinsic_ratio {
i_over_b
} else {
return clamp(inline_size, block_size);
LogicalVec2 {
inline: inline_size,
block: block_size,
}
},
}
};
// https://drafts.csswg.org/css2/visudet.html#min-max-widths
// “However, for replaced elements with an intrinsic ratio and both
// 'width' and 'height' specified as 'auto', the algorithm is as follows”
if let (AuOrAuto::Auto, AuOrAuto::Auto, Some(i_over_b)) =
(box_size.inline, box_size.block, intrinsic_ratio)
{
let LogicalVec2 {
inline: inline_size,
block: block_size,
} = get_tentative_size(box_size);
enum Violation {
None,
Below(Au),
@ -478,7 +483,7 @@ impl ReplacedContent {
_ => Violation::None,
}
};
match (
return match (
violation(inline_size, min_box_size.inline, max_box_size.inline),
violation(block_size, min_box_size.block, max_box_size.block),
) {
@ -565,9 +570,61 @@ impl ReplacedContent {
block: min_block_size,
}
},
};
}
},
// https://drafts.csswg.org/css2/#min-max-widths "The following algorithm describes how the two properties
// influence the used value of the width property:
//
// 1. The tentative used width is calculated (without min-width and max-width) following the rules under
// "Calculating widths and margins" above.
// 2. If the tentative used width is greater than max-width, the rules above are applied again, but this time
// using the computed value of max-width as the computed value for width.
// 3. If the resulting width is smaller than min-width, the rules above are applied again, but this time using
// the value of min-width as the computed value for width."
let mut tentative_size = get_tentative_size(box_size);
// Create an inline/block size vector from the given clamped inline and block sizes if they are provided,
// falling back to the regular box size if they are not
let size_from_maybe_clamped =
|(clamped_inline, clamped_block): (Option<Au>, Option<Au>)| {
let clamped_inline = clamped_inline
.map(|size| GenericLengthPercentageOrAuto::LengthPercentage(size))
.unwrap_or(box_size.inline);
let clamped_block = clamped_block
.map(|size| GenericLengthPercentageOrAuto::LengthPercentage(size))
.unwrap_or(box_size.block);
LogicalVec2 {
inline: clamped_inline,
block: clamped_block,
}
};
let clamped_max = (
max_box_size
.inline
.filter(|max_inline_size| tentative_size.inline > *max_inline_size),
max_box_size
.block
.filter(|max_block_size| tentative_size.block > *max_block_size),
);
if clamped_max.0.is_some() || clamped_max.1.is_some() {
tentative_size = get_tentative_size(size_from_maybe_clamped(clamped_max));
}
let clamped_min = (
Some(min_box_size.inline)
.filter(|min_inline_size| tentative_size.inline < *min_inline_size),
Some(min_box_size.block)
.filter(|min_block_size| tentative_size.block < *min_block_size),
);
if clamped_min.0.is_some() || clamped_min.1.is_some() {
tentative_size = get_tentative_size(size_from_maybe_clamped(clamped_min));
}
tentative_size
}
}

View file

@ -1,2 +0,0 @@
[inline-replaced-height-010.xht]
expected: FAIL

View file

@ -1,2 +0,0 @@
[inline-replaced-height-011.xht]
expected: FAIL

View file

@ -1,2 +0,0 @@
[inline-replaced-width-016.xht]
expected: FAIL

View file

@ -1,2 +0,0 @@
[inline-replaced-width-017.xht]
expected: FAIL

View file

@ -1,2 +0,0 @@
[flex-aspect-ratio-img-column-005.html]
expected: FAIL

View file

@ -20,12 +20,6 @@
[Computed style test: canvas with {"width":"10%","height":"20"}]
expected: FAIL
[Canvas width and height attributes are used as the surface size with contain:size]
expected: FAIL
[Canvas width and height attributes are used as the surface size]
expected: FAIL
[Computed style test: canvas with {"width":null,"height":null}]
expected: FAIL

View file

@ -109,12 +109,3 @@
[Computed style test: input with {"type":"submit","width":"10%","height":"20"}]
expected: FAIL
[Loaded images test: <img> without width height attributes]
expected: FAIL
[Loaded images test: <img> with width and height attributes, but conflicting to the original aspect ratio]
expected: FAIL
[Loaded images test: <img> with width and height attributes, but not equal to the original aspect ratio]
expected: FAIL