Skip to content

Commit 8dd5f32

Browse files
committed
Allow opacity percentage values, code cleanup
1 parent 4a67cba commit 8dd5f32

11 files changed

Lines changed: 732 additions & 714 deletions

File tree

src/ExCSS.Tests/Cases.cs

Lines changed: 662 additions & 645 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Xunit;
2+
3+
namespace ExCSS.Tests.PropertyTests;
4+
public class OpacityPropertyTests : CssConstructionFunctions
5+
{
6+
[Fact]
7+
public void OpacityPercentLegal()
8+
{
9+
var snippet = "opacity: 50%";
10+
var property = ParseDeclaration(snippet);
11+
Assert.Equal("opacity", property.Name);
12+
Assert.False(property.IsImportant);
13+
Assert.IsType<OpacityProperty>(property);
14+
var concrete = (OpacityProperty)property;
15+
Assert.False(concrete.IsInherited);
16+
Assert.True(concrete.HasValue);
17+
Assert.Equal("50%", concrete.Value);
18+
}
19+
}

src/ExCSS/Conditions/AndCondition.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.IO;
22
using System.Linq;
33

4-
namespace ExCSS
4+
namespace ExCSS.Conditions
55
{
66
internal sealed class AndCondition : StylesheetNode, IConditionFunction
77
{
@@ -20,13 +20,9 @@ public override void ToCss(TextWriter writer, IStyleFormatter formatter)
2020
foreach (var condition in conditions)
2121
{
2222
if (first)
23-
{
2423
first = false;
25-
}
2624
else
27-
{
2825
writer.Write(" and ");
29-
}
3026

3127
condition.ToCss(writer, formatter);
3228
}

src/ExCSS/Extensions/ValueExtensions.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static string ToUri(this IEnumerable<Token> value)
4242
{
4343
var element = value.OnlyOrDefault();
4444

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

4747
return null;
4848
}
@@ -86,17 +86,47 @@ public static Length ToLength(this FontSize fontSize)
8686
{
8787
var element = value.OnlyOrDefault();
8888

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

9292
return null;
9393
}
9494

95+
public static Percent? ToPercentOrFraction(this IEnumerable<Token> value)
96+
{
97+
var enumerable = value as Token[] ?? value.ToArray();
98+
var percent = ToPercent(enumerable);
99+
100+
if (percent is not null)
101+
{
102+
return percent;
103+
}
104+
105+
var element = enumerable.OnlyOrDefault();
106+
107+
if (element is not { Type: TokenType.Percentage })
108+
{
109+
return null;
110+
}
111+
112+
var number = ((NumberToken)element).Value;
113+
114+
try
115+
{
116+
var percentage = number / 100;
117+
return new Percent(percentage);
118+
}
119+
catch
120+
{
121+
return null;
122+
}
123+
}
124+
95125
public static string ToCssString(this IEnumerable<Token> value)
96126
{
97127
var element = value.OnlyOrDefault();
98128

99-
if (element != null && element.Type == TokenType.String) return element.Data;
129+
if (element is { Type: TokenType.String }) return element.Data;
100130

101131
return null;
102132
}

src/ExCSS/Model/Converters.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static readonly IValueConverter
6767
public static readonly IValueConverter LengthOrPercentConverter =
6868
new StructValueConverter<Length>(ValueExtensions.ToDistance);
6969

70+
public static readonly IValueConverter PercentOrFractionConverter =
71+
new StructValueConverter<Percent>(ValueExtensions.ToPercentOrFraction);
72+
7073
public static readonly IValueConverter AngleNumberConverter =
7174
new StructValueConverter<Angle>(ValueExtensions.ToAngleNumber);
7275

@@ -319,6 +322,7 @@ public static readonly IValueConverter
319322
public static readonly IValueConverter AutoLengthConverter = LengthConverter.OrAuto();
320323
public static readonly IValueConverter OptionalLengthOrPercentConverter = LengthOrPercentConverter.OrNone();
321324
public static readonly IValueConverter AutoLengthOrPercentConverter = LengthOrPercentConverter.OrAuto();
325+
public static readonly IValueConverter OptionalPercentOrFractionConverter = PercentOrFractionConverter.OrDefault(1f);
322326

323327
public static readonly IValueConverter FontSizeConverter =
324328
LengthOrPercentConverter.Or(Map.FontSizes.ToConverter());
@@ -460,11 +464,6 @@ public static IValueConverter Toggle(string on, string off)
460464
return Assign(on, true).Or(off, false);
461465
}
462466

463-
//public static IValueConverter WithFallback<T>(T fallbackValue) where T : struct, IFormattable
464-
//{
465-
// return new StructValueConverter<T>(_ => fallbackValue);
466-
//}
467-
468467
#endregion
469468

470469
#region Order / Unordered

src/ExCSS/Model/ParserExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using ExCSS.Conditions;
34

45
namespace ExCSS
56
{

src/ExCSS/Model/ValueBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void Apply(Token token)
4444
Add(token);
4545
break;
4646
case TokenType.Whitespace:
47-
if (_values.Count > 0 && IsSlash(_values[_values.Count - 1]) == false)
47+
if (_values.Count > 0 && IsSlash(_values[^1]) == false)
4848
_buffer = token;
4949
break;
5050
case TokenType.Dimension:

src/ExCSS/Parser/SelectorConstructor.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,34 +652,34 @@ public override ISelector Produce()
652652

653653
private sealed class LangFunctionState : FunctionState
654654
{
655-
private bool valid = true;
656-
private string value ;
655+
private bool _valid = true;
656+
private string _value;
657657

658658
protected override bool OnToken(Token token)
659659
{
660660
if (token.Type == TokenType.Ident)
661661
{
662-
value = token.Data;
662+
_value = token.Data;
663663
}
664664
else if (token.Type == TokenType.RoundBracketClose)
665665
{
666666
return true;
667667
}
668668
else if (token.Type != TokenType.Whitespace)
669669
{
670-
valid = false;
670+
_valid = false;
671671
}
672672

673673
return false;
674674
}
675675

676676
public override ISelector Produce()
677677
{
678-
if (!valid || value == null)
678+
if (!_valid || _value == null)
679679
{
680680
return null;
681681
}
682-
var code = PseudoClassNames.Lang.StylesheetFunction(value);
682+
var code = PseudoClassNames.Lang.StylesheetFunction(_value);
683683
return PseudoClassSelector.Create(code);
684684

685685
}
@@ -784,7 +784,7 @@ public ChildFunctionState(SelectorConstructor parent, bool withOptionalSelector
784784

785785
public override ISelector Produce()
786786
{
787-
var invalid = !_valid || _nested != null && !_nested.IsValid;
787+
var invalid = !_valid || _nested is { IsValid: false };
788788
var sel = _nested?.ToPool() ?? AllSelector.Create();
789789
if (invalid)
790790
{

src/ExCSS/Parser/StylesheetComposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ public TextPosition FillDeclarations(StyleDeclaration style)
583583
// times in the same style declaration.
584584
// Example: "background-color:green !important; text-align:center; background-color:yellow;";
585585
// In this example even though background-color yellow is defined last, the previous value
586-
// of green should be he one exposed given it is tagged as important.
586+
// of green should be the one exposed given it is tagged as important.
587587
// ------------------------------------------------------------------------------------------
588588
// Only set this property if one of the following conditions is true:
589589
// a) It was not previously added or...

src/ExCSS/Rules/MarginRule.cs

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)