Auto merge of #16640 - hiikezoe:timing-function, r=xidorn

Use FunctionKeyword for computed_value of timing function

<!-- Please describe your changes on the following line: -->
https://bugzilla.mozilla.org/show_bug.cgi?id=1358330

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #15086

<!-- Either: -->
- [X] These changes do not require tests because mozilla-central has test cases.  There might be some test cases in servo tree.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/16640)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-04-28 22:49:26 -05:00 committed by GitHub
commit 93a513c06f
5 changed files with 141 additions and 43 deletions

View file

@ -339,19 +339,24 @@ impl PropertyAnimation {
/// Update the given animation at a given point of progress. /// Update the given animation at a given point of progress.
pub fn update(&self, style: &mut ComputedValues, time: f64) { pub fn update(&self, style: &mut ComputedValues, time: f64) {
let progress = match self.timing_function { let timing_function = match self.timing_function {
TransitionTimingFunction::Keyword(keyword) =>
keyword.to_non_keyword_value(),
other => other,
};
let progress = match timing_function {
TransitionTimingFunction::CubicBezier(p1, p2) => { TransitionTimingFunction::CubicBezier(p1, p2) => {
// See `WebCore::AnimationBase::solveEpsilon(double)` in WebKit. // See `WebCore::AnimationBase::solveEpsilon(double)` in WebKit.
let epsilon = 1.0 / (200.0 * (self.duration.seconds() as f64)); let epsilon = 1.0 / (200.0 * (self.duration.seconds() as f64));
Bezier::new(Point2D::new(p1.x as f64, p1.y as f64), Bezier::new(Point2D::new(p1.x as f64, p1.y as f64),
Point2D::new(p2.x as f64, p2.y as f64)).solve(time, epsilon) Point2D::new(p2.x as f64, p2.y as f64)).solve(time, epsilon)
} },
TransitionTimingFunction::Steps(steps, StartEnd::Start) => { TransitionTimingFunction::Steps(steps, StartEnd::Start) => {
(time * (steps as f64)).ceil() / (steps as f64) (time * (steps as f64)).ceil() / (steps as f64)
} },
TransitionTimingFunction::Steps(steps, StartEnd::End) => { TransitionTimingFunction::Steps(steps, StartEnd::End) => {
(time * (steps as f64)).floor() / (steps as f64) (time * (steps as f64)).floor() / (steps as f64)
} },
TransitionTimingFunction::Frames(frames) => { TransitionTimingFunction::Frames(frames) => {
// https://drafts.csswg.org/css-timing/#frames-timing-functions // https://drafts.csswg.org/css-timing/#frames-timing-functions
let mut out = (time * (frames as f64)).floor() / ((frames - 1) as f64); let mut out = (time * (frames as f64)).floor() / ((frames - 1) as f64);
@ -367,7 +372,10 @@ impl PropertyAnimation {
out = 1.0; out = 1.0;
} }
out out
} },
TransitionTimingFunction::Keyword(_) => {
panic!("Keyword function should not appear")
},
}; };
self.property.update(style, progress); self.property.update(style, progress);

View file

@ -9,6 +9,7 @@ use properties::longhands::transition_timing_function::single_value::SpecifiedVa
use properties::longhands::transition_timing_function::single_value::computed_value::StartEnd; use properties::longhands::transition_timing_function::single_value::computed_value::StartEnd;
use properties::longhands::transition_timing_function::single_value::computed_value::T as ComputedTimingFunction; use properties::longhands::transition_timing_function::single_value::computed_value::T as ComputedTimingFunction;
use std::mem; use std::mem;
use values::computed::ToComputedValue;
impl nsTimingFunction { impl nsTimingFunction {
fn set_as_step(&mut self, function_type: nsTimingFunction_Type, steps: u32) { fn set_as_step(&mut self, function_type: nsTimingFunction_Type, steps: u32) {
@ -46,23 +47,7 @@ impl nsTimingFunction {
impl From<ComputedTimingFunction> for nsTimingFunction { impl From<ComputedTimingFunction> for nsTimingFunction {
fn from(function: ComputedTimingFunction) -> nsTimingFunction { fn from(function: ComputedTimingFunction) -> nsTimingFunction {
let mut tf: nsTimingFunction = unsafe { mem::zeroed() }; SpecifiedTimingFunction::from_computed_value(&function).into()
match function {
ComputedTimingFunction::Steps(steps, StartEnd::Start) => {
tf.set_as_step(nsTimingFunction_Type::StepStart, steps);
},
ComputedTimingFunction::Steps(steps, StartEnd::End) => {
tf.set_as_step(nsTimingFunction_Type::StepEnd, steps);
},
ComputedTimingFunction::Frames(frames) => {
tf.set_as_frames(frames);
},
ComputedTimingFunction::CubicBezier(p1, p2) => {
tf.set_as_bezier(nsTimingFunction_Type::CubicBezier, p1, p2);
},
}
tf
} }
} }
@ -89,7 +74,7 @@ impl From<SpecifiedTimingFunction> for nsTimingFunction {
Point2D::new(p2.x.get(), p2.y.get())); Point2D::new(p2.x.get(), p2.y.get()));
}, },
SpecifiedTimingFunction::Keyword(keyword) => { SpecifiedTimingFunction::Keyword(keyword) => {
match keyword.to_computed_value() { match keyword.to_non_keyword_value() {
ComputedTimingFunction::CubicBezier(p1, p2) => { ComputedTimingFunction::CubicBezier(p1, p2) => {
match keyword { match keyword {
FunctionKeyword::Ease => { FunctionKeyword::Ease => {
@ -120,7 +105,10 @@ impl From<SpecifiedTimingFunction> for nsTimingFunction {
}, },
ComputedTimingFunction::Frames(frames) => { ComputedTimingFunction::Frames(frames) => {
tf.set_as_frames(frames) tf.set_as_frames(frames)
} },
ComputedTimingFunction::Keyword(_) => {
panic!("Keyword function should not appear")
},
} }
}, },
} }
@ -145,11 +133,21 @@ impl From<nsTimingFunction> for ComputedTimingFunction {
ComputedTimingFunction::Frames( ComputedTimingFunction::Frames(
unsafe { function.__bindgen_anon_1.__bindgen_anon_1.as_ref().mStepsOrFrames }) unsafe { function.__bindgen_anon_1.__bindgen_anon_1.as_ref().mStepsOrFrames })
} }
nsTimingFunction_Type::Ease | nsTimingFunction_Type::Ease => {
nsTimingFunction_Type::Linear | ComputedTimingFunction::Keyword(FunctionKeyword::Ease)
nsTimingFunction_Type::EaseIn | },
nsTimingFunction_Type::EaseOut | nsTimingFunction_Type::Linear => {
nsTimingFunction_Type::EaseInOut | ComputedTimingFunction::Keyword(FunctionKeyword::Linear)
},
nsTimingFunction_Type::EaseIn => {
ComputedTimingFunction::Keyword(FunctionKeyword::EaseIn)
},
nsTimingFunction_Type::EaseOut => {
ComputedTimingFunction::Keyword(FunctionKeyword::EaseOut)
},
nsTimingFunction_Type::EaseInOut => {
ComputedTimingFunction::Keyword(FunctionKeyword::EaseInOut)
},
nsTimingFunction_Type::CubicBezier => { nsTimingFunction_Type::CubicBezier => {
ComputedTimingFunction::CubicBezier( ComputedTimingFunction::CubicBezier(
TypedPoint2D::new(unsafe { function.__bindgen_anon_1.mFunc.as_ref().mX1 }, TypedPoint2D::new(unsafe { function.__bindgen_anon_1.mFunc.as_ref().mX1 },

View file

@ -502,6 +502,7 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use super::FunctionKeyword;
use values::specified; use values::specified;
pub use super::parse; pub use super::parse;
@ -512,6 +513,7 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
CubicBezier(Point2D<f32>, Point2D<f32>), CubicBezier(Point2D<f32>, Point2D<f32>),
Steps(u32, StartEnd), Steps(u32, StartEnd),
Frames(u32), Frames(u32),
Keyword(FunctionKeyword),
} }
impl ToCss for T { impl ToCss for T {
@ -538,6 +540,9 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
try!(frames.to_css(dest)); try!(frames.to_css(dest));
dest.write_str(")") dest.write_str(")")
}, },
T::Keyword(keyword) => {
super::serialize_keyword(dest, keyword)
}
} }
} }
} }
@ -651,6 +656,22 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
dest.write_str(")") dest.write_str(")")
} }
fn serialize_keyword<W>(dest: &mut W, keyword: FunctionKeyword) -> fmt::Result
where W: fmt::Write,
{
match keyword {
FunctionKeyword::StepStart => {
serialize_steps(dest, specified::Integer::new(1), StartEnd::Start)
},
FunctionKeyword::StepEnd => {
serialize_steps(dest, specified::Integer::new(1), StartEnd::End)
},
_ => {
keyword.to_css(dest)
},
}
}
// https://drafts.csswg.org/css-transitions/#serializing-a-timing-function // https://drafts.csswg.org/css-transitions/#serializing-a-timing-function
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -675,17 +696,7 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
dest.write_str(")") dest.write_str(")")
}, },
SpecifiedValue::Keyword(keyword) => { SpecifiedValue::Keyword(keyword) => {
match keyword { serialize_keyword(dest, keyword)
FunctionKeyword::StepStart => {
serialize_steps(dest, specified::Integer::new(1), StartEnd::Start)
},
FunctionKeyword::StepEnd => {
serialize_steps(dest, specified::Integer::new(1), StartEnd::End)
},
_ => {
keyword.to_css(dest)
},
}
}, },
} }
} }
@ -708,7 +719,9 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
SpecifiedValue::Frames(frames) => { SpecifiedValue::Frames(frames) => {
computed_value::T::Frames(frames.to_computed_value(context) as u32) computed_value::T::Frames(frames.to_computed_value(context) as u32)
}, },
SpecifiedValue::Keyword(keyword) => keyword.to_computed_value(), SpecifiedValue::Keyword(keyword) => {
computed_value::T::Keyword(keyword)
},
} }
} }
#[inline] #[inline]
@ -729,13 +742,16 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
let frames = frames as i32; let frames = frames as i32;
SpecifiedValue::Frames(specified::Integer::from_computed_value(&frames)) SpecifiedValue::Frames(specified::Integer::from_computed_value(&frames))
}, },
computed_value::T::Keyword(keyword) => {
SpecifiedValue::Keyword(keyword)
},
} }
} }
} }
impl FunctionKeyword { impl FunctionKeyword {
#[inline] #[inline]
pub fn to_computed_value(&self) -> computed_value::T { pub fn to_non_keyword_value(&self) -> computed_value::T {
match *self { match *self {
FunctionKeyword::Ease => ease(), FunctionKeyword::Ease => ease(),
FunctionKeyword::Linear => linear(), FunctionKeyword::Linear => linear(),
@ -753,7 +769,7 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
#[inline] #[inline]
pub fn get_initial_value() -> computed_value::T { pub fn get_initial_value() -> computed_value::T {
ease() computed_value::T::Keyword(FunctionKeyword::Ease)
} }
#[inline] #[inline]

View file

@ -0,0 +1,38 @@
[transition-001.htm]
type: testharness
[parse '1s']
expected: FAIL
[parse '1s 2s']
expected: FAIL
[parse '1s 2s ease-in']
expected: FAIL
[parse '1s ease-in 2s']
expected: FAIL
[parse 'ease-in 1s 2s']
expected: FAIL
[parse '1s width']
expected: FAIL
[parse 'width 1s']
expected: FAIL
[parse '1s width 2s']
expected: FAIL
[parse '1s 2s width ease-in']
expected: FAIL
[parse '1s ease-in 2s width']
expected: FAIL
[parse 'width ease-in 1s 2s']
expected: FAIL
[parse 'width .1s ease-in .2s']
expected: FAIL

View file

@ -0,0 +1,38 @@
[transition-timing-function-001.htm]
type: testharness
[parse 'ease']
expected: FAIL
[parse 'linear']
expected: FAIL
[parse 'ease-in']
expected: FAIL
[parse 'ease-out']
expected: FAIL
[parse 'ease-in-out']
expected: FAIL
[parse 'cubic-bezier(foobar)']
expected: FAIL
[parse 'steps(foobar)']
expected: FAIL
[parse 'steps(3.3, end)']
expected: FAIL
[parse 'steps(3, top)']
expected: FAIL
[parse 'steps(-3, top)']
expected: FAIL
[parse 'cubic-bezier(-0.1, -0.2, -0.3, -0.4)']
expected: FAIL
[parse 'cubic-bezier(1.1, 1.2, 1.3, 1.4)']
expected: FAIL