Skip to content

Commit

Permalink
Prevent aspect-ratio:auto from pulling the ratio from the default obj…
Browse files Browse the repository at this point in the history
…ect size

As resolved in w3c/csswg-drafts#7524 (comment)

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
  • Loading branch information
Loirooriol committed Oct 29, 2024
1 parent 848353b commit 215294c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
17 changes: 2 additions & 15 deletions components/layout_2020/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,7 @@ pub(crate) trait NodeExt<'dom>: 'dom + LayoutNode<'dom> {
fn as_image(self) -> Option<(Option<Arc<Image>>, PhysicalSize<f64>)>;
fn as_canvas(self) -> Option<(CanvasInfo, PhysicalSize<f64>)>;
fn as_iframe(self) -> Option<(PipelineId, BrowsingContextId)>;
fn as_video(
self,
) -> Option<(
Option<webrender_api::ImageKey>,
Option<PhysicalSize<f64>>,
bool,
)>;
fn as_video(self) -> Option<(Option<webrender_api::ImageKey>, Option<PhysicalSize<f64>>)>;
fn as_typeless_object_with_data_attribute(self) -> Option<String>;
fn style(self, context: &LayoutContext) -> ServoArc<ComputedValues>;

Expand Down Expand Up @@ -142,13 +136,7 @@ where
Some((resource, PhysicalSize::new(width, height)))
}

fn as_video(
self,
) -> Option<(
Option<webrender_api::ImageKey>,
Option<PhysicalSize<f64>>,
bool,
)> {
fn as_video(self) -> Option<(Option<webrender_api::ImageKey>, Option<PhysicalSize<f64>>)> {
let node = self.to_threadsafe();
let data = node.media_data()?;
let natural_size = if let Some(frame) = data.current_frame {
Expand All @@ -160,7 +148,6 @@ where
Some((
data.current_frame.map(|frame| frame.image_key),
natural_size,
data.has_default_size,
))
}

Expand Down
33 changes: 12 additions & 21 deletions components/layout_2020/replaced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ impl NaturalSizes {
ratio: None,
}
}

pub(crate) fn from_ratio(ratio: Option<f32>) -> Self {
Self {
width: None,
height: None,
ratio,
}
}
}

#[derive(Serialize)]
Expand Down Expand Up @@ -160,18 +152,16 @@ impl ReplacedContent {
}
}

let (kind, natural_size_in_dots, needs_ratio_fallback) = {
let (kind, natural_size_in_dots) = {
if let Some((image, natural_size_in_dots)) = element.as_image() {
(
ReplacedContentKind::Image(image),
Some(natural_size_in_dots),
false,
)
} else if let Some((canvas_info, natural_size_in_dots)) = element.as_canvas() {
(
ReplacedContentKind::Canvas(canvas_info),
Some(natural_size_in_dots),
false,
)
} else if let Some((pipeline_id, browsing_context_id)) = element.as_iframe() {
(
Expand All @@ -180,15 +170,11 @@ impl ReplacedContent {
browsing_context_id,
}),
None,
false,
)
} else if let Some((image_key, natural_size_in_dots, needs_ratio_fallback)) =
element.as_video()
{
} else if let Some((image_key, natural_size_in_dots)) = element.as_video() {
(
ReplacedContentKind::Video(image_key.map(|key| VideoInfo { image_key: key })),
natural_size_in_dots,
needs_ratio_fallback,
)
} else {
return None;
Expand All @@ -203,10 +189,6 @@ impl ReplacedContent {
let width = (naturalc_size_in_dots.width as CSSFloat) / dppx;
let height = (naturalc_size_in_dots.height as CSSFloat) / dppx;
NaturalSizes::from_width_and_height(width, height)
} else if needs_ratio_fallback {
let size = ReplacedContent::default_object_size();
let ratio_fallback = size.width.to_f32_px() / size.height.to_f32_px();
NaturalSizes::from_ratio(Some(ratio_fallback))
} else {
NaturalSizes::empty()
};
Expand Down Expand Up @@ -493,7 +475,16 @@ impl ReplacedContent {
) -> LogicalVec2<Au> {
let mode = style.writing_mode;
let intrinsic_size = self.flow_relative_intrinsic_size(style);
let intrinsic_ratio = self.preferred_aspect_ratio(&containing_block.into(), style);
let intrinsic_ratio = self
.preferred_aspect_ratio(&containing_block.into(), style)
.or_else(|| {
matches!(self.kind, ReplacedContentKind::Video(_)).then(|| {
let size = Self::default_object_size();
AspectRatio::from_content_ratio(
size.width.to_f32_px() / size.height.to_f32_px(),
)
})
});

let default_object_size =
|| LogicalVec2::from_physical_size(&Self::default_object_size(), mode);
Expand Down
26 changes: 13 additions & 13 deletions components/layout_2020/style_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ impl AspectRatio {
},
}
}

pub(crate) fn from_content_ratio(i_over_b: CSSFloat) -> Self {
Self {
box_sizing_adjustment: LogicalVec2::zero(),
i_over_b,
}
}
}

#[derive(Clone)]
Expand Down Expand Up @@ -905,25 +912,18 @@ impl ComputedValuesExt for ComputedValues {
// ratio; otherwise the box has no preferred aspect ratio. Size
// calculations involving the aspect ratio work with the content box
// dimensions always."
(_, PreferredRatio::None) => natural_aspect_ratio.map(|natural_ratio| AspectRatio {
i_over_b: natural_ratio,
box_sizing_adjustment: LogicalVec2::zero(),
}),
(_, PreferredRatio::None) => natural_aspect_ratio.map(AspectRatio::from_content_ratio),
// "If both auto and a <ratio> are specified together, the preferred
// aspect ratio is the specified ratio of width / height unless it
// is a replaced element with a natural aspect ratio, in which case
// that aspect ratio is used instead. In all cases, size
// calculations involving the aspect ratio work with the content box
// dimensions always."
(true, PreferredRatio::Ratio(preferred_ratio)) => match natural_aspect_ratio {
Some(natural_ratio) => Some(AspectRatio {
i_over_b: natural_ratio,
box_sizing_adjustment: LogicalVec2::zero(),
}),
None => Some(AspectRatio {
i_over_b: (preferred_ratio.0).0 / (preferred_ratio.1).0,
box_sizing_adjustment: LogicalVec2::zero(),
}),
(true, PreferredRatio::Ratio(preferred_ratio)) => {
Some(AspectRatio::from_content_ratio(
natural_aspect_ratio
.unwrap_or_else(|| (preferred_ratio.0).0 / (preferred_ratio.1).0),
))
},

// "The box’s preferred aspect ratio is the specified ratio of width
Expand Down

0 comments on commit 215294c

Please sign in to comment.