From abe8c0d457e51940b85675fdd3679cc9fbca2e75 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Tue, 27 Sep 2016 15:49:47 -0700 Subject: [PATCH] Add `util::clamp` function --- components/layout/block.rs | 7 ++----- components/util/lib.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/components/layout/block.rs b/components/layout/block.rs index 74392510c0c..afc1f9e7180 100644 --- a/components/layout/block.rs +++ b/components/layout/block.rs @@ -63,6 +63,7 @@ use style::logical_geometry::{LogicalPoint, LogicalRect, LogicalSize, WritingMod use style::properties::ServoComputedValues; use style::values::computed::{LengthOrNone, LengthOrPercentageOrNone}; use style::values::computed::{LengthOrPercentage, LengthOrPercentageOrAuto}; +use util::clamp; use util::geometry::max_rect; /// The number of screens of data we're allowed to generate display lists for in each direction. @@ -1534,12 +1535,8 @@ impl BlockFlow { box_sizing::T::content_box => size + self.fragment.border_padding.inline_start_end(), } - } else if available_inline_size > max_inline_size { - max_inline_size - } else if available_inline_size < min_inline_size { - min_inline_size } else { - available_inline_size + clamp(min_inline_size, available_inline_size, max_inline_size) }; self.base.position.size.inline = inline_size + self.fragment.margin.inline_start_end(); diff --git a/components/util/lib.rs b/components/util/lib.rs index 6513f61a940..edc4a27da42 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -44,3 +44,13 @@ pub fn servo_version() -> String { None => format!("Servo {}", cargo_version), } } + +pub fn clamp(lo: T, mid: T, hi: T) -> T { + if mid < lo { + lo + } else if mid > hi { + hi + } else { + mid + } +} \ No newline at end of file