Skip to content

Truly independent web browser

LibWeb: Store replaced content facts as optional sizes

Aliaksandr Kalenik9a8ae7754c7b
diff --git a/Libraries/LibWeb/Rust/build.rs b/Libraries/LibWeb/Rust/build.rs
index 5af8b667495..cf20f4294bc 100644
--- a/Libraries/LibWeb/Rust/build.rs
+++ b/Libraries/LibWeb/Rust/build.rs
@@ -2876,7 +2876,6 @@ fn main() -> Result<(), Box<dyn Error>> {
"FfiRenderedTextView".to_string(),
"FfiRenderedTextOffset".to_string(),
"FfiRenderedTextWithOffsetsView".to_string(),
- "FfiReplacedContentFacts".to_string(),
"FfiStylePayloads".to_string(),
"FfiUtf16View".to_string(),
// Lent and handed back through the layout header, which builds on this one.
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 1a042f6cc9d..72c9a83aec9 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -1299,10 +1299,10 @@ impl<'pass> FlexFormattingContext<'pass> {
// intrinsic size and no cross size to resolve the ratio against.
// - in response to cross size min/max constraints.
let facts = self.facts(node);
- if facts.has_auto_content_aspect_ratio()
+ if facts.auto_content_size().aspect_ratio.is_some()
&& !basis_is_definite
- && !facts.has_auto_content_width()
- && !facts.has_auto_content_height()
+ && !facts.auto_content_size().width.is_some()
+ && !facts.auto_content_size().height.is_some()
&& !self.has_definite_cross_size(index)
&& self.has_definite_main_size_used(self.container_used())
{
@@ -1984,8 +1984,8 @@ impl<'pass> FlexFormattingContext<'pass> {
// AD-HOC: For a replaced box whose only sizing information is a natural aspect ratio (e.g an SVG with a
// viewBox but no natural width or height) and an indefinite flex basis, we stretch-fit the cross size
// to the flex container instead of transferring the used main size through the aspect ratio.
- let replaced_with_only_natural_ratio =
- facts.is_replaced_box() && !(facts.has_auto_content_width() && facts.has_auto_content_height());
+ let replaced_with_only_natural_ratio = facts.is_replaced_box()
+ && !(facts.auto_content_size().width.is_some() && facts.auto_content_size().height.is_some());
if replaced_with_only_natural_ratio
&& !self.flex_items[index].used_flex_basis_is_definite
&& self.has_definite_cross_size_used(self.container_used())
@@ -3201,7 +3201,7 @@ impl<'pass> FlexFormattingContext<'pass> {
// NB: A replaced element with a natural inline size contributes that size. Its post-flexing
// block size must not replace the natural inline size through the aspect ratio. Explicit
// computed heights are handled by the intrinsic inline size calculation instead.
- if self.facts(node).is_replaced_box() && self.facts(node).has_auto_content_width() {
+ if self.facts(node).is_replaced_box() && self.facts(node).auto_content_size().width.is_some() {
return None;
}
self.facts(node).preferred_aspect_ratio()?;
@@ -3591,7 +3591,7 @@ impl<'pass> FlexFormattingContext<'pass> {
fn cross_content_contribution_depends_on_used_main_size(&self, index: usize) -> bool {
let node = self.flex_items[index].box_;
let facts = self.facts(node);
- let has_natural_inline_size = facts.is_replaced_box() && facts.has_auto_content_width();
+ let has_natural_inline_size = facts.is_replaced_box() && facts.auto_content_size().width.is_some();
if has_natural_inline_size && self.style(node).height().is_auto() {
return false;
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index f91c53cf13f..8204c54a079 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -68,17 +68,18 @@ pub(crate) enum CyclicPercentageSizeProperty {
MinSize,
}
-#[derive(Clone, Copy, Debug)]
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PixelFraction {
pub(crate) numerator: CssPixels,
pub(crate) denominator: CssPixels,
}
-#[derive(Clone, Copy, Debug, Default)]
-pub(super) struct ReplacedIntrinsicSize {
- pub(super) width: Option<CssPixels>,
- pub(super) height: Option<CssPixels>,
- pub(super) aspect_ratio: Option<PixelFraction>,
+/// A replaced box's natural size and aspect ratio, any of which it can lack.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub(crate) struct ReplacedIntrinsicSize {
+ pub(crate) width: Option<CssPixels>,
+ pub(crate) height: Option<CssPixels>,
+ pub(crate) aspect_ratio: Option<PixelFraction>,
}
#[derive(Clone, Copy, Debug, Default)]
diff --git a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
index 118ff132c82..7e77340e7c2 100644
--- a/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/grid_formatting_context.rs
@@ -3793,8 +3793,10 @@ impl<'pass> GridFormattingContext<'pass> {
let preferred = self.item_preferred_size(item, axis);
let alignment = self.item_alignment(item, axis);
let has_natural = axis.select(
- facts.has_auto_content_width() || facts.has_auto_content_height() && facts.has_preferred_aspect_ratio(),
- facts.has_auto_content_height() || facts.has_auto_content_width() && facts.has_preferred_aspect_ratio(),
+ facts.auto_content_size().width.is_some()
+ || facts.auto_content_size().height.is_some() && facts.has_preferred_aspect_ratio(),
+ facts.auto_content_size().height.is_some()
+ || facts.auto_content_size().width.is_some() && facts.has_preferred_aspect_ratio(),
);
// https://drafts.csswg.org/css-grid-1/#grid-item-sizing
// If the grid item has no preferred aspect ratio, and no natural size in the relevant axis (if it is a replaced
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 5b6b438691f..d891eba5435 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -24,12 +24,12 @@ use crate::css::style::{
};
use crate::layout::ComputedValuesView;
use crate::layout::CssPixels;
-use crate::layout::FfiReplacedContentFacts;
use crate::layout::node_data::{
AncestorFact, DomPaintFact, FfiStylePayloads, GENERATED_FOR_AFTER, GENERATED_FOR_BEFORE,
GENERATED_FOR_LAST_SYNTHETIC, GENERATED_FOR_MARKER, MAX_NODE_SLOT_COUNT, NodeData, NodeFlag, NodeKind, NodeSlotId,
StylePayloadsRef, pseudo_kind_of,
};
+use crate::layout::node_facts::ReplacedContentFacts;
use crate::layout::tree_mutation::HostCalls;
use crate::painting::paint_read::{GeometryRead, PaintRead};
use crate::render_state::DocumentHost;
@@ -651,7 +651,7 @@ impl RowsVersion {
#[derive(Clone, Default)]
struct ReplacedContentFactsSlot {
generation: u8,
- facts: Option<FfiReplacedContentFacts>,
+ facts: Option<ReplacedContentFacts>,
}
#[derive(Clone, Copy, Default)]
@@ -5393,7 +5393,7 @@ impl LayoutNodeArena {
.is_some_and(|content| content.rendering_key.is_some())
}
- pub(crate) fn set_replaced_content_facts(&mut self, id: NodeSlotId, facts: FfiReplacedContentFacts) -> bool {
+ pub(crate) fn set_replaced_content_facts(&mut self, id: NodeSlotId, facts: ReplacedContentFacts) -> bool {
self.data(id);
let index = id.slot_index() as usize;
if self.replaced_content_facts.len() <= index {
@@ -5409,7 +5409,7 @@ impl LayoutNodeArena {
changed
}
- pub(crate) fn replaced_content_facts(&self, id: NodeSlotId) -> Option<FfiReplacedContentFacts> {
+ pub(crate) fn replaced_content_facts(&self, id: NodeSlotId) -> Option<ReplacedContentFacts> {
assert!(!id.is_invalid(), "invalid layout node arena slot ID");
self.replaced_content_facts
.get(id.slot_index() as usize)
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
index 50db7c57dad..a9991cbcab7 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_pass.rs
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use super::node_facts::ReplacedContentFacts;
use super::*;
/// The one question a layout pass still asks the document while it runs: what the container-relative
@@ -173,7 +174,7 @@ impl<'arena> LayoutPass<'arena> {
self.arena.style_payloads(node)
}
- pub(crate) fn replaced_content_facts(&self, node: Node) -> Option<FfiReplacedContentFacts> {
+ pub(crate) fn replaced_content_facts(&self, node: Node) -> Option<ReplacedContentFacts> {
self.arena.replaced_content_facts(node)
}
diff --git a/Libraries/LibWeb/Rust/src/layout/mod.rs b/Libraries/LibWeb/Rust/src/layout/mod.rs
index 614a76d7c3d..3236f99f9c2 100644
--- a/Libraries/LibWeb/Rust/src/layout/mod.rs
+++ b/Libraries/LibWeb/Rust/src/layout/mod.rs
@@ -79,7 +79,6 @@ pub(crate) use crate::layout::layout_node_arena::{
HostStyle, LayoutNodeArena, LiveRow, PublishedBoundRows, RowsVersion, SLOTS_PER_CHUNK, SampleKind, StyleRelayout,
};
use crate::layout::node_data::AncestorFact;
-pub use crate::layout::node_data::FfiReplacedContentFacts;
pub use crate::layout::node_data::FfiStylePayloads;
use crate::layout::node_data::NodeData;
use crate::layout::node_data::NodeFlag;
diff --git a/Libraries/LibWeb/Rust/src/layout/node_data.rs b/Libraries/LibWeb/Rust/src/layout/node_data.rs
index cdb3e6e9f43..ed8eb303a27 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_data.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_data.rs
@@ -57,22 +57,6 @@ pub(crate) unsafe fn style_payloads<'a>(style: StylePayloadsRef) -> Option<&'a F
(!style.is_null()).then(|| unsafe { style.deref() })
}
-#[derive(Clone, Copy, Default, PartialEq, Eq)]
-#[repr(C)]
-pub struct FfiReplacedContentFacts {
- pub has_auto_content_width: bool,
- pub auto_content_width: CssPixels,
- pub has_auto_content_height: bool,
- pub auto_content_height: CssPixels,
- pub auto_content_aspect_ratio_numerator: CssPixels,
- pub auto_content_aspect_ratio_denominator: CssPixels,
- pub has_default_preferred_width: bool,
- pub default_preferred_width: CssPixels,
- pub has_default_preferred_height: bool,
- pub default_preferred_height: CssPixels,
- pub text_entry_content_is_ordinary_box: bool,
-}
-
impl From<crate::painting::host::FfiNaturalSize> for crate::css::style::NaturalSize {
fn from(size: crate::painting::host::FfiNaturalSize) -> Self {
Self {
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index 0a2f4e5144f..f1fdb2ee610 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+use super::formatting_context::{PixelFraction, ReplacedIntrinsicSize};
use super::*;
use crate::css::style::{NaturalSize, ReplacedContentInput};
@@ -45,7 +46,7 @@ fn style_has_size_containment(style: ComputedValuesView<'_>) -> bool {
/// The replaced content facts of an enrolled node, from its kind, its computed style and what its
/// element published as the input of its replaced content.
-pub(crate) fn derived_replaced_content_facts(data: &NodeData, input: ReplacedContentInput) -> FfiReplacedContentFacts {
+pub(crate) fn derived_replaced_content_facts(data: &NodeData, input: ReplacedContentInput) -> ReplacedContentFacts {
if data.kind.get() == NodeKind::SVGImageBox {
return svg_image_facts(input);
}
@@ -134,34 +135,19 @@ impl ReplacedContentStyle {
// An SVG <image> runs the default sizing algorithm over its own geometry, so it takes the natural size exactly as its
// image reports it, absent rather than zero while nothing has decoded, together with the default object size that
// applies once something has. Size containment does not apply to it.
-fn svg_image_facts(input: ReplacedContentInput) -> FfiReplacedContentFacts {
+fn svg_image_facts(input: ReplacedContentInput) -> ReplacedContentFacts {
let (ReplacedContentInput::NaturalSize(natural_size) | ReplacedContentInput::DecodedSvgImage(natural_size)) = input
else {
panic!("an SVG image publishes its natural size as it arrives");
};
- let mut facts = FfiReplacedContentFacts::default();
- set_auto_content_size(&mut facts, AutoContentSize::natural(natural_size));
- // The SVG formatting context reads the default object size as it is. It is no preferred size the
- // sizing of an ordinary replaced box would take, so the flags that offer it as one stay clear.
- if matches!(input, ReplacedContentInput::DecodedSvgImage(_)) {
- facts.default_preferred_width = CssPixels::from_integer(300);
- facts.default_preferred_height = CssPixels::from_integer(150);
- }
- facts
-}
-
-fn set_auto_content_size(facts: &mut FfiReplacedContentFacts, auto_content_size: AutoContentSize) {
- if let Some(width) = auto_content_size.width {
- facts.has_auto_content_width = true;
- facts.auto_content_width = width;
- }
- if let Some(height) = auto_content_size.height {
- facts.has_auto_content_height = true;
- facts.auto_content_height = height;
- }
- if let Some((numerator, denominator)) = auto_content_size.aspect_ratio {
- facts.auto_content_aspect_ratio_numerator = numerator;
- facts.auto_content_aspect_ratio_denominator = denominator;
+ ReplacedContentFacts {
+ auto_content_size: ReplacedIntrinsicSize::natural(natural_size),
+ svg_image_default_object_size: if matches!(input, ReplacedContentInput::DecodedSvgImage(_)) {
+ CssPixelSize::new(CssPixels::from_integer(300), CssPixels::from_integer(150))
+ } else {
+ CssPixelSize::default()
+ },
+ ..ReplacedContentFacts::default()
}
}
@@ -169,9 +155,11 @@ fn replaced_content_facts(
kind: NodeKind,
style: ReplacedContentStyle,
input: ReplacedContentInput,
-) -> FfiReplacedContentFacts {
- let mut facts = FfiReplacedContentFacts::default();
- set_auto_content_size(&mut facts, auto_content_size(kind, style, input));
+) -> ReplacedContentFacts {
+ let mut facts = ReplacedContentFacts {
+ auto_content_size: auto_content_size(kind, style, input),
+ ..ReplacedContentFacts::default()
+ };
if let ReplacedContentInput::Input {
size,
is_text_entry: true,
@@ -180,24 +168,50 @@ fn replaced_content_facts(
facts.text_entry_content_is_ordinary_box = style.appearance_is_none || style.field_sizing_is_content;
if style.appearance_is_none && !style.field_sizing_is_content {
let (width, height) = style.text_control_default_preferred_size(size);
- facts.has_default_preferred_width = true;
- facts.default_preferred_width = width;
- facts.has_default_preferred_height = true;
- facts.default_preferred_height = height;
+ facts.default_preferred_width = Some(width);
+ facts.default_preferred_height = Some(height);
}
}
facts
}
-/// A replaced box's natural size and aspect ratio, any of which it can lack.
-#[derive(Default)]
-struct AutoContentSize {
- width: Option<CssPixels>,
- height: Option<CssPixels>,
- aspect_ratio: Option<(CssPixels, CssPixels)>,
+/// What layout reads of the content of a replaced box.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub(crate) struct ReplacedContentFacts {
+ pub(crate) auto_content_size: ReplacedIntrinsicSize,
+ pub(crate) default_preferred_width: Option<CssPixels>,
+ pub(crate) default_preferred_height: Option<CssPixels>,
+ pub(crate) text_entry_content_is_ordinary_box: bool,
+ /// The default object size of an SVG `<image>`, which applies once something has decoded. The SVG formatting
+ /// context reads it as it is: it is no preferred size the sizing of an ordinary replaced box would take.
+ pub(crate) svg_image_default_object_size: CssPixelSize,
+}
+
+impl ReplacedContentFacts {
+ /// The facts with the axes swapped, as a frame whose horizontal axis is vertical sees them.
+ fn transposed(self) -> Self {
+ let size = self.auto_content_size;
+ Self {
+ auto_content_size: ReplacedIntrinsicSize {
+ width: size.height,
+ height: size.width,
+ aspect_ratio: size
+ .aspect_ratio
+ .and_then(|ratio| aspect_ratio_of(ratio.denominator, ratio.numerator)),
+ },
+ default_preferred_width: self.default_preferred_height,
+ default_preferred_height: self.default_preferred_width,
+ ..self
+ }
+ }
+}
+
+/// The aspect ratio of `numerator` to `denominator`, unless the denominator is zero.
+fn aspect_ratio_of(numerator: CssPixels, denominator: CssPixels) -> Option<PixelFraction> {
+ (denominator != CssPixels::default()).then_some(PixelFraction { numerator, denominator })
}
-impl AutoContentSize {
+impl ReplacedIntrinsicSize {
fn of_size((width, height): (CssPixels, CssPixels)) -> Self {
Self {
width: Some(width),
@@ -210,35 +224,43 @@ impl AutoContentSize {
Self {
width: natural_size.width.map(CssPixels::from_raw),
height: natural_size.height.map(CssPixels::from_raw),
- aspect_ratio: natural_size
- .aspect_ratio
- .map(|(numerator, denominator)| (CssPixels::from_raw(numerator), CssPixels::from_raw(denominator))),
+ aspect_ratio: natural_size.aspect_ratio.and_then(|(numerator, denominator)| {
+ aspect_ratio_of(CssPixels::from_raw(numerator), CssPixels::from_raw(denominator))
+ }),
}
}
}
-fn auto_content_size(kind: NodeKind, style: ReplacedContentStyle, input: ReplacedContentInput) -> AutoContentSize {
+fn auto_content_size(
+ kind: NodeKind,
+ style: ReplacedContentStyle,
+ input: ReplacedContentInput,
+) -> ReplacedIntrinsicSize {
if let Some(explicit_size) = style.size_contained {
- return AutoContentSize::of_size(explicit_size);
+ return ReplacedIntrinsicSize::of_size(explicit_size);
}
// https://drafts.csswg.org/css-forms-1/#valdef-field-sizing-content
// The UA must determine the element's intrinsic size based on its content, and must ignore any default preferred
// size defined by the host language for that element.
if style.field_sizing_is_content && matches!(kind, NodeKind::TextAreaBox | NodeKind::TextInputBox) {
- return AutoContentSize::default();
+ return ReplacedIntrinsicSize::default();
}
match kind {
- NodeKind::CheckBox => AutoContentSize::of_size((CssPixels::from_integer(13), CssPixels::from_integer(13))),
- NodeKind::RadioButton => AutoContentSize::of_size((CssPixels::from_integer(12), CssPixels::from_integer(12))),
+ NodeKind::CheckBox => {
+ ReplacedIntrinsicSize::of_size((CssPixels::from_integer(13), CssPixels::from_integer(13)))
+ }
+ NodeKind::RadioButton => {
+ ReplacedIntrinsicSize::of_size((CssPixels::from_integer(12), CssPixels::from_integer(12)))
+ }
// AD-HOC: A slider has no in-flow content to size itself from, so provide a default content-box size for when
// its `width` or `height` is `auto`: 20ch by 16px.
- NodeKind::RangeInputBox => AutoContentSize::of_size((style.characters(20), CssPixels::from_integer(16))),
+ NodeKind::RangeInputBox => ReplacedIntrinsicSize::of_size((style.characters(20), CssPixels::from_integer(16))),
NodeKind::TextAreaBox => {
let ReplacedContentInput::TextArea { cols, rows } = input else {
panic!("a textarea publishes its cols and rows as it arrives");
};
let block_size = CssPixels::nearest_value_for(f64::from(rows) * style.line_height.to_double());
- AutoContentSize::of_size(style.in_writing_mode(style.characters(cols), block_size))
+ ReplacedIntrinsicSize::of_size(style.in_writing_mode(style.characters(cols), block_size))
}
NodeKind::CanvasBox => {
let ReplacedContentInput::Canvas { width, height } = input else {
@@ -246,38 +268,37 @@ fn auto_content_size(kind: NodeKind, style: ReplacedContentStyle, input: Replace
};
let width = CssPixels::from_integer(i64::from(width));
let height = CssPixels::from_integer(i64::from(height));
- AutoContentSize {
+ ReplacedIntrinsicSize {
width: Some(width),
height: Some(height),
- aspect_ratio: (width != CssPixels::default() && height != CssPixels::default())
- .then_some((width, height)),
+ aspect_ratio: aspect_ratio_of(width, height).filter(|_| width != CssPixels::default()),
}
}
NodeKind::TextInputBox => {
let ReplacedContentInput::Input { size, .. } = input else {
panic!("an input publishes its size as it arrives");
};
- AutoContentSize::of_size(style.text_control_default_preferred_size(size))
+ ReplacedIntrinsicSize::of_size(style.text_control_default_preferred_size(size))
}
NodeKind::VideoBox => {
let ReplacedContentInput::NaturalSize(natural_size) = input else {
panic!("a video publishes its natural size as it arrives");
};
- AutoContentSize::natural(natural_size)
+ ReplacedIntrinsicSize::natural(natural_size)
}
NodeKind::ImageBox => {
let ReplacedContentInput::NaturalSize(natural_size) = input else {
panic!("an image box's element or its provider publishes its image's natural size");
};
- AutoContentSize::natural(natural_size)
+ ReplacedIntrinsicSize::natural(natural_size)
}
// An <object> showing an SVG document is sized from the document's root. Any other navigable container has
// no natural size.
NodeKind::NavigableContainerViewport => match input {
- ReplacedContentInput::NaturalSize(natural_size) => AutoContentSize::natural(natural_size),
- _ => AutoContentSize::default(),
+ ReplacedContentInput::NaturalSize(natural_size) => ReplacedIntrinsicSize::natural(natural_size),
+ _ => ReplacedIntrinsicSize::default(),
},
- _ => AutoContentSize::default(),
+ _ => ReplacedIntrinsicSize::default(),
}
}
@@ -833,27 +854,16 @@ impl<'pass> NodeFacts<'pass> {
#[inline]
/// The natural and default sizes of the box's replaced content, as width and height in the frame of the pass.
- fn replaced_content(&self) -> crate::layout::FfiReplacedContentFacts {
+ fn replaced_content(&self) -> ReplacedContentFacts {
let facts = self.physical_replaced_content();
- if !self.callbacks.frame.is_transposed() {
- return facts;
- }
- crate::layout::FfiReplacedContentFacts {
- has_auto_content_width: facts.has_auto_content_height,
- auto_content_width: facts.auto_content_height,
- has_auto_content_height: facts.has_auto_content_width,
- auto_content_height: facts.auto_content_width,
- auto_content_aspect_ratio_numerator: facts.auto_content_aspect_ratio_denominator,
- auto_content_aspect_ratio_denominator: facts.auto_content_aspect_ratio_numerator,
- has_default_preferred_width: facts.has_default_preferred_height,
- default_preferred_width: facts.default_preferred_height,
- has_default_preferred_height: facts.has_default_preferred_width,
- default_preferred_height: facts.default_preferred_width,
- text_entry_content_is_ordinary_box: facts.text_entry_content_is_ordinary_box,
+ if self.callbacks.frame.is_transposed() {
+ facts.transposed()
+ } else {
+ facts
}
}
- fn physical_replaced_content(&self) -> crate::layout::FfiReplacedContentFacts {
+ fn physical_replaced_content(&self) -> ReplacedContentFacts {
let mut facts = self.unzoomed_physical_replaced_content();
// NB: SVG geometry and explicit intrinsic inner sizes already resolve against computed lengths.
if self.data().kind.get() == NodeKind::SVGSVGBox
@@ -867,22 +877,22 @@ impl<'pass> NodeFacts<'pass> {
.computed_values_view_if_styled()
.map_or(1.0, |style| style.effective_zoom());
if zoom != 1.0 {
+ let size = &mut facts.auto_content_size;
+ let zoomed = |length: Option<CssPixels>| length.map(|length| length.scaled(f64::from(zoom)));
match self.data().kind.get() {
// NB: These dimensions are derived from font metrics that already include zoom.
NodeKind::TextAreaBox | NodeKind::TextInputBox => {}
- NodeKind::RangeInputBox => {
- facts.auto_content_height = facts.auto_content_height.scaled(f64::from(zoom));
- }
+ NodeKind::RangeInputBox => size.height = zoomed(size.height),
_ => {
- facts.auto_content_width = facts.auto_content_width.scaled(f64::from(zoom));
- facts.auto_content_height = facts.auto_content_height.scaled(f64::from(zoom));
+ size.width = zoomed(size.width);
+ size.height = zoomed(size.height);
}
}
}
facts
}
- fn unzoomed_physical_replaced_content(&self) -> crate::layout::FfiReplacedContentFacts {
+ fn unzoomed_physical_replaced_content(&self) -> ReplacedContentFacts {
if self.data().kind.get() == NodeKind::SVGSVGBox {
return self.svg_root_replaced_content();
}
@@ -898,7 +908,7 @@ impl<'pass> NodeFacts<'pass> {
!node_may_have_replaced_content_facts_including_size_containment(self.data()),
"replaced content facts were not synced to the arena before layout"
);
- return crate::layout::FfiReplacedContentFacts::default();
+ return ReplacedContentFacts::default();
};
facts
}
@@ -906,7 +916,7 @@ impl<'pass> NodeFacts<'pass> {
/// An <svg> root's natural size resolves the lengths its element published against its style and the
/// viewport, so the pass negotiates it rather than reading synced facts.
#[cold]
- fn svg_root_replaced_content(&self) -> crate::layout::FfiReplacedContentFacts {
+ fn svg_root_replaced_content(&self) -> ReplacedContentFacts {
let mut facts = derived_replaced_content_facts(self.data(), ReplacedContentInput::None);
let arena = self.callbacks.arena();
let is_document_element_box = arena.is_document_svg_root_box(self.node);
@@ -930,14 +940,11 @@ impl<'pass> NodeFacts<'pass> {
});
}
if !size_contained {
- set_auto_content_size(
- &mut facts,
- AutoContentSize {
- width,
- height,
- aspect_ratio,
- },
- );
+ facts.auto_content_size = ReplacedIntrinsicSize {
+ width,
+ height,
+ aspect_ratio: aspect_ratio.and_then(|(numerator, denominator)| aspect_ratio_of(numerator, denominator)),
+ };
}
facts
}
@@ -1304,32 +1311,8 @@ impl<'pass> NodeFacts<'pass> {
has_flag(self.data(), NodeFlag::ListMarkerIsInside)
}
- pub(crate) fn has_auto_content_width(&self) -> bool {
- self.replaced_content().has_auto_content_width
- }
-
- pub(crate) fn auto_content_width(&self) -> crate::layout::CssPixels {
- self.replaced_content().auto_content_width
- }
-
- pub(crate) fn has_auto_content_height(&self) -> bool {
- self.replaced_content().has_auto_content_height
- }
-
- pub(crate) fn auto_content_height(&self) -> crate::layout::CssPixels {
- self.replaced_content().auto_content_height
- }
-
- pub(crate) fn has_auto_content_aspect_ratio(&self) -> bool {
- self.replaced_content().auto_content_aspect_ratio_denominator != crate::layout::CssPixels::default()
- }
-
- pub(crate) fn auto_content_aspect_ratio_numerator(&self) -> crate::layout::CssPixels {
- self.replaced_content().auto_content_aspect_ratio_numerator
- }
-
- pub(crate) fn auto_content_aspect_ratio_denominator(&self) -> crate::layout::CssPixels {
- self.replaced_content().auto_content_aspect_ratio_denominator
+ pub(crate) fn auto_content_size(&self) -> ReplacedIntrinsicSize {
+ self.replaced_content().auto_content_size
}
pub(crate) fn has_auto_content_box_size(&self) -> bool {
@@ -1359,33 +1342,21 @@ impl<'pass> NodeFacts<'pass> {
pub(crate) fn preferred_aspect_ratio(&self) -> Option<formatting_context::PixelFraction> {
let style = self.style();
- if !self.node_has_size_containment() && style.aspect_ratio_uses_natural_when_available() {
- let replaced = self.replaced_content();
- if replaced.auto_content_aspect_ratio_denominator != crate::layout::CssPixels::default() {
- return Some(formatting_context::PixelFraction {
- numerator: replaced.auto_content_aspect_ratio_numerator,
- denominator: replaced.auto_content_aspect_ratio_denominator,
- });
- }
+ if !self.node_has_size_containment()
+ && style.aspect_ratio_uses_natural_when_available()
+ && let Some(aspect_ratio) = self.auto_content_size().aspect_ratio
+ {
+ return Some(aspect_ratio);
}
let (numerator, denominator) = style.css_preferred_aspect_ratio();
- (denominator != crate::layout::CssPixels::default())
- .then_some(formatting_context::PixelFraction { numerator, denominator })
- }
-
- pub(crate) fn has_default_preferred_width(&self) -> bool {
- self.replaced_content().has_default_preferred_width
+ aspect_ratio_of(numerator, denominator)
}
- pub(crate) fn default_preferred_width(&self) -> crate::layout::CssPixels {
+ pub(crate) fn default_preferred_width(&self) -> Option<CssPixels> {
self.replaced_content().default_preferred_width
}
- pub(crate) fn has_default_preferred_height(&self) -> bool {
- self.replaced_content().has_default_preferred_height
- }
-
- pub(crate) fn default_preferred_height(&self) -> crate::layout::CssPixels {
+ pub(crate) fn default_preferred_height(&self) -> Option<CssPixels> {
self.replaced_content().default_preferred_height
}
@@ -1522,13 +1493,9 @@ mod node_facts_tests {
}
}
- fn auto_content_size(facts: crate::layout::FfiReplacedContentFacts) -> Option<(i64, i64)> {
- (facts.has_auto_content_width && facts.has_auto_content_height).then(|| {
- (
- i64::from(facts.auto_content_width.to_int()),
- i64::from(facts.auto_content_height.to_int()),
- )
- })
+ fn auto_content_size(facts: super::ReplacedContentFacts) -> Option<(i64, i64)> {
+ let size = facts.auto_content_size;
+ Some((i64::from(size.width?.to_int()), i64::from(size.height?.to_int())))
}
#[test]
@@ -1552,7 +1519,7 @@ mod node_facts_tests {
};
let text_input = super::replaced_content_facts(NodeKind::TextInputBox, horizontal_style(), input);
assert_eq!(auto_content_size(text_input), Some((160, 16)));
- assert!(!text_input.has_default_preferred_width);
+ assert!(text_input.default_preferred_width.is_none());
let primitive_style = super::ReplacedContentStyle {
appearance_is_none: true,
@@ -1560,15 +1527,15 @@ mod node_facts_tests {
};
let primitive = super::replaced_content_facts(NodeKind::BlockContainer, primitive_style, input);
assert_eq!(auto_content_size(primitive), None);
- assert!(primitive.has_default_preferred_width && primitive.has_default_preferred_height);
- assert_eq!(primitive.default_preferred_width.to_int(), 160);
+ assert!(primitive.default_preferred_height.is_some());
+ assert_eq!(primitive.default_preferred_width.map(|width| width.to_int()), Some(160));
let checkbox_input = ReplacedContentInput::Input {
size: 20,
is_text_entry: false,
};
let checkbox = super::replaced_content_facts(NodeKind::BlockContainer, primitive_style, checkbox_input);
- assert!(!checkbox.has_default_preferred_width);
+ assert!(checkbox.default_preferred_width.is_none());
}
#[test]
@@ -1579,7 +1546,7 @@ mod node_facts_tests {
ReplacedContentInput::Canvas { width: 0, height: 150 },
);
assert_eq!(auto_content_size(empty), Some((0, 150)));
- assert_eq!(empty.auto_content_aspect_ratio_denominator, CssPixels::default());
+ assert!(empty.auto_content_size.aspect_ratio.is_none());
let sized = super::replaced_content_facts(
NodeKind::CanvasBox,
horizontal_style(),
@@ -1588,8 +1555,12 @@ mod node_facts_tests {
height: 150,
},
);
- assert_eq!(sized.auto_content_aspect_ratio_numerator.to_int(), 300);
- assert_eq!(sized.auto_content_aspect_ratio_denominator.to_int(), 150);
+ let aspect_ratio = sized
+ .auto_content_size
+ .aspect_ratio
+ .expect("a sized canvas has an aspect ratio");
+ assert_eq!(aspect_ratio.numerator.to_int(), 300);
+ assert_eq!(aspect_ratio.denominator.to_int(), 150);
}
#[test]
@@ -1599,7 +1570,7 @@ mod node_facts_tests {
horizontal_style(),
ReplacedContentInput::NaturalSize(NaturalSize::default()),
);
- assert!(!without_size.has_auto_content_width && !without_size.has_auto_content_height);
+ assert!(without_size.auto_content_size.width.is_none() && without_size.auto_content_size.height.is_none());
let contained_style = super::ReplacedContentStyle {
size_contained: Some((CssPixels::from_integer(5), CssPixels::default())),
..horizontal_style()
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index cc938ede657..dc4f423425a 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -313,17 +313,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
}
fn auto_content_size(&self, node: Node) -> formatting_context::ReplacedIntrinsicSize {
- let facts = self.facts(node);
- formatting_context::ReplacedIntrinsicSize {
- width: facts.has_auto_content_width().then_some(facts.auto_content_width()),
- height: facts.has_auto_content_height().then_some(facts.auto_content_height()),
- aspect_ratio: facts
- .has_auto_content_aspect_ratio()
- .then_some(formatting_context::PixelFraction {
- numerator: facts.auto_content_aspect_ratio_numerator(),
- denominator: facts.auto_content_aspect_ratio_denominator(),
- }),
- }
+ self.facts(node).auto_content_size()
}
fn intrinsic_size_for_replaced_sizing(&self, node: Node) -> formatting_context::ReplacedIntrinsicSize {
@@ -341,12 +331,8 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// An input element whose type attribute is in one of the above states is an element with default preferred size,
// and user agents are expected to apply the 'field-sizing' CSS property to the element.
formatting_context::ReplacedIntrinsicSize {
- width: facts
- .has_default_preferred_width()
- .then_some(facts.default_preferred_width()),
- height: facts
- .has_default_preferred_height()
- .then_some(facts.default_preferred_height()),
+ width: facts.default_preferred_width(),
+ height: facts.default_preferred_height(),
aspect_ratio: None,
}
}
@@ -1013,8 +999,8 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// size it as a normal box to match other browsers.
if self.should_treat_inline_size_as_auto(node, available_space)
&& self.should_treat_block_size_as_auto(node, available_space, constraints)
- && !facts.has_auto_content_width()
- && !facts.has_auto_content_height()
+ && !facts.auto_content_size().width.is_some()
+ && !facts.auto_content_size().height.is_some()
{
return false;
}
@@ -1351,7 +1337,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// AD-HOC: If the box has a preferred aspect ratio and an intrinsic keyword for width...
if facts.has_preferred_aspect_ratio() && size.is_intrinsic_sizing_constraint() {
// If the box has no natural height to resolve the aspect ratio, we treat the width as auto.
- if !facts.has_auto_content_height() {
+ if !facts.auto_content_size().height.is_some() {
return true;
}
// If the box has definite height, we can resolve the width through the aspect ratio.
@@ -1451,7 +1437,7 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// AD-HOC: If the box has a preferred aspect ratio and an intrinsic keyword for height...
if facts.has_preferred_aspect_ratio() && size.is_intrinsic_sizing_constraint() {
// If the box has no natural width to resolve the aspect ratio, we treat the height as auto.
- if !facts.has_auto_content_width() {
+ if !facts.auto_content_size().width.is_some() {
return true;
}
// If the box has definite width, we can resolve the height through the aspect ratio.
@@ -2026,12 +2012,13 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// replaced sizing. That size remains definite when a min/max constraint clamps the automatic preferred
// height, unless the preferred height is itself an intrinsic sizing constraint.
let native_control_block_size_constraint_is_active = facts.is_native_form_control_box()
- && facts.has_auto_content_height()
&& !style.height().is_intrinsic_sizing_constraint()
- && ((!is_plain_auto(style.min_height()) && block_size > facts.auto_content_height())
- || (!style.max_height().is_auto()
- && !self.should_treat_max_block_size_as_none(node, available_space.block_size, constraints)
- && block_size < facts.auto_content_height()));
+ && facts.auto_content_size().height.is_some_and(|auto_content_height| {
+ (!is_plain_auto(style.min_height()) && block_size > auto_content_height)
+ || (!style.max_height().is_auto()
+ && !self.should_treat_max_block_size_as_none(node, available_space.block_size, constraints)
+ && block_size < auto_content_height)
+ });
let block_size_is_automatic = self.preferred_block_size(node, constraints).is_auto()
|| self.should_treat_block_size_as_auto(node, available_space, constraints);
@@ -2741,7 +2728,9 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
if let Some(transferred) = self.calculate_transferred_inline_size_for_replaced_element(node, constraints) {
return answered_from_style(transferred);
}
- if auto_size.width.is_none() && (facts.has_default_preferred_width() || facts.has_default_preferred_height()) {
+ if auto_size.width.is_none()
+ && (facts.default_preferred_width().is_some() || facts.default_preferred_height().is_some())
+ {
// https://drafts.csswg.org/css-sizing-3/#cyclic-percentage-contribution
// "If the box is non-replaced, then the entire value of any max size property or preferred size property
// ('width'/'max-width'/'height'/'max-height') specified as an expression containing a percentage [...] that is
@@ -2753,12 +2742,8 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
// Elements" section considers non-button-like <input> controls replaced for the percentage-sized replaced
// element rule, so their cyclic-percentage min-content contribution can still compress toward zero.
auto_size = formatting_context::ReplacedIntrinsicSize {
- width: facts
- .has_default_preferred_width()
- .then_some(facts.default_preferred_width()),
- height: facts
- .has_default_preferred_height()
- .then_some(facts.default_preferred_height()),
+ width: facts.default_preferred_width(),
+ height: facts.default_preferred_height(),
aspect_ratio: None,
};
}
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index c2ea16c68ce..2b8a1da2589 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -2278,18 +2278,16 @@ impl<'pass> SvgFormattingContext<'pass> {
.replaced_content_facts(image_box)
.unwrap_or_default();
let natural = SizeWithAspectRatio {
- width: facts.has_auto_content_width.then_some(facts.auto_content_width),
- height: facts.has_auto_content_height.then_some(facts.auto_content_height),
- aspect_ratio: (facts.auto_content_aspect_ratio_denominator != CssPixels::default()).then(|| {
- Fraction::of(
- facts.auto_content_aspect_ratio_numerator,
- facts.auto_content_aspect_ratio_denominator,
- )
- }),
+ width: facts.auto_content_size.width,
+ height: facts.auto_content_size.height,
+ aspect_ratio: facts
+ .auto_content_size
+ .aspect_ratio
+ .map(|ratio| Fraction::of(ratio.numerator, ratio.denominator)),
};
// The default object size applies only once something has decoded; until then the image
// has no size at all.
- let default_size = CssPixelSize::new(facts.default_preferred_width, facts.default_preferred_height);
+ let default_size = facts.svg_image_default_object_size;
let sizing = run_default_sizing_algorithm(specified_width, specified_height, &natural, default_size);
SvgCssPixelRect {