style: Move cursor property out of mako

This commit is contained in:
Igor Gutorov 2018-01-15 16:21:44 +02:00
parent 671b69c0b7
commit 4ee9eb8563
17 changed files with 413 additions and 360 deletions

View file

@ -20,7 +20,7 @@ use std::cell::RefCell;
#[cfg(feature = "servo")]
use std::sync::Arc;
use style_traits::ToCss;
use style_traits::cursor::Cursor;
use style_traits::cursor::CursorKind;
use super::{CSSFloat, CSSInteger};
use super::generics::{GreaterThanOrEqualToOne, NonNegative};
use super::generics::grid::{GridLine as GenericGridLine, TrackBreadth as GenericTrackBreadth};
@ -61,6 +61,9 @@ pub use self::list::ListStyleType;
pub use self::outline::OutlineStyle;
pub use self::percentage::Percentage;
pub use self::position::{Position, GridAutoFlow, GridTemplateAreas};
pub use self::pointing::Cursor;
#[cfg(feature = "gecko")]
pub use self::pointing::CursorImage;
pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind};
pub use self::svg::{SVGPaintOrder, SVGStrokeDashArray, SVGWidth};
pub use self::svg::MozContextProperties;
@ -90,6 +93,7 @@ pub mod length;
pub mod list;
pub mod outline;
pub mod percentage;
pub mod pointing;
pub mod position;
pub mod rect;
pub mod svg;
@ -404,7 +408,7 @@ trivial_to_computed_value!(u16);
trivial_to_computed_value!(u32);
trivial_to_computed_value!(Atom);
trivial_to_computed_value!(BorderStyle);
trivial_to_computed_value!(Cursor);
trivial_to_computed_value!(CursorKind);
trivial_to_computed_value!(Namespace);
trivial_to_computed_value!(String);
trivial_to_computed_value!(Box<str>);

View file

@ -0,0 +1,138 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Computed values for Pointing properties.
//!
//! https://drafts.csswg.org/css-ui/#pointing-keyboard
use cssparser::Parser;
use parser::{Parse, ParserContext};
use selectors::parser::SelectorParseErrorKind;
#[cfg(feature = "gecko")]
use std::fmt;
use style_traits::ParseError;
#[cfg(feature = "gecko")]
use style_traits::ToCss;
use style_traits::cursor::CursorKind;
/// The computed value for the `cursor` property.
///
/// https://drafts.csswg.org/css-ui/#cursor
pub use values::specified::pointing::Cursor;
#[cfg(feature = "gecko")]
pub use values::specified::pointing::CursorImage;
#[cfg(feature = "gecko")]
use values::specified::url::SpecifiedUrl;
impl Cursor {
/// Set `cursor` to `auto`
#[cfg(feature = "servo")]
#[inline]
pub fn auto() -> Self {
Cursor(CursorKind::Auto)
}
/// Set `cursor` to `auto`
#[cfg(feature = "gecko")]
#[inline]
pub fn auto() -> Self {
Self {
images: vec![].into_boxed_slice(),
keyword: CursorKind::Auto
}
}
}
impl Parse for Cursor {
/// cursor: [auto | default | ...]
#[cfg(feature = "servo")]
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<Self, ParseError<'i>> {
Ok(Cursor(CursorKind::parse(context, input)?))
}
/// cursor: [<url> [<number> <number>]?]# [auto | default | ...]
#[cfg(feature = "gecko")]
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<Self, ParseError<'i>> {
let mut images = vec![];
loop {
match input.try(|input| CursorImage::parse_image(context, input)) {
Ok(mut image) => {
image.url.build_image_value();
images.push(image)
}
Err(_) => break,
}
input.expect_comma()?;
}
Ok(Self {
images: images.into_boxed_slice(),
keyword: CursorKind::parse(context, input)?,
})
}
}
#[cfg(feature = "gecko")]
impl ToCss for Cursor {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
for url in &*self.images {
url.to_css(dest)?;
dest.write_str(", ")?;
}
self.keyword.to_css(dest)
}
}
impl Parse for CursorKind {
fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<Self, ParseError<'i>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
CursorKind::from_css_keyword(&ident)
.map_err(|_| location.new_custom_error(
SelectorParseErrorKind::UnexpectedIdent(ident.clone())))
}
}
#[cfg(feature = "gecko")]
impl CursorImage {
fn parse_image<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<Self, ParseError<'i>> {
Ok(Self {
url: SpecifiedUrl::parse(context, input)?,
// FIXME(emilio): Should use Number::parse to handle calc() correctly.
hotspot: match input.try(|input| input.expect_number()) {
Ok(number) => Some((number, input.expect_number()?)),
Err(_) => None,
},
})
}
}
#[cfg(feature = "gecko")]
impl ToCss for CursorImage {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write
{
self.url.to_css(dest)?;
if let Some((x, y)) = self.hotspot {
dest.write_str(" ")?;
x.to_css(dest)?;
dest.write_str(" ")?;
y.to_css(dest)?;
}
Ok(())
}
}

View file

@ -57,6 +57,9 @@ pub use self::outline::OutlineStyle;
pub use self::rect::LengthOrNumberRect;
pub use self::percentage::Percentage;
pub use self::position::{Position, PositionComponent, GridAutoFlow, GridTemplateAreas};
pub use self::pointing::Cursor;
#[cfg(feature = "gecko")]
pub use self::pointing::CursorImage;
pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind};
pub use self::svg::{SVGPaintOrder, SVGStrokeDashArray, SVGWidth};
pub use self::svg::MozContextProperties;
@ -90,6 +93,7 @@ pub mod length;
pub mod list;
pub mod outline;
pub mod percentage;
pub mod pointing;
pub mod position;
pub mod rect;
pub mod source_size_list;

View file

@ -0,0 +1,40 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Specified values for Pointing properties.
//!
//! https://drafts.csswg.org/css-ui/#pointing-keyboard
use style_traits::cursor::CursorKind;
#[cfg(feature = "gecko")]
use values::specified::url::SpecifiedUrl;
/// The specified value for the `cursor` property.
///
/// https://drafts.csswg.org/css-ui/#cursor
#[cfg(feature = "servo")]
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss)]
pub struct Cursor(pub CursorKind);
/// The specified value for the `cursor` property.
///
/// https://drafts.csswg.org/css-ui/#cursor
#[cfg(feature = "gecko")]
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
pub struct Cursor {
/// The parsed images for the cursor.
pub images: Box<[CursorImage]>,
/// The kind of the cursor [default | help | ...].
pub keyword: CursorKind,
}
/// The specified value for the `image cursors`.
#[cfg(feature = "gecko")]
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
pub struct CursorImage {
/// The url to parse images from.
pub url: SpecifiedUrl,
/// The <x> and <y> coordinates.
pub hotspot: Option<(f32, f32)>,
}