Implement parsing/serialization of will-change

This commit is contained in:
Nazım Can Altınova 2017-03-02 22:04:37 +03:00
parent 5a656cfa54
commit 56b8c1d8b7
No known key found for this signature in database
GPG key ID: AF9BCD7CE6449954
3 changed files with 93 additions and 0 deletions

View file

@ -1973,3 +1973,67 @@ ${helpers.single_keyword("-moz-orient",
gecko_enum_prefix="StyleOrient", gecko_enum_prefix="StyleOrient",
spec="Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-orient)", spec="Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-orient)",
animatable=False)} 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>

View file

@ -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());
}

View file

@ -85,6 +85,7 @@ mod animation;
mod background; mod background;
mod basic_shape; mod basic_shape;
mod border; mod border;
mod box_;
mod column; mod column;
mod effects; mod effects;
mod font; mod font;