diff --git a/Source/HtmlRenderer/Core/CssEngine/StyleProperties/Transition/TransitionProperty.cs b/Source/HtmlRenderer/Core/CssEngine/StyleProperties/Transition/TransitionProperty.cs index b7a1b753f..b9a1e5041 100644 --- a/Source/HtmlRenderer/Core/CssEngine/StyleProperties/Transition/TransitionProperty.cs +++ b/Source/HtmlRenderer/Core/CssEngine/StyleProperties/Transition/TransitionProperty.cs @@ -4,14 +4,10 @@ namespace TheArtOfDev.HtmlRenderer.Core.CssEngine internal sealed class TransitionProperty : ShorthandProperty { - internal static readonly IValueConverter ListConverter = new TimeBasedShorthandConverter( - 1, // duration index - 3, // delay index - AnimatableConverter.Option().For(PropertyNames.TransitionProperty), // 0 - TimeConverter.Option().For(PropertyNames.TransitionDuration), // 1 - TransitionConverter.Option().For(PropertyNames.TransitionTimingFunction), // 2 - TimeConverter.Option().For(PropertyNames.TransitionDelay) // 3 - ).FromList().OrDefault(); + // css-transitions-1 ยง2.5: none is only valid when the list has a single item + internal static readonly IValueConverter ListConverter = SingleTransition(AnimatableConverter).FromList() + .Or(SingleTransition(AnimatableConverter.OrNone())) + .OrDefault(); internal TransitionProperty() : base(PropertyNames.Transition) @@ -19,5 +15,17 @@ internal TransitionProperty() } internal override IValueConverter Converter => ListConverter; + + private static IValueConverter SingleTransition(IValueConverter propertyConverter) + { + return new TimeBasedShorthandConverter( + 1, // duration index + 3, // delay index + propertyConverter.Option().For(PropertyNames.TransitionProperty), // 0 + TimeConverter.Option().For(PropertyNames.TransitionDuration), // 1 + TransitionConverter.Option().For(PropertyNames.TransitionTimingFunction), // 2 + TimeConverter.Option().For(PropertyNames.TransitionDelay) // 3 + ); + } } -} \ No newline at end of file +} diff --git a/Source/HtmlRenderer/Core/CssEngine/ValueConverters/TimeBasedShorthandConverter.cs b/Source/HtmlRenderer/Core/CssEngine/ValueConverters/TimeBasedShorthandConverter.cs index 01504d044..f7f115080 100644 --- a/Source/HtmlRenderer/Core/CssEngine/ValueConverters/TimeBasedShorthandConverter.cs +++ b/Source/HtmlRenderer/Core/CssEngine/ValueConverters/TimeBasedShorthandConverter.cs @@ -63,9 +63,9 @@ public IPropertyValue Convert(IEnumerable value) if (targetIndex == i) { - // This is the right slot for this time value - actually consume it - TryMatchTime(list, out timeValue); - options[i] = timeValue; + // This is the right slot for this time value - actually consume it, + // through the slot's converter so the value only extracts for its longhand + options[i] = _converters[i].VaryStart(list); matched[i] = true; timeValuesFound++; foundMatch = true; diff --git a/Source/Test/HtmlRenderer.Test/Css/AnimationPropertyTests.cs b/Source/Test/HtmlRenderer.Test/Css/AnimationPropertyTests.cs index c9682c503..fff5ca47e 100644 --- a/Source/Test/HtmlRenderer.Test/Css/AnimationPropertyTests.cs +++ b/Source/Test/HtmlRenderer.Test/Css/AnimationPropertyTests.cs @@ -604,4 +604,49 @@ public void AnimationMultipleLegal() Assert.IsTrue(concrete.HasValue); Assert.AreEqual("0s ease 10 my-animation, linear 5 other-animation, 0s step-start 1s 10 yet-another", concrete.Value); } + + [TestMethod] + public void AnimationShorthandSetsEveryLonghand() + { + var style = CssConstructionFunctions.ParseDeclarations("animation: slide 1s ease-in 2s infinite alternate both paused"); + + Assert.AreEqual("slide", style.GetPropertyValue("animation-name")); + Assert.AreEqual("1s", style.GetPropertyValue("animation-duration")); + Assert.AreEqual("ease-in", style.GetPropertyValue("animation-timing-function")); + Assert.AreEqual("2s", style.GetPropertyValue("animation-delay")); + Assert.AreEqual("infinite", style.GetPropertyValue("animation-iteration-count")); + Assert.AreEqual("alternate", style.GetPropertyValue("animation-direction")); + Assert.AreEqual("both", style.GetPropertyValue("animation-fill-mode")); + Assert.AreEqual("paused", style.GetPropertyValue("animation-play-state")); + } + + [TestMethod] + public void AnimationShorthandWithOneTimeSetsOnlyTheDuration() + { + var style = CssConstructionFunctions.ParseDeclarations("animation: slide 1s"); + + Assert.AreEqual("slide", style.GetPropertyValue("animation-name")); + Assert.AreEqual("1s", style.GetPropertyValue("animation-duration")); + Assert.AreEqual("initial", style.GetPropertyValue("animation-delay")); + } + + [TestMethod] + public void AnimationShorthandGivesKeywordsToTheOtherLonghandsFirst() + { + var style = CssConstructionFunctions.ParseDeclarations("animation: 3s none backwards"); + + Assert.AreEqual("3s", style.GetPropertyValue("animation-duration")); + Assert.AreEqual("none", style.GetPropertyValue("animation-fill-mode")); + Assert.AreEqual("backwards", style.GetPropertyValue("animation-name")); + } + + [TestMethod] + public void AnimationShorthandListSetsTheLonghandsPerItem() + { + var style = CssConstructionFunctions.ParseDeclarations("animation: a 1s 2s, b 3s 4s"); + + Assert.AreEqual("a, b", style.GetPropertyValue("animation-name")); + Assert.AreEqual("1s, 3s", style.GetPropertyValue("animation-duration")); + Assert.AreEqual("2s, 4s", style.GetPropertyValue("animation-delay")); + } } diff --git a/Source/Test/HtmlRenderer.Test/Css/TransitionPropertyTests.cs b/Source/Test/HtmlRenderer.Test/Css/TransitionPropertyTests.cs index b9736f7f3..78f8e9b27 100644 --- a/Source/Test/HtmlRenderer.Test/Css/TransitionPropertyTests.cs +++ b/Source/Test/HtmlRenderer.Test/Css/TransitionPropertyTests.cs @@ -362,4 +362,39 @@ public void CssTransitionSecondsEaseAllHeightMsStepsWidthCubicBezierLegal() Assert.IsTrue(concrete.HasValue); Assert.AreEqual("all 1s ease, height 50ms step-start, width, cubic-bezier(0.2, 0.5, 1, 1)", concrete.Value); } + + [TestMethod] + public void CssTransitionShorthandWithOneTimeSetsOnlyTheDuration() + { + var style = CssConstructionFunctions.ParseDeclarations("transition: color 1s"); + + Assert.AreEqual("color", style.GetPropertyValue("transition-property")); + Assert.AreEqual("1s", style.GetPropertyValue("transition-duration")); + Assert.AreEqual("initial", style.GetPropertyValue("transition-delay")); + } + + [TestMethod] + public void CssTransitionShorthandAssignsTheSecondTimeToTheDelay() + { + var style = CssConstructionFunctions.ParseDeclarations("transition: 1s 2s"); + + Assert.AreEqual("1s", style.GetPropertyValue("transition-duration")); + Assert.AreEqual("2s", style.GetPropertyValue("transition-delay")); + } + + [TestMethod] + public void CssTransitionShorthandNoneSetsThePropertyToNone() + { + var style = CssConstructionFunctions.ParseDeclarations("transition: none"); + + Assert.AreEqual("none", style.GetPropertyValue("transition-property")); + } + + [TestMethod] + public void CssTransitionShorthandWithNoneInAListIsInvalid() + { + var style = CssConstructionFunctions.ParseDeclarations("transition: none, color 1s"); + + Assert.AreEqual(string.Empty, style.GetPropertyValue("transition")); + } }