Skip to content

Commit

Permalink
Merge pull request #181 from cwall-dev/change/opacitypercentageoptimi…
Browse files Browse the repository at this point in the history
…zation

Refactor opacity parsing and add percent/number converters
  • Loading branch information
TylerBrinks authored Oct 14, 2024
2 parents d0a5763 + 15bf42d commit 120ddee
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/ExCSS.Tests/PropertyTests/OpacityPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ public void OpacityVarianceTests()
var result = (OpacityProperty)property;
Assert.Equal("50%", result.Value);

property = ParseDeclaration("opacity: 0.4");
result = (OpacityProperty)property;
Assert.Equal("0.4", result.Value);

property = ParseDeclaration("opacity: .50");
result = (OpacityProperty)property;
Assert.Equal("50%", result.Value);
Assert.Equal("0.5", result.Value);

property = ParseDeclaration("opacity: 1");
result = (OpacityProperty)property;
Assert.Equal("100%", result.Value);
Assert.Equal("1", result.Value);

property = ParseDeclaration("opacity: 0");
result = (OpacityProperty)property;
Assert.Equal("0%", result.Value);
Assert.Equal("0", result.Value);
}
}
26 changes: 26 additions & 0 deletions src/ExCSS/Extensions/ValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,32 @@ public static Length ToLength(this FontSize fontSize)
}
}

public static Number? ToPercentOrNumber(this IEnumerable<Token> value)
{
var enumerable = value as Token[] ?? value.ToArray();
var percent = ToPercent(enumerable);

if (percent is not null)
{
return new Number(percent.Value.Value, Number.Unit.Percent);
}

var element = value.OnlyOrDefault();
if (element is not NumberToken token)
{
return null;
}

try
{
return new Number(token.Value, Number.Unit.Float);
}
catch
{
return null;
}
}

public static string ToCssString(this IEnumerable<Token> value)
{
var element = value.OnlyOrDefault();
Expand Down
4 changes: 4 additions & 0 deletions src/ExCSS/Model/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public static readonly IValueConverter
public static readonly IValueConverter PercentOrFractionConverter =
new StructValueConverter<Percent>(ValueExtensions.ToPercentOrFraction);

public static readonly IValueConverter PercentOrNumberConverter =
new StructValueConverter<Number>(ValueExtensions.ToPercentOrNumber);

public static readonly IValueConverter AngleNumberConverter =
new StructValueConverter<Angle>(ValueExtensions.ToAngleNumber);

Expand Down Expand Up @@ -323,6 +326,7 @@ public static readonly IValueConverter
public static readonly IValueConverter OptionalLengthOrPercentConverter = LengthOrPercentConverter.OrNone();
public static readonly IValueConverter AutoLengthOrPercentConverter = LengthOrPercentConverter.OrAuto();
public static readonly IValueConverter OptionalPercentOrFractionConverter = PercentOrFractionConverter.OrDefault(1f);
public static readonly IValueConverter OptionalPercentOrNumberConverter = PercentOrNumberConverter.OrDefault(1f);

public static readonly IValueConverter FontSizeConverter =
LengthOrPercentConverter.Or(Map.FontSizes.ToConverter());
Expand Down
2 changes: 1 addition & 1 deletion src/ExCSS/StyleProperties/Visibility/OpacityProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
internal sealed class OpacityProperty : Property
{
private static readonly IValueConverter StyleConverter = Converters.OptionalPercentOrFractionConverter;
private static readonly IValueConverter StyleConverter = Converters.OptionalPercentOrNumberConverter;

internal OpacityProperty() : base(PropertyNames.Opacity, PropertyFlags.Animatable)
{
Expand Down
7 changes: 4 additions & 3 deletions src/ExCSS/Values/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public bool Equals(Number other)
public enum Unit : byte
{
Integer,
Float
Float,
Percent
}

public static bool operator ==(Number a, Number b)
Expand All @@ -90,12 +91,12 @@ public override int GetHashCode()

public override string ToString()
{
return Value.ToString();
return Value.ToString() + (_unit == Unit.Percent ? "%" : string.Empty);
}

public string ToString(string format, IFormatProvider formatProvider)
{
return Value.ToString(format, formatProvider);
return Value.ToString(format, formatProvider) + (_unit == Unit.Percent ? "%" : string.Empty);
}
}
}

0 comments on commit 120ddee

Please sign in to comment.