mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
style: Use Rust lengths for margin / padding / inset.
Also for the intersection observer root margin, since it was easier to fix it up and clean it up than not doing it. This is the first big step to get rid of nscoord. It duplicates a bit of logic in nsLayoutUtils since for now max/min-width/height are still represented with nsStyleCoord, but I think I prefer to land this incrementally. I didn't add helpers for the physical accessors of the style rect sides that nsStyleSides has (top/bottom/left/right) since I think we generally should encourage the logical versions, but let me know if you want me to do that. Differential Revision: https://phabricator.services.mozilla.com/D17739
This commit is contained in:
parent
13e12d23f3
commit
8dad956513
5 changed files with 64 additions and 64 deletions
|
@ -4,21 +4,19 @@
|
|||
|
||||
//! Specified types for legacy Gecko-only properties.
|
||||
|
||||
use crate::gecko::values::GeckoStyleCoordConvertible;
|
||||
use crate::gecko_bindings::sugar::ns_style_coord::{CoordData, CoordDataMut};
|
||||
use crate::parser::{Parse, ParserContext};
|
||||
use crate::values::computed;
|
||||
use crate::values::computed::{self, LengthPercentage};
|
||||
use crate::values::computed::length::CSSPixelLength;
|
||||
use crate::values::generics::gecko::ScrollSnapPoint as GenericScrollSnapPoint;
|
||||
use crate::values::generics::rect::Rect;
|
||||
use crate::values::specified::length::LengthPercentage;
|
||||
use crate::values::specified::length::LengthPercentage as SpecifiedLengthPercentage;
|
||||
use cssparser::{Parser, Token};
|
||||
use std::fmt;
|
||||
use style_traits::values::SequenceWriter;
|
||||
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
|
||||
|
||||
/// A specified type for scroll snap points.
|
||||
pub type ScrollSnapPoint = GenericScrollSnapPoint<LengthPercentage>;
|
||||
pub type ScrollSnapPoint = GenericScrollSnapPoint<SpecifiedLengthPercentage>;
|
||||
|
||||
impl Parse for ScrollSnapPoint {
|
||||
fn parse<'i, 't>(
|
||||
|
@ -29,62 +27,34 @@ impl Parse for ScrollSnapPoint {
|
|||
return Ok(GenericScrollSnapPoint::None);
|
||||
}
|
||||
input.expect_function_matching("repeat")?;
|
||||
// FIXME(emilio): This won't clamp properly when animating.
|
||||
let length =
|
||||
input.parse_nested_block(|i| LengthPercentage::parse_non_negative(context, i))?;
|
||||
input.parse_nested_block(|i| SpecifiedLengthPercentage::parse_non_negative(context, i))?;
|
||||
Ok(GenericScrollSnapPoint::Repeat(length))
|
||||
}
|
||||
}
|
||||
|
||||
/// A component of an IntersectionObserverRootMargin.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
|
||||
pub enum PixelOrPercentage {
|
||||
/// An absolute length in pixels (px)
|
||||
Pixel(CSSPixelLength),
|
||||
/// A percentage (%)
|
||||
Percentage(computed::Percentage),
|
||||
}
|
||||
|
||||
impl Parse for PixelOrPercentage {
|
||||
fn parse<'i, 't>(
|
||||
_context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
let location = input.current_source_location();
|
||||
let token = input.next()?;
|
||||
let value = match *token {
|
||||
Token::Dimension {
|
||||
value, ref unit, ..
|
||||
} => {
|
||||
match_ignore_ascii_case! { unit,
|
||||
"px" => Ok(PixelOrPercentage::Pixel(CSSPixelLength::new(value))),
|
||||
_ => Err(()),
|
||||
}
|
||||
},
|
||||
Token::Percentage { unit_value, .. } => Ok(PixelOrPercentage::Percentage(
|
||||
computed::Percentage(unit_value),
|
||||
)),
|
||||
_ => Err(()),
|
||||
};
|
||||
value.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
|
||||
}
|
||||
}
|
||||
|
||||
impl GeckoStyleCoordConvertible for PixelOrPercentage {
|
||||
fn to_gecko_style_coord<T: CoordDataMut>(&self, coord: &mut T) {
|
||||
match *self {
|
||||
PixelOrPercentage::Pixel(ref l) => l.to_gecko_style_coord(coord),
|
||||
PixelOrPercentage::Percentage(ref pc) => pc.to_gecko_style_coord(coord),
|
||||
}
|
||||
}
|
||||
|
||||
fn from_gecko_style_coord<T: CoordData>(coord: &T) -> Option<Self> {
|
||||
CSSPixelLength::from_gecko_style_coord(coord)
|
||||
.map(PixelOrPercentage::Pixel)
|
||||
.or_else(|| {
|
||||
computed::Percentage::from_gecko_style_coord(coord)
|
||||
.map(PixelOrPercentage::Percentage)
|
||||
})
|
||||
}
|
||||
fn parse_pixel_or_percent<'i, 't>(
|
||||
_context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<LengthPercentage, ParseError<'i>> {
|
||||
let location = input.current_source_location();
|
||||
let token = input.next()?;
|
||||
let value = match *token {
|
||||
Token::Dimension {
|
||||
value, ref unit, ..
|
||||
} => {
|
||||
match_ignore_ascii_case! { unit,
|
||||
"px" => Ok(LengthPercentage::new(CSSPixelLength::new(value), None)),
|
||||
_ => Err(()),
|
||||
}
|
||||
},
|
||||
Token::Percentage { unit_value, .. } => Ok(
|
||||
LengthPercentage::new_percent(computed::Percentage(unit_value))
|
||||
),
|
||||
_ => Err(()),
|
||||
};
|
||||
value.map_err(|()| location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
|
||||
}
|
||||
|
||||
/// The value of an IntersectionObserver's rootMargin property.
|
||||
|
@ -93,14 +63,15 @@ impl GeckoStyleCoordConvertible for PixelOrPercentage {
|
|||
/// calc() values are not allowed.
|
||||
///
|
||||
/// <https://w3c.github.io/IntersectionObserver/#parse-a-root-margin>
|
||||
pub struct IntersectionObserverRootMargin(pub Rect<PixelOrPercentage>);
|
||||
#[repr(transparent)]
|
||||
pub struct IntersectionObserverRootMargin(pub Rect<LengthPercentage>);
|
||||
|
||||
impl Parse for IntersectionObserverRootMargin {
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
let rect = Rect::parse_with(context, input, PixelOrPercentage::parse)?;
|
||||
let rect = Rect::parse_with(context, input, parse_pixel_or_percent)?;
|
||||
Ok(IntersectionObserverRootMargin(rect))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue