Skip to content

Commit

Permalink
Allow opacity percentage values, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerBrinks committed Sep 23, 2024
1 parent 4a67cba commit 8dd5f32
Show file tree
Hide file tree
Showing 11 changed files with 732 additions and 714 deletions.
1,307 changes: 662 additions & 645 deletions src/ExCSS.Tests/Cases.cs

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/ExCSS.Tests/PropertyTests/OpacityPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Xunit;

namespace ExCSS.Tests.PropertyTests;
public class OpacityPropertyTests : CssConstructionFunctions
{
[Fact]
public void OpacityPercentLegal()
{
var snippet = "opacity: 50%";
var property = ParseDeclaration(snippet);
Assert.Equal("opacity", property.Name);
Assert.False(property.IsImportant);
Assert.IsType<OpacityProperty>(property);
var concrete = (OpacityProperty)property;
Assert.False(concrete.IsInherited);
Assert.True(concrete.HasValue);
Assert.Equal("50%", concrete.Value);
}
}
6 changes: 1 addition & 5 deletions src/ExCSS/Conditions/AndCondition.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using System.Linq;

namespace ExCSS
namespace ExCSS.Conditions
{
internal sealed class AndCondition : StylesheetNode, IConditionFunction
{
Expand All @@ -20,13 +20,9 @@ public override void ToCss(TextWriter writer, IStyleFormatter formatter)
foreach (var condition in conditions)
{
if (first)
{
first = false;
}
else
{
writer.Write(" and ");
}

condition.ToCss(writer, formatter);
}
Expand Down
38 changes: 34 additions & 4 deletions src/ExCSS/Extensions/ValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static string ToUri(this IEnumerable<Token> value)
{
var element = value.OnlyOrDefault();

if (element != null && element.Type == TokenType.Url) return element.Data;
if (element is { Type: TokenType.Url }) return element.Data;

return null;
}
Expand Down Expand Up @@ -86,17 +86,47 @@ public static Length ToLength(this FontSize fontSize)
{
var element = value.OnlyOrDefault();

if (element != null && element.Type == TokenType.Percentage)
return new Percent(((UnitToken) element).Value);
if (element is { Type: TokenType.Percentage })
return new Percent(((UnitToken)element).Value);

return null;
}

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

if (percent is not null)
{
return percent;
}

var element = enumerable.OnlyOrDefault();

if (element is not { Type: TokenType.Percentage })
{
return null;
}

var number = ((NumberToken)element).Value;

try
{
var percentage = number / 100;
return new Percent(percentage);
}
catch
{
return null;
}
}

public static string ToCssString(this IEnumerable<Token> value)
{
var element = value.OnlyOrDefault();

if (element != null && element.Type == TokenType.String) return element.Data;
if (element is { Type: TokenType.String }) return element.Data;

return null;
}
Expand Down
9 changes: 4 additions & 5 deletions src/ExCSS/Model/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public static readonly IValueConverter
public static readonly IValueConverter LengthOrPercentConverter =
new StructValueConverter<Length>(ValueExtensions.ToDistance);

public static readonly IValueConverter PercentOrFractionConverter =
new StructValueConverter<Percent>(ValueExtensions.ToPercentOrFraction);

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

Expand Down Expand Up @@ -319,6 +322,7 @@ public static readonly IValueConverter
public static readonly IValueConverter AutoLengthConverter = LengthConverter.OrAuto();
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 FontSizeConverter =
LengthOrPercentConverter.Or(Map.FontSizes.ToConverter());
Expand Down Expand Up @@ -460,11 +464,6 @@ public static IValueConverter Toggle(string on, string off)
return Assign(on, true).Or(off, false);
}

//public static IValueConverter WithFallback<T>(T fallbackValue) where T : struct, IFormattable
//{
// return new StructValueConverter<T>(_ => fallbackValue);
//}

#endregion

#region Order / Unordered
Expand Down
1 change: 1 addition & 0 deletions src/ExCSS/Model/ParserExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using ExCSS.Conditions;

namespace ExCSS
{
Expand Down
2 changes: 1 addition & 1 deletion src/ExCSS/Model/ValueBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void Apply(Token token)
Add(token);
break;
case TokenType.Whitespace:
if (_values.Count > 0 && IsSlash(_values[_values.Count - 1]) == false)
if (_values.Count > 0 && IsSlash(_values[^1]) == false)

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Predefined type 'System.Index' is not defined or imported

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Missing compiler required member 'System.Index..ctor'

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Predefined type 'System.Index' is not defined or imported

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Missing compiler required member 'System.Index..ctor'

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Predefined type 'System.Index' is not defined or imported

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Missing compiler required member 'System.Index..ctor'

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Predefined type 'System.Index' is not defined or imported

Check failure on line 47 in src/ExCSS/Model/ValueBuilder.cs

View workflow job for this annotation

GitHub Actions / continuous-integration / build-and-sign

Missing compiler required member 'System.Index..ctor'
_buffer = token;
break;
case TokenType.Dimension:
Expand Down
14 changes: 7 additions & 7 deletions src/ExCSS/Parser/SelectorConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,34 +652,34 @@ public override ISelector Produce()

private sealed class LangFunctionState : FunctionState
{
private bool valid = true;
private string value ;
private bool _valid = true;
private string _value;

protected override bool OnToken(Token token)
{
if (token.Type == TokenType.Ident)
{
value = token.Data;
_value = token.Data;
}
else if (token.Type == TokenType.RoundBracketClose)
{
return true;
}
else if (token.Type != TokenType.Whitespace)
{
valid = false;
_valid = false;
}

return false;
}

public override ISelector Produce()
{
if (!valid || value == null)
if (!_valid || _value == null)
{
return null;
}
var code = PseudoClassNames.Lang.StylesheetFunction(value);
var code = PseudoClassNames.Lang.StylesheetFunction(_value);
return PseudoClassSelector.Create(code);

}
Expand Down Expand Up @@ -784,7 +784,7 @@ public ChildFunctionState(SelectorConstructor parent, bool withOptionalSelector

public override ISelector Produce()
{
var invalid = !_valid || _nested != null && !_nested.IsValid;
var invalid = !_valid || _nested is { IsValid: false };
var sel = _nested?.ToPool() ?? AllSelector.Create();
if (invalid)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ExCSS/Parser/StylesheetComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public TextPosition FillDeclarations(StyleDeclaration style)
// times in the same style declaration.
// Example: "background-color:green !important; text-align:center; background-color:yellow;";
// In this example even though background-color yellow is defined last, the previous value
// of green should be he one exposed given it is tagged as important.
// of green should be the one exposed given it is tagged as important.
// ------------------------------------------------------------------------------------------
// Only set this property if one of the following conditions is true:
// a) It was not previously added or...
Expand Down
43 changes: 0 additions & 43 deletions src/ExCSS/Rules/MarginRule.cs

This file was deleted.

5 changes: 2 additions & 3 deletions src/ExCSS/StyleProperties/Visibility/OpacityProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
{
internal sealed class OpacityProperty : Property
{
private static readonly IValueConverter StyleConverter = Converters.NumberConverter.OrDefault(1f);
private static readonly IValueConverter StyleConverter = Converters.OptionalPercentOrFractionConverter;

internal OpacityProperty()
: base(PropertyNames.Opacity, PropertyFlags.Animatable)
internal OpacityProperty() : base(PropertyNames.Opacity, PropertyFlags.Animatable)
{
}

Expand Down

0 comments on commit 8dd5f32

Please sign in to comment.