Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/AngleSharp.Css.Tests/Values/Calc.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#nullable disable
namespace AngleSharp.Css.Tests.Values
{
using AngleSharp.Css.Dom;
using AngleSharp.Css.Parser;
using AngleSharp.Css.Values;
using AngleSharp.Dom;
using AngleSharp.Text;
using NUnit.Framework;
using System;
using static CssConstructionFunctions;

[TestFixture]
Expand Down Expand Up @@ -100,5 +105,47 @@ public void IntegerCanBeUsedWithCalc()
Assert.IsTrue(property.HasValue);
Assert.AreEqual("calc(21 + 5 - 4 * 2)", property.Value);
}

[Test]
public void CalcAdditionOfLengthsIsComputed()
{
var document = ParseDocument("<style>p { width: calc(100px + 20px) }</style><p></p>");
var style = document.QuerySelector("p").ComputeCurrentStyle();
Assert.AreEqual("120px", style.GetWidth());
}

[Test]
public void CalcSubtractionOfLengthsIsComputed()
{
var document = ParseDocument("<style>p { width: calc(100px - 20px) }</style><p></p>");
var style = document.QuerySelector("p").ComputeCurrentStyle();
Assert.AreEqual("80px", style.GetWidth());
}

[Test]
public void CalcAdditionOfTimesIsComputed()
{
var document = ParseDocument("<style>p { transition-duration: calc(30ms + 20ms) }</style><p></p>");
var style = document.QuerySelector("p").ComputeCurrentStyle();
Assert.AreEqual("50ms", style.GetTransitionDuration());
}

[TestCase(typeof(CssAngleValue))]
[TestCase(typeof(CssFrequencyValue))]
[TestCase(typeof(CssIntegerValue))]
[TestCase(typeof(CssLengthValue))]
[TestCase(typeof(CssNumberValue))]
[TestCase(typeof(CssPercentageValue))]
[TestCase(typeof(CssResolutionValue))]
[TestCase(typeof(CssTimeValue))]
public void MetricValueCanBeCreatedWithAnotherValue(Type type)
{
var template = (ICssMetricValue)Activator.CreateInstance(type, 2.0);
var result = template.WithValue(5.0);

Assert.IsInstanceOf(type, result);
Assert.AreEqual(5.0, ((ICssMetricValue)result).Value);
Assert.AreEqual(template.UnitString, ((ICssMetricValue)result).UnitString);
}
}
}
39 changes: 39 additions & 0 deletions src/AngleSharp.Css/Extensions/CssMetricValueExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#nullable disable
namespace AngleSharp.Css.Values
{
using AngleSharp.Css.Dom;
using System;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

/// <summary>
/// A set of helpers for dealing with metric values.
/// </summary>
static class CssMetricValueExtensions
{
/// <summary>
/// Creates a new metric value of the same type as the given template, but
/// carrying the provided value.
/// </summary>
/// <param name="template">The value determining the type to create.</param>
/// <param name="value">The value to use for the created instance.</param>
/// <returns>The newly created metric value.</returns>
#if NET5_0_OR_GREATER
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssAngleValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssFrequencyValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssIntegerValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssLengthValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssNumberValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssPercentageValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssResolutionValue))]
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(CssTimeValue))]
[UnconditionalSuppressMessage("Trimming", "IL2072",
Justification = "The constructors of the metric values shipped with AngleSharp.Css are preserved via " +
"DynamicDependency. Metric values implemented outside of AngleSharp.Css have to preserve their " +
"public constructor taking a single Double themselves, e.g., via DynamicDependency.")]
#endif
public static ICssValue WithValue(this ICssMetricValue template, Double value) =>
(ICssValue)Activator.CreateInstance(template.GetType(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ICssValue ICssValue.Compute(ICssComputeContext context)
if (left is ICssMetricValue x && right is ICssMetricValue y && x.UnitString == y.UnitString)
{
var result = x.Value + y.Value;
return (ICssValue)Activator.CreateInstance(x.GetType(), result);
return x.WithValue(result);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ICssValue ICssValue.Compute(ICssComputeContext context)
if (left is ICssMetricValue x && right is ICssMetricValue y && x.UnitString == y.UnitString)
{
var result = x.Value / y.Value;
return (ICssValue)Activator.CreateInstance(x.GetType(), result);
return x.WithValue(result);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ICssValue ICssValue.Compute(ICssComputeContext context)
if (left is ICssMetricValue x && right is ICssMetricValue y && x.UnitString == y.UnitString)
{
var result = x.Value * y.Value;
return (ICssValue)Activator.CreateInstance(x.GetType(), result);
return x.WithValue(result);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ICssValue ICssValue.Compute(ICssComputeContext context)
if (left is ICssMetricValue x && right is ICssMetricValue y && x.UnitString == y.UnitString)
{
var result = x.Value - y.Value;
return (ICssValue)Activator.CreateInstance(x.GetType(), result);
return x.WithValue(result);
}

return null;
Expand Down
Loading