Skip to content

Truly independent web browser

LibWeb: Keep plain lengths and percentages inline in computed sizes

Aliaksandr Kalenik01177a984c69
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
index 463aabd7171..d21b5adbafd 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
@@ -37,6 +37,10 @@ unsafe impl Sync for ComputedStyleValueHandle where crate::css::style_value::Sty
#[derive(Debug)]
pub struct ComputedSize {
pub kind: ComputedSizeKind,
+ /// The plain length or percentage `value` holds, kept inline so that reading it needs no dereference: the form of
+ /// `inline_value`, one of the forms `InlineLengthPercentage` decodes.
+ pub inline_form: u8,
+ pub inline_value: u32,
pub value: ComputedStyleValueHandle,
}
@@ -107,6 +111,9 @@ pub const OVERFLOW_ALIGNMENT_UNSAFE: u8 = 2;
#[repr(C)]
pub struct ComputedLengthPercentageOrAuto {
pub is_auto: bool,
+ /// The plain length or percentage `value` holds, as in [`ComputedSize`].
+ pub inline_form: u8,
+ pub inline_value: u32,
pub value: ComputedStyleValueHandle,
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
index a0b9d48860a..3df791eb9a4 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_views.rs
@@ -138,6 +138,73 @@ impl LengthPercentageRef<'_> {
}
}
+/// The plain length or percentage a computed size or length-percentage keeps inline beside its style value, as the
+/// reads of the style value would resolve it.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) enum InlineLengthPercentage {
+ /// An absolute length, in CSS pixels.
+ Length(CssPixels),
+ /// A percentage, whose value as an f32 is exactly the f64 the style value holds.
+ Percentage(f32),
+}
+
+impl InlineLengthPercentage {
+ const NONE: u8 = 0;
+ const LENGTH: u8 = 1;
+ const PERCENTAGE: u8 = 2;
+
+ /// What a computed value holding `value` keeps inline: nothing for a calculation, a relative length or a percentage
+ /// an f32 does not hold.
+ pub(crate) fn of(value: Option<&StyleValueData>) -> Option<Self> {
+ match value? {
+ StyleValueData::Length { value, unit } => {
+ let ratio = crate::css::style_compute::LENGTH_UNIT_CANONICAL_PX_RATIOS[*unit as usize];
+ ratio
+ .is_finite()
+ .then(|| Self::Length(CssPixels::nearest_value_for(value * ratio)))
+ }
+ StyleValueData::Percentage { value } => {
+ let narrowed = *value as f32;
+ (f64::from(narrowed) == *value).then_some(Self::Percentage(narrowed))
+ }
+ _ => None,
+ }
+ }
+
+ /// The form and value fields that hold `inline`.
+ pub(crate) fn encode(inline: Option<Self>) -> (u8, u32) {
+ match inline {
+ None => (Self::NONE, 0),
+ Some(Self::Length(pixels)) => (Self::LENGTH, pixels.raw_value() as u32),
+ Some(Self::Percentage(value)) => (Self::PERCENTAGE, value.to_bits()),
+ }
+ }
+
+ #[inline]
+ fn decode(form: u8, value: u32) -> Option<Self> {
+ match form {
+ Self::LENGTH => Some(Self::Length(CssPixels::from_raw(value as i32))),
+ Self::PERCENTAGE => Some(Self::Percentage(f32::from_bits(value))),
+ _ => None,
+ }
+ }
+
+ /// As [`LengthPercentageRef::to_px`] resolves the style value.
+ #[inline]
+ fn to_px(self, reference: CssPixels) -> CssPixels {
+ match self {
+ Self::Length(pixels) => pixels,
+ Self::Percentage(value) => {
+ CssPixels::truncated_value_for(reference.to_double() * (f64::from(value) * 0.01))
+ }
+ }
+ }
+
+ fn contains_percentage(self) -> bool {
+ matches!(self, Self::Percentage(_))
+ }
+}
+
impl ComputedStyleValueHandle {
/// The lifetime is the caller's to choose: the referenced style value is
/// retained by whatever owns the handle, not by the handle borrow itself.
@@ -233,6 +300,12 @@ impl ComputedSize {
self.value.length_percentage()
}
+ /// The plain length or percentage the size keeps inline, if any.
+ #[inline]
+ pub(crate) fn inline_length_percentage(&self) -> Option<InlineLengthPercentage> {
+ InlineLengthPercentage::decode(self.inline_form, self.inline_value)
+ }
+
pub(crate) fn to_px(&self, reference: CssPixels) -> CssPixels {
match self.kind {
ComputedSizeKind::Auto
@@ -241,7 +314,10 @@ impl ComputedSize {
| ComputedSizeKind::Stretch
| ComputedSizeKind::None => CssPixels::default(),
ComputedSizeKind::Calculated | ComputedSizeKind::Length | ComputedSizeKind::Percentage => {
- self.length_percentage().to_px(reference)
+ match self.inline_length_percentage() {
+ Some(inline) => inline.to_px(reference),
+ None => self.length_percentage().to_px(reference),
+ }
}
ComputedSizeKind::FitContent => self
.fit_content_available_space()
@@ -290,12 +366,24 @@ impl ComputedLengthPercentageOrAuto {
)
}
+ /// The plain length or percentage the value keeps inline, if any.
+ #[inline]
+ pub(crate) fn inline_length_percentage(&self) -> Option<InlineLengthPercentage> {
+ InlineLengthPercentage::decode(self.inline_form, self.inline_value)
+ }
+
pub(crate) fn to_px(&self, reference: CssPixels) -> CssPixels {
+ if let Some(inline) = self.inline_length_percentage() {
+ return inline.to_px(reference);
+ }
self.length_percentage()
.map_or(CssPixels::default(), |value| value.to_px(reference))
}
pub(crate) fn contains_percentage(&self) -> bool {
+ if let Some(inline) = self.inline_length_percentage() {
+ return inline.contains_percentage();
+ }
self.length_percentage()
.is_some_and(|value| value.contains_percentage())
}
@@ -331,6 +419,8 @@ unsafe impl Sync for SyncComputedSize {}
static AUTO_COMPUTED_SIZE: SyncComputedSize = SyncComputedSize(ComputedSize {
kind: ComputedSizeKind::Auto,
+ inline_form: InlineLengthPercentage::NONE,
+ inline_value: 0,
value: ComputedStyleValueHandle {
pointer: std::ptr::null(),
},
@@ -344,6 +434,8 @@ pub(crate) fn auto_computed_size() -> &'static ComputedSize {
static NONE_COMPUTED_SIZE: SyncComputedSize = SyncComputedSize(ComputedSize {
kind: ComputedSizeKind::None,
+ inline_form: InlineLengthPercentage::NONE,
+ inline_value: 0,
value: ComputedStyleValueHandle {
pointer: std::ptr::null(),
},
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index a043907f3ea..f0d580aa2fb 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -44,6 +44,7 @@ pub use crate::css::computed_value_types::{
RetainedTextDecorationLineList, RetainedWillChangePropertyList, SVGResetValues, SizingValues, StyleBits,
SurroundValues, TextResetValues, TransformValues,
};
+use crate::css::computed_value_views::InlineLengthPercentage;
use crate::css::retained_fly_string::{RetainedUtf16FlyString, RetainedUtf16FlyStringList};
use crate::css::style_value::{RetainedStyleValueData, StyleValueData};
use crate::css::style_value::{retained_list_drop, retained_list_partial_eq};
@@ -118,17 +119,37 @@ impl Clone for ComputedSize {
fn clone(&self) -> Self {
Self {
kind: self.kind,
+ inline_form: self.inline_form,
+ inline_value: self.inline_value,
value: self.value.clone(),
}
}
}
+// NB: The inline length or percentage is derived from the value, so it takes no part in comparisons.
impl PartialEq for ComputedSize {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind && self.value == other.value
}
}
+impl Clone for ComputedLengthPercentageOrAuto {
+ fn clone(&self) -> Self {
+ Self {
+ is_auto: self.is_auto,
+ inline_form: self.inline_form,
+ inline_value: self.inline_value,
+ value: self.value.clone(),
+ }
+ }
+}
+
+impl PartialEq for ComputedLengthPercentageOrAuto {
+ fn eq(&self, other: &Self) -> bool {
+ self.is_auto == other.is_auto && self.value == other.value
+ }
+}
+
macro_rules! impl_computed_payload_clone_and_eq {
($type:ty { $($field:ident),+ $(,)? }) => {
impl Clone for $type {
@@ -205,7 +226,6 @@ impl_computed_payload_clone_and_eq!(AlignmentValues {
column_gap,
row_gap,
});
-impl_computed_payload_clone_and_eq!(ComputedLengthPercentageOrAuto { is_auto, value });
impl_computed_payload_clone_and_eq!(ComputedLengthBox {
top,
right,
@@ -915,10 +935,7 @@ impl GridValues {
ComputedGridTrackBreadth {
is_flex: false,
flex_factor: 0.0,
- size: ComputedSize {
- kind: ComputedSizeKind::Auto,
- value: ComputedStyleValueHandle::empty(),
- },
+ size: ComputedSize::keyword(ComputedSizeKind::Auto),
}
}
@@ -5600,18 +5617,23 @@ pub(crate) fn build_group_payload_with_rust_fill<T: StyleGroupPayload>(
}
impl ComputedSize {
- pub(crate) fn keyword(kind: ComputedSizeKind) -> Self {
+ /// A size of `kind` holding `value`, which keeps the plain length or percentage `value` holds inline.
+ pub(crate) fn new(kind: ComputedSizeKind, value: ComputedStyleValueHandle) -> Self {
+ let (inline_form, inline_value) = InlineLengthPercentage::encode(InlineLengthPercentage::of(value.data()));
Self {
kind,
- value: ComputedStyleValueHandle::empty(),
+ inline_form,
+ inline_value,
+ value,
}
}
+ pub(crate) fn keyword(kind: ComputedSizeKind) -> Self {
+ Self::new(kind, ComputedStyleValueHandle::empty())
+ }
+
fn retained(kind: ComputedSizeKind, data: &RetainedStyleValueData) -> Self {
- Self {
- kind,
- value: ComputedStyleValueHandle::from_retained(data),
- }
+ Self::new(kind, ComputedStyleValueHandle::from_retained(data))
}
pub(crate) fn from_data(data: &RetainedStyleValueData) -> Self {
@@ -5663,10 +5685,9 @@ impl ComputedSize {
Some(StyleValueData::Percentage { .. }) => Self::retained(ComputedSizeKind::Percentage, data),
Some(StyleValueData::Length { .. }) => Self::retained(ComputedSizeKind::Length, data),
// NB: Layout resolves anchor-size() itself, through the calculation that wraps it.
- Some(StyleValueData::AnchorSize { .. }) => Self {
- kind: ComputedSizeKind::Calculated,
- value: anchor_function_calculated_handle(data),
- },
+ Some(StyleValueData::AnchorSize { .. }) => {
+ Self::new(ComputedSizeKind::Calculated, anchor_function_calculated_handle(data))
+ }
_ => Self::keyword(ComputedSizeKind::Auto),
}
}
@@ -5786,18 +5807,23 @@ fn anchor_function_calculated_handle(data: &RetainedStyleValueData) -> ComputedS
}
impl ComputedLengthPercentageOrAuto {
- fn auto() -> Self {
+ /// A value holding `value`, or auto, which keeps the plain length or percentage `value` holds inline.
+ pub(crate) fn new(is_auto: bool, value: ComputedStyleValueHandle) -> Self {
+ let (inline_form, inline_value) = InlineLengthPercentage::encode(InlineLengthPercentage::of(value.data()));
Self {
- is_auto: true,
- value: ComputedStyleValueHandle::empty(),
+ is_auto,
+ inline_form,
+ inline_value,
+ value,
}
}
+ fn auto() -> Self {
+ Self::new(true, ComputedStyleValueHandle::empty())
+ }
+
fn zero() -> Self {
- Self {
- is_auto: false,
- value: ComputedStyleValueHandle::length(0.0),
- }
+ Self::new(false, ComputedStyleValueHandle::length(0.0))
}
pub(crate) fn from_data(data: &RetainedStyleValueData) -> Self {
@@ -5808,14 +5834,14 @@ impl ComputedLengthPercentageOrAuto {
data.optional_data(),
Some(StyleValueData::Keyword { keyword }) if *keyword == keyword::AUTO
);
- Self {
+ Self::new(
is_auto,
- value: if is_auto {
+ if is_auto {
ComputedStyleValueHandle::empty()
} else {
ComputedStyleValueHandle::from_retained(data)
},
- }
+ )
}
fn from_length_box_data(data: &RetainedStyleValueData, default_is_auto: bool) -> Self {
@@ -5826,16 +5852,12 @@ impl ComputedLengthPercentageOrAuto {
Some(StyleValueData::Keyword { keyword: value }) if *value == keyword::AUTO => Self::auto(),
Some(
StyleValueData::Length { .. } | StyleValueData::Percentage { .. } | StyleValueData::Calculated { .. },
- ) => Self {
- is_auto: false,
- value: ComputedStyleValueHandle::from_retained(data),
- },
+ ) => Self::new(false, ComputedStyleValueHandle::from_retained(data)),
// NB: Layout resolves an anchor-size() margin itself, through the calculation that wraps it. Insets keep
// their anchor functions in separate wrappers instead, and expose auto here.
- Some(StyleValueData::AnchorSize { .. }) if !default_is_auto => Self {
- is_auto: false,
- value: anchor_function_calculated_handle(data),
- },
+ Some(StyleValueData::AnchorSize { .. }) if !default_is_auto => {
+ Self::new(false, anchor_function_calculated_handle(data))
+ }
_ if default_is_auto => Self::auto(),
_ => Self::zero(),
}
@@ -5854,10 +5876,7 @@ impl ComputedLengthBox {
pub(crate) fn zero() -> Self {
let zero = ComputedStyleValueHandle::length(0.0);
- let side = || ComputedLengthPercentageOrAuto {
- is_auto: false,
- value: zero.clone(),
- };
+ let side = || ComputedLengthPercentageOrAuto::new(false, zero.clone());
Self {
top: side(),
right: side(),
@@ -5970,14 +5989,8 @@ impl SVGResetValues {
cy: zero.clone(),
d: ComputedStyleValueHandle::keyword(keyword::NONE),
r: zero.clone(),
- rx: ComputedLengthPercentageOrAuto {
- is_auto: true,
- value: ComputedStyleValueHandle::empty(),
- },
- ry: ComputedLengthPercentageOrAuto {
- is_auto: true,
- value: ComputedStyleValueHandle::empty(),
- },
+ rx: ComputedLengthPercentageOrAuto::auto(),
+ ry: ComputedLengthPercentageOrAuto::auto(),
x: zero.clone(),
y: zero,
stop_color: crate::css::computed_value_types::StyleColor::rgba(OPAQUE_BLACK_BGRA),
diff --git a/Libraries/LibWeb/Rust/src/css/style/layout_style.rs b/Libraries/LibWeb/Rust/src/css/style/layout_style.rs
index 42de09f5669..4429f9be585 100644
--- a/Libraries/LibWeb/Rust/src/css/style/layout_style.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/layout_style.rs
@@ -526,10 +526,10 @@ impl LayoutStyle {
values.flex_direction = flex_direction::COLUMN;
});
style.edit::<SizingValues>(|values| {
- values.height = ComputedSize {
- kind: ComputedSizeKind::Percentage,
- value: ComputedStyleValueHandle::percentage(100.0),
- }
+ values.height = ComputedSize::new(
+ ComputedSizeKind::Percentage,
+ ComputedStyleValueHandle::percentage(100.0),
+ );
});
}
AnonymousStyleKind::FieldsetContentWrapper => {
@@ -572,10 +572,7 @@ impl LayoutStyle {
}
if kind == AnonymousStyleKind::ButtonContentBox {
style.edit::<SizingValues>(|values| {
- values.min_height = ComputedSize {
- kind: ComputedSizeKind::Length,
- value: ComputedStyleValueHandle::length(0.0),
- }
+ values.min_height = ComputedSize::new(ComputedSizeKind::Length, ComputedStyleValueHandle::length(0.0));
});
}
style
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index 2f9164fef04..6b1f3740695 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -4657,16 +4657,16 @@ mod stretch_cross_limit_tests {
if index == 1 && max_constrained {
let surround = style.surround_mut();
for side in [&mut surround.margin.top, &mut surround.margin.bottom] {
- *side = ComputedLengthPercentageOrAuto {
- is_auto: false,
- value: crate::css::computed_values::ComputedStyleValueHandle::length(5.0),
- };
+ *side = ComputedLengthPercentageOrAuto::new(
+ false,
+ crate::css::computed_values::ComputedStyleValueHandle::length(5.0),
+ );
}
for side in [&mut surround.padding.top, &mut surround.padding.bottom] {
- *side = ComputedLengthPercentageOrAuto {
- is_auto: false,
- value: crate::css::computed_values::ComputedStyleValueHandle::length(2.0),
- };
+ *side = ComputedLengthPercentageOrAuto::new(
+ false,
+ crate::css::computed_values::ComputedStyleValueHandle::length(2.0),
+ );
}
let border = style.border_mut();
for side in [&mut border.border_top, &mut border.border_bottom] {
diff --git a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
index 5abe92dbf13..0ea32e84da7 100644
--- a/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fragment_overflow.rs
@@ -877,10 +877,8 @@ mod tests {
});
// SAFETY: The container's style outlives the child's.
let child_style = LayoutScratchStyle::over(unsafe { container_style.style().view() }, |style| {
- style.surround_mut().margin.bottom = ComputedLengthPercentageOrAuto {
- is_auto: false,
- value: ComputedStyleValueHandle::percentage(50.0),
- };
+ style.surround_mut().margin.bottom =
+ ComputedLengthPercentageOrAuto::new(false, ComputedStyleValueHandle::percentage(50.0));
});
let container = arena.allocate_for_test().slot;
let child = arena.allocate_for_test().slot;
diff --git a/Libraries/LibWeb/Rust/src/layout/style_values.rs b/Libraries/LibWeb/Rust/src/layout/style_values.rs
index fbc08d2065a..fe13135c866 100644
--- a/Libraries/LibWeb/Rust/src/layout/style_values.rs
+++ b/Libraries/LibWeb/Rust/src/layout/style_values.rs
@@ -135,18 +135,15 @@ impl AnchorResolvedValues {
pub(crate) fn new(resolved: &ResolvedAnchorSizes, frame: geometry::Frame) -> Self {
let size = |value: Option<ResolvedSizeOverride>| {
value.map(|value| match value {
- ResolvedSizeOverride::Length(px) => ComputedSize {
- kind: ComputedSizeKind::Length,
- value: ComputedStyleValueHandle::length(px.to_double()),
- },
+ ResolvedSizeOverride::Length(px) => ComputedSize::new(
+ ComputedSizeKind::Length,
+ ComputedStyleValueHandle::length(px.to_double()),
+ ),
ResolvedSizeOverride::Keyword(kind) => ComputedSize::keyword(kind),
})
};
let margin = |value: Option<CssPixels>| {
- value.map(|px| ComputedLengthPercentageOrAuto {
- is_auto: false,
- value: ComputedStyleValueHandle::length(px.to_double()),
- })
+ value.map(|px| ComputedLengthPercentageOrAuto::new(false, ComputedStyleValueHandle::length(px.to_double())))
};
Self {
width: size(resolved.width),