mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
style: Add at <position> into ray() in style system
We reuse PositionOrAuto here, and let "auto" represent the situation when the author omits "at <position>" because it has a special meaning. https://drafts.fxtf.org/motion-1/#valdef-ray-at-position Note: No need to update css/motion/parsing/offset-path-parsing-valid.html because Blink added some to the upstream repo already. Differential Revision: https://phabricator.services.mozilla.com/D179860
This commit is contained in:
parent
8b60424e29
commit
2fef5d1a17
4 changed files with 86 additions and 33 deletions
|
@ -4,12 +4,17 @@
|
|||
|
||||
//! Computed types for CSS values that are related to motion path.
|
||||
|
||||
use crate::values::computed::{Angle, LengthPercentage};
|
||||
use crate::values::generics::motion::{GenericOffsetPath, GenericOffsetPosition};
|
||||
use crate::values::computed::{Angle, LengthPercentage, Position};
|
||||
use crate::values::generics::motion::{
|
||||
GenericOffsetPath, GenericOffsetPosition, GenericRayFunction,
|
||||
};
|
||||
use crate::Zero;
|
||||
|
||||
/// The computed value of ray() function.
|
||||
pub type RayFunction = GenericRayFunction<Angle, Position>;
|
||||
|
||||
/// The computed value of `offset-path`.
|
||||
pub type OffsetPath = GenericOffsetPath<Angle>;
|
||||
pub type OffsetPath = GenericOffsetPath<RayFunction>;
|
||||
|
||||
/// The computed value of `offset-position`.
|
||||
pub type OffsetPosition = GenericOffsetPosition<LengthPercentage, LengthPercentage>;
|
||||
|
|
|
@ -5,15 +5,19 @@
|
|||
//! Generic types for CSS Motion Path.
|
||||
|
||||
use crate::values::animated::ToAnimatedZero;
|
||||
use crate::values::generics::position::GenericPosition;
|
||||
use crate::values::generics::position::{GenericPosition, GenericPositionOrAuto};
|
||||
use crate::values::specified::SVGPathData;
|
||||
use std::fmt::{self, Write};
|
||||
use style_traits::{CssWriter, ToCss};
|
||||
|
||||
/// The <size> in ray() function.
|
||||
///
|
||||
/// https://drafts.fxtf.org/motion-1/#valdef-offsetpath-size
|
||||
#[allow(missing_docs)]
|
||||
#[derive(
|
||||
Animate,
|
||||
Clone,
|
||||
ComputeSquaredDistance,
|
||||
Copy,
|
||||
Debug,
|
||||
Deserialize,
|
||||
|
@ -36,15 +40,7 @@ pub enum RaySize {
|
|||
Sides,
|
||||
}
|
||||
|
||||
impl RaySize {
|
||||
/// Returns true if it is the default value.
|
||||
#[inline]
|
||||
pub fn is_default(&self) -> bool {
|
||||
*self == RaySize::ClosestSide
|
||||
}
|
||||
}
|
||||
|
||||
/// The `ray()` function, `ray( [ <angle> && <size> && contain? ] )`
|
||||
/// The `ray()` function, `ray( [ <angle> && <size> && contain? && [at <position>]? ] )`
|
||||
///
|
||||
/// https://drafts.fxtf.org/motion-1/#valdef-offsetpath-ray
|
||||
#[derive(
|
||||
|
@ -58,25 +54,54 @@ impl RaySize {
|
|||
Serialize,
|
||||
SpecifiedValueInfo,
|
||||
ToComputedValue,
|
||||
ToCss,
|
||||
ToResolvedValue,
|
||||
ToShmem,
|
||||
)]
|
||||
#[repr(C)]
|
||||
pub struct RayFunction<Angle> {
|
||||
pub struct GenericRayFunction<Angle, Position> {
|
||||
/// The bearing angle with `0deg` pointing up and positive angles
|
||||
/// representing clockwise rotation.
|
||||
pub angle: Angle,
|
||||
/// Decide the path length used when `offset-distance` is expressed
|
||||
/// as a percentage.
|
||||
#[animation(constant)]
|
||||
#[css(skip_if = "RaySize::is_default")]
|
||||
pub size: RaySize,
|
||||
/// Clamp `offset-distance` so that the box is entirely contained
|
||||
/// within the path.
|
||||
#[animation(constant)]
|
||||
#[css(represents_keyword)]
|
||||
pub contain: bool,
|
||||
/// The "at <position>" part. If omitted, we use auto to represent it.
|
||||
pub position: GenericPositionOrAuto<Position>,
|
||||
}
|
||||
|
||||
pub use self::GenericRayFunction as RayFunction;
|
||||
|
||||
impl<Angle, Position> ToCss for RayFunction<Angle, Position>
|
||||
where
|
||||
Angle: ToCss,
|
||||
Position: ToCss,
|
||||
{
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
self.angle.to_css(dest)?;
|
||||
|
||||
if !matches!(self.size, RaySize::ClosestSide) {
|
||||
dest.write_char(' ')?;
|
||||
self.size.to_css(dest)?;
|
||||
}
|
||||
|
||||
if self.contain {
|
||||
dest.write_str(" contain")?;
|
||||
}
|
||||
|
||||
if !matches!(self.position, GenericPositionOrAuto::Auto) {
|
||||
dest.write_str(" at ")?;
|
||||
self.position.to_css(dest)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The offset-path value.
|
||||
|
@ -98,15 +123,16 @@ pub struct RayFunction<Angle> {
|
|||
ToShmem,
|
||||
)]
|
||||
#[repr(C, u8)]
|
||||
pub enum GenericOffsetPath<Angle> {
|
||||
pub enum GenericOffsetPath<RayFunction> {
|
||||
// We could merge SVGPathData into ShapeSource, so we could reuse them. However,
|
||||
// we don't want to support other value for offset-path, so use SVGPathData only for now.
|
||||
/// Path value for path(<string>).
|
||||
#[css(function)]
|
||||
Path(SVGPathData),
|
||||
/// ray() function, which defines a path in the polar coordinate system.
|
||||
/// Use Box<> to make sure the size of offset-path is not too large.
|
||||
#[css(function)]
|
||||
Ray(RayFunction<Angle>),
|
||||
Ray(Box<RayFunction>),
|
||||
/// None value.
|
||||
#[animation(error)]
|
||||
None,
|
||||
|
@ -115,7 +141,7 @@ pub enum GenericOffsetPath<Angle> {
|
|||
|
||||
pub use self::GenericOffsetPath as OffsetPath;
|
||||
|
||||
impl<Angle> OffsetPath<Angle> {
|
||||
impl<Ray> OffsetPath<Ray> {
|
||||
/// Return None.
|
||||
#[inline]
|
||||
pub fn none() -> Self {
|
||||
|
@ -123,7 +149,7 @@ impl<Angle> OffsetPath<Angle> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<Angle> ToAnimatedZero for OffsetPath<Angle> {
|
||||
impl<Ray> ToAnimatedZero for OffsetPath<Ray> {
|
||||
#[inline]
|
||||
fn to_animated_zero(&self) -> Result<Self, ()> {
|
||||
Err(())
|
||||
|
|
|
@ -101,6 +101,12 @@ impl<Pos> PositionOrAuto<Pos> {
|
|||
pub fn auto() -> Self {
|
||||
PositionOrAuto::Auto
|
||||
}
|
||||
|
||||
/// Return true if it is 'auto'.
|
||||
#[inline]
|
||||
pub fn is_auto(&self) -> bool {
|
||||
matches!(self, PositionOrAuto::Auto)
|
||||
}
|
||||
}
|
||||
|
||||
/// A generic value for the `z-index` property.
|
||||
|
|
|
@ -7,20 +7,21 @@
|
|||
use crate::parser::{Parse, ParserContext};
|
||||
use crate::values::computed::motion::OffsetRotate as ComputedOffsetRotate;
|
||||
use crate::values::computed::{Context, ToComputedValue};
|
||||
use crate::values::generics::motion::{
|
||||
GenericOffsetPath, GenericOffsetPosition, RayFunction, RaySize,
|
||||
};
|
||||
use crate::values::generics::motion as generics;
|
||||
use crate::values::specified::position::{HorizontalPosition, VerticalPosition};
|
||||
use crate::values::specified::{Angle, SVGPathData};
|
||||
use crate::values::specified::{Angle, Position};
|
||||
use crate::Zero;
|
||||
use cssparser::Parser;
|
||||
use style_traits::{ParseError, StyleParseErrorKind};
|
||||
|
||||
/// The specified value of ray() function.
|
||||
pub type RayFunction = generics::GenericRayFunction<Angle, Position>;
|
||||
|
||||
/// The specified value of `offset-path`.
|
||||
pub type OffsetPath = GenericOffsetPath<Angle>;
|
||||
pub type OffsetPath = generics::GenericOffsetPath<RayFunction>;
|
||||
|
||||
/// The specified value of `offset-position`.
|
||||
pub type OffsetPosition = GenericOffsetPosition<HorizontalPosition, VerticalPosition>;
|
||||
pub type OffsetPosition = generics::GenericOffsetPosition<HorizontalPosition, VerticalPosition>;
|
||||
|
||||
#[cfg(feature = "gecko")]
|
||||
fn is_ray_enabled() -> bool {
|
||||
|
@ -31,11 +32,13 @@ fn is_ray_enabled() -> bool {
|
|||
false
|
||||
}
|
||||
|
||||
impl Parse for RayFunction<Angle> {
|
||||
impl Parse for RayFunction {
|
||||
fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
use crate::values::specified::PositionOrAuto;
|
||||
|
||||
if !is_ray_enabled() {
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
|
@ -43,13 +46,14 @@ impl Parse for RayFunction<Angle> {
|
|||
let mut angle = None;
|
||||
let mut size = None;
|
||||
let mut contain = false;
|
||||
let mut position = None;
|
||||
loop {
|
||||
if angle.is_none() {
|
||||
angle = input.try_parse(|i| Angle::parse(context, i)).ok();
|
||||
}
|
||||
|
||||
if size.is_none() {
|
||||
size = input.try_parse(RaySize::parse).ok();
|
||||
size = input.try_parse(generics::RaySize::parse).ok();
|
||||
if size.is_some() {
|
||||
continue;
|
||||
}
|
||||
|
@ -63,6 +67,17 @@ impl Parse for RayFunction<Angle> {
|
|||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if position.is_none() {
|
||||
if input.try_parse(|i| i.expect_ident_matching("at")).is_ok() {
|
||||
let pos = Position::parse(context, input)?;
|
||||
position = Some(PositionOrAuto::Position(pos));
|
||||
}
|
||||
|
||||
if position.is_some() {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -73,8 +88,9 @@ impl Parse for RayFunction<Angle> {
|
|||
Ok(RayFunction {
|
||||
angle: angle.unwrap(),
|
||||
// If no <ray-size> is specified it defaults to closest-side.
|
||||
size: size.unwrap_or(RaySize::ClosestSide),
|
||||
size: size.unwrap_or(generics::RaySize::ClosestSide),
|
||||
contain,
|
||||
position: position.unwrap_or(PositionOrAuto::auto()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -84,7 +100,7 @@ impl Parse for OffsetPath {
|
|||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
use crate::values::specified::svg_path::AllowEmpty;
|
||||
use crate::values::specified::svg_path::{AllowEmpty, SVGPathData};
|
||||
|
||||
// Parse none.
|
||||
if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
|
||||
|
@ -99,7 +115,7 @@ impl Parse for OffsetPath {
|
|||
// Bug 1186329: Implement the parser for <basic-shape>, <geometry-box>,
|
||||
// and <url>.
|
||||
"path" => SVGPathData::parse(i, AllowEmpty::No).map(OffsetPath::Path),
|
||||
"ray" => RayFunction::parse(context, i).map(OffsetPath::Ray),
|
||||
"ray" => RayFunction::parse(context, i).map(|v| OffsetPath::Ray(Box::new(v))),
|
||||
_ => {
|
||||
Err(location.new_custom_error(
|
||||
StyleParseErrorKind::UnexpectedFunction(function.clone())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue