Skip to content

Commit

Permalink
Implement parsing/serialization of will-change
Browse files Browse the repository at this point in the history
  • Loading branch information
canova committed Mar 21, 2017
1 parent 5a656cf commit 56b8c1d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
64 changes: 64 additions & 0 deletions components/style/properties/longhand/box.mako.rs
Expand Up @@ -1973,3 +1973,67 @@ ${helpers.single_keyword("-moz-orient",
gecko_enum_prefix="StyleOrient",
spec="Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-orient)",
animatable=False)}

<%helpers:longhand name="will-change" products="none" animatable="False"
spec="https://drafts.csswg.org/css-will-change/#will-change">
use cssparser::serialize_identifier;
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
use values::computed::ComputedValueAsSpecified;

impl ComputedValueAsSpecified for SpecifiedValue {}
no_viewport_percentage!(SpecifiedValue);

pub mod computed_value {
pub use super::SpecifiedValue as T;
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum SpecifiedValue {
Auto,
AnimateableFeatures(Vec<Atom>),
}

impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SpecifiedValue::Auto => dest.write_str("auto"),
SpecifiedValue::AnimateableFeatures(ref features) => {
let (first, rest) = features.split_first().unwrap();
// handle head element
serialize_identifier(&*first.to_string(), dest)?;
// handle tail, precede each with a delimiter
for feature in rest {
dest.write_str(", ")?;
serialize_identifier(&*feature.to_string(), dest)?;
}
Ok(())
}
}
}
}

#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T::Auto
}

/// auto | <animateable-feature>#
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("auto")).is_ok() {
Ok(computed_value::T::Auto)
} else {
input.parse_comma_separated(|i| {
let ident = i.expect_ident()?;
match_ignore_ascii_case! { &ident,
"will-change" | "none" | "all" | "auto" |
"initial" | "inherit" | "unset" | "default" => return Err(()),
_ => {},
}
Ok((Atom::from(ident)))
}).map(SpecifiedValue::AnimateableFeatures)
}
}
</%helpers:longhand>
28 changes: 28 additions & 0 deletions tests/unit/style/parsing/box_.rs
@@ -0,0 +1,28 @@
/* 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/. */

use cssparser::Parser;
use media_queries::CSSErrorReporterTest;
use parsing::parse;
use style::parser::ParserContext;
use style::stylesheets::Origin;
use style_traits::ToCss;

#[test]
fn test_will_change() {
use style::properties::longhands::will_change;

assert_roundtrip_with_context!(will_change::parse, "auto");
assert_roundtrip_with_context!(will_change::parse, "scroll-position");
assert_roundtrip_with_context!(will_change::parse, "contents");
assert_roundtrip_with_context!(will_change::parse, "transition");
assert_roundtrip_with_context!(will_change::parse, "opacity, transform");

assert!(parse(will_change::parse, "will-change").is_err());
assert!(parse(will_change::parse, "all").is_err());
assert!(parse(will_change::parse, "none").is_err());
assert!(parse(will_change::parse, "contents, auto").is_err());
assert!(parse(will_change::parse, "contents, inherit, initial").is_err());
assert!(parse(will_change::parse, "transform scroll-position").is_err());
}
1 change: 1 addition & 0 deletions tests/unit/style/parsing/mod.rs
Expand Up @@ -85,6 +85,7 @@ mod animation;
mod background;
mod basic_shape;
mod border;
mod box_;
mod column;
mod effects;
mod font;
Expand Down

0 comments on commit 56b8c1d

Please sign in to comment.