style: Improve Percentage -> LengthPercentage conversion

This doesn't change behavior because we only use them for images that
have no clamping.

Depends on D147008

Differential Revision: https://phabricator.services.mozilla.com/D147511
This commit is contained in:
Emilio Cobos Álvarez 2023-08-15 00:53:25 +02:00 committed by Martin Robinson
parent 079fce37d2
commit 6ae56890b3
3 changed files with 17 additions and 7 deletions

View file

@ -733,12 +733,12 @@ impl Gradient {
if items.is_empty() {
items = vec![
generic::GradientItem::ComplexColorStop {
color: Color::transparent().into(),
position: Percentage::zero().into(),
color: Color::transparent(),
position: LengthPercentage::zero_percent(),
},
generic::GradientItem::ComplexColorStop {
color: Color::transparent().into(),
position: Percentage::hundred().into(),
color: Color::transparent(),
position: LengthPercentage::hundred_percent(),
},
];
} else if items.len() == 1 {

View file

@ -1328,10 +1328,9 @@ impl From<NoCalcLength> for LengthPercentage {
impl From<Percentage> for LengthPercentage {
#[inline]
fn from(pc: Percentage) -> Self {
if pc.is_calc() {
// FIXME(emilio): Hard-coding the clamping mode is suspect.
if let Some(clamping_mode) = pc.calc_clamping_mode() {
LengthPercentage::Calc(Box::new(CalcLengthPercentage {
clamping_mode: AllowedNumericType::All,
clamping_mode,
node: CalcNode::Leaf(calc::Leaf::Percentage(pc.get())),
}))
} else {
@ -1364,6 +1363,12 @@ impl LengthPercentage {
LengthPercentage::Percentage(computed::Percentage::zero())
}
#[inline]
/// Returns a `100%` value.
pub fn hundred_percent() -> LengthPercentage {
LengthPercentage::Percentage(computed::Percentage::hundred())
}
fn parse_internal<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,

View file

@ -97,6 +97,11 @@ impl Percentage {
self.calc_clamping_mode.is_some()
}
/// Returns the calc() clamping mode for this percentage.
pub fn calc_clamping_mode(&self) -> Option<AllowedNumericType> {
self.calc_clamping_mode
}
/// Reverses this percentage, preserving calc-ness.
///
/// For example: If it was 20%, convert it into 80%.