mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Move Percentage from length.rs to mod.rs
This commit is contained in:
parent
82de4c49f3
commit
a5b585a5f1
8 changed files with 166 additions and 171 deletions
|
@ -15,9 +15,9 @@ use std::{cmp, fmt, mem};
|
|||
use std::ascii::AsciiExt;
|
||||
use std::ops::Mul;
|
||||
use style_traits::{HasViewportPercentage, ToCss, ParseError, StyleParseError};
|
||||
use style_traits::values::specified::{AllowedLengthType, AllowedNumericType};
|
||||
use style_traits::values::specified::AllowedLengthType;
|
||||
use stylesheets::CssRuleType;
|
||||
use super::{AllowQuirks, Number, ToComputedValue};
|
||||
use super::{AllowQuirks, Number, ToComputedValue, Percentage};
|
||||
use values::{Auto, CSSFloat, Either, FONT_MEDIUM_PX, None_, Normal};
|
||||
use values::ExtremumLength;
|
||||
use values::computed::{self, Context};
|
||||
|
@ -756,124 +756,6 @@ pub type NonNegativeLengthOrAuto = Either<NonNegativeLength, Auto>;
|
|||
/// Either a NonNegativeLength or a NonNegativeNumber value.
|
||||
pub type NonNegativeLengthOrNumber = Either<NonNegativeLength, NonNegativeNumber>;
|
||||
|
||||
/// A percentage value.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct Percentage {
|
||||
/// The percentage value as a float.
|
||||
///
|
||||
/// [0 .. 100%] maps to [0.0 .. 1.0]
|
||||
value: CSSFloat,
|
||||
/// If this percentage came from a calc() expression, this tells how
|
||||
/// clamping should be done on the value.
|
||||
calc_clamping_mode: Option<AllowedNumericType>,
|
||||
}
|
||||
|
||||
no_viewport_percentage!(Percentage);
|
||||
|
||||
impl ToCss for Percentage {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
||||
where W: fmt::Write,
|
||||
{
|
||||
if self.calc_clamping_mode.is_some() {
|
||||
dest.write_str("calc(")?;
|
||||
}
|
||||
|
||||
write!(dest, "{}%", self.value * 100.)?;
|
||||
|
||||
if self.calc_clamping_mode.is_some() {
|
||||
dest.write_str(")")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Percentage {
|
||||
/// Create a percentage from a numeric value.
|
||||
pub fn new(value: CSSFloat) -> Self {
|
||||
Self {
|
||||
value,
|
||||
calc_clamping_mode: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the underlying value for this float.
|
||||
pub fn get(&self) -> CSSFloat {
|
||||
self.calc_clamping_mode.map_or(self.value, |mode| mode.clamp(self.value))
|
||||
}
|
||||
|
||||
/// Reverse this percentage, preserving calc-ness.
|
||||
///
|
||||
/// For example: If it was 20%, convert it into 80%.
|
||||
pub fn reverse(&mut self) {
|
||||
let new_value = 1. - self.value;
|
||||
self.value = new_value;
|
||||
}
|
||||
|
||||
|
||||
/// Parse a specific kind of percentage.
|
||||
pub fn parse_with_clamping_mode<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
num_context: AllowedNumericType,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
// FIXME: remove early returns when lifetimes are non-lexical
|
||||
match *input.next()? {
|
||||
Token::Percentage { unit_value, .. } if num_context.is_ok(context.parsing_mode, unit_value) => {
|
||||
return Ok(Percentage::new(unit_value))
|
||||
}
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {}
|
||||
ref t => return Err(BasicParseError::UnexpectedToken(t.clone()).into())
|
||||
}
|
||||
|
||||
let result = input.parse_nested_block(|i| {
|
||||
CalcNode::parse_percentage(context, i)
|
||||
})?;
|
||||
|
||||
// TODO(emilio): -moz-image-rect is the only thing that uses
|
||||
// the clamping mode... I guess we could disallow it...
|
||||
Ok(Percentage {
|
||||
value: result,
|
||||
calc_clamping_mode: Some(num_context),
|
||||
})
|
||||
}
|
||||
|
||||
/// Parses a percentage token, but rejects it if it's negative.
|
||||
pub fn parse_non_negative<'i, 't>(context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with_clamping_mode(context, input, AllowedNumericType::NonNegative)
|
||||
}
|
||||
|
||||
/// 0%
|
||||
#[inline]
|
||||
pub fn zero() -> Self {
|
||||
Percentage {
|
||||
value: 0.,
|
||||
calc_clamping_mode: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// 100%
|
||||
#[inline]
|
||||
pub fn hundred() -> Self {
|
||||
Percentage {
|
||||
value: 1.,
|
||||
calc_clamping_mode: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for Percentage {
|
||||
#[inline]
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
Self::parse_with_clamping_mode(context, input, AllowedNumericType::All)
|
||||
}
|
||||
}
|
||||
|
||||
/// A length or a percentage value.
|
||||
#[allow(missing_docs)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue