use super::*;
pub(in crate::layout) fn used_inset_left(
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> Option<f32> {
used_length_percentage_or_auto(
style.box_values.inset_left.clone(),
PercentageBasis::definite(layout_pt(containing_block.width())),
)
.map(SemanticLengthExt::points)
}
pub(in crate::layout) fn used_inset_right(
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> Option<f32> {
used_length_percentage_or_auto(
style.box_values.inset_right.clone(),
PercentageBasis::definite(layout_pt(containing_block.width())),
)
.map(SemanticLengthExt::points)
}
pub(in crate::layout) fn used_inset_top(
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> Option<f32> {
used_length_percentage_or_auto(
style.box_values.inset_top.clone(),
PercentageBasis::definite(layout_pt(containing_block.height())),
)
.map(SemanticLengthExt::points)
}
pub(in crate::layout) fn used_inset_bottom(
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> Option<f32> {
used_length_percentage_or_auto(
style.box_values.inset_bottom.clone(),
PercentageBasis::definite(layout_pt(containing_block.height())),
)
.map(SemanticLengthExt::points)
}
pub(in crate::layout) fn set_style_used_width(style: &mut ComputedStyle, width: f32) {
let width = width.max(0.0);
style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(width),
);
}
pub(in crate::layout) fn set_style_used_height(style: &mut ComputedStyle, height: f32) {
let height = height.max(0.0);
style.box_values.height.replace_with_used(
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(height),
),
);
}
pub(in crate::layout) fn set_style_used_logical_inline_size(
style: &mut ComputedStyle,
inline_size: f32,
) {
if style.writing_mode.has_vertical_lines() {
set_style_used_height(style, inline_size);
} else {
set_style_used_width(style, inline_size);
}
}
pub(in crate::layout) fn set_style_used_content_box_width_bounds(
style: &mut ComputedStyle,
width: ContentBoxLength,
) {
let width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(width.points().max(0.0)),
);
style.box_values.min_width = width.clone();
style.box_values.max_width = width;
}
pub(in crate::layout) fn set_style_used_content_box_height_bounds(
style: &mut ComputedStyle,
height: ContentBoxLength,
) {
let height = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(height.points().max(0.0)),
);
style.box_values.min_height = height.clone();
style.box_values.max_height = height;
}
pub(in crate::layout) fn set_style_auto_width(style: &mut ComputedStyle) {
style.box_values.width = css::ComputedLengthPercentageOrAuto::Auto;
}
pub(in crate::layout) fn set_style_auto_height(style: &mut ComputedStyle) {
style
.box_values
.height
.replace_with_used(css::ComputedLengthPercentageOrAuto::Auto);
}
pub(in crate::layout) fn clear_style_insets(style: &mut ComputedStyle) {
style.box_values.inset_left = css::ComputedLengthPercentageOrAuto::Auto;
style.box_values.inset_top = css::ComputedLengthPercentageOrAuto::Auto;
style.box_values.inset_right = css::ComputedLengthPercentageOrAuto::Auto;
style.box_values.inset_bottom = css::ComputedLengthPercentageOrAuto::Auto;
}
#[cfg(test)]
mod tests {
use super::super::*;
fn length(points: f32) -> css::ComputedLengthPercentageOrAuto {
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(points),
)
}
#[test]
fn mutating_used_width_replaces_typed_percentage_with_used_length() {
let mut style = ComputedStyle::initial();
style.box_values.width = css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_percent(0.5),
);
set_style_used_width(&mut style, 42.0);
assert_eq!(
style.box_values.width,
css::ComputedLengthPercentageOrAuto::LengthPercentage(
css::ComputedLengthPercentage::from_points(42.0)
)
);
}
#[test]
fn logical_inline_override_targets_height_in_vertical_writing() {
let mut style = ComputedStyle::initial();
style.writing_mode = WritingMode::VerticalRl;
set_style_used_logical_inline_size(&mut style, 42.0);
assert_eq!(
style.box_values.width,
css::ComputedLengthPercentageOrAuto::Auto
);
assert_eq!(
style
.box_values
.height
.length_if_no_percent()
.expect("used height is a definite length"),
42.0
);
}
#[test]
fn substituting_a_used_or_auto_height_clears_deferred_font_metric_provenance() {
let mut style = ComputedStyle::initial();
style.box_values.height = css::PhysicalHeight::DeferredFontMetric(length(12.0));
set_style_used_height(&mut style, 20.0);
assert!(!style.box_values.height.is_deferred_font_metric());
assert_eq!(
style
.box_values
.height
.length_if_no_percent()
.expect("used height is a definite length"),
20.0
);
style.box_values.height = css::PhysicalHeight::DeferredFontMetric(length(12.0));
set_style_auto_height(&mut style);
assert!(!style.box_values.height.is_deferred_font_metric());
assert!(style.box_values.height.is_auto());
}
}