Skip to content

Refactor opacity parsing and add percent/number converters #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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);
}
}
}