Bug 1883600: Added a new public method resolve_percentage to the LengthPercentage struct in the length_percentage.rs file. r=emilio

(Re-submit with moz-phab cli from https://phabricator.services.mozilla.com/D203584)

Differential Revision: https://phabricator.services.mozilla.com/D203599
This commit is contained in:
Liu 2024-03-05 15:08:37 +00:00
parent e554c2f28c
commit b1de9b0ee8

View file

@ -443,6 +443,19 @@ impl LengthPercentage {
} }
} }
/// Converts to a `<percentage>` with given basis. Returns None if the basis is 0.
#[inline]
pub fn to_percentage_of(&self, basis: Length) -> Option<Percentage> {
if basis.px() == 0. {
return None;
}
Some(match self.unpack() {
Unpacked::Length(l) => Percentage(l.px() / basis.px()),
Unpacked::Percentage(p) => p,
Unpacked::Calc(ref c) => Percentage(c.resolve(basis).px() / basis.px()),
})
}
/// Returns the used value. /// Returns the used value.
#[inline] #[inline]
pub fn to_used_value(&self, containing_length: Au) -> Au { pub fn to_used_value(&self, containing_length: Au) -> Au {