Skip to content

Commit b5f0783

Browse files
author
Gideon de Swardt
committed
Make selectors accessors public
This commit modifies the access level of internal accessors on the selector instance, making them public. This change allows for better integration and utilization of the selector in LINQ queries when the package is used within other applications.
1 parent de5c1e1 commit b5f0783

16 files changed

Lines changed: 85 additions & 15 deletions

src/ExCSS.Tests/ExCSS.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@
3030
<Folder Include="Properties\" />
3131
</ItemGroup>
3232

33+
<ItemGroup>
34+
<EmbeddedResource Include="bootstrap.css" />
35+
</ItemGroup>
36+
3337
</Project>

src/ExCSS.Tests/Selectors.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Xunit;
5+
6+
namespace ExCSS.Tests;
7+
8+
public class SelectorsTests
9+
{
10+
[Fact]
11+
public async Task FindAllStyleRulesForAnElement()
12+
{
13+
// Arrange
14+
var sheet = await ParseBootstrapAsync();
15+
16+
// Act
17+
var list = sheet.StyleRules
18+
.Where(r =>
19+
r.Selector is SimpleSelector { Text: "input" }
20+
|| (r.Selector is CompoundSelector selector && selector.First() is SimpleSelector { Text: "input" })
21+
);
22+
23+
// Assert
24+
Assert.Equal(6, list.Count());
25+
}
26+
27+
[Fact]
28+
public async Task FindAllStyleRulesElementsWithMoreThanTwoCompoundSelectors()
29+
{
30+
// Arrange
31+
var sheet = await ParseBootstrapAsync();
32+
33+
// Act
34+
var list = sheet.StyleRules
35+
.Where(r => (r.Selector as CompoundSelector)?.Length > 2);
36+
37+
// Assert
38+
Assert.Equal(2, list.Count());
39+
}
40+
41+
[Fact]
42+
public async Task FindAllStyleRulesWithCompoundSelector()
43+
{
44+
// Arrange
45+
var sheet = await ParseBootstrapAsync();
46+
47+
// Act
48+
var list = sheet.StyleRules
49+
.Where(r => r.Selector is CompoundSelector selector && selector.Last().Text.StartsWith("["));
50+
51+
// Assert
52+
Assert.Equal(7, list.Count());
53+
}
54+
55+
private async Task<Stylesheet> ParseBootstrapAsync()
56+
{
57+
await using var stream = GetStream("bootstrap.css");
58+
var parser = new StylesheetParser();
59+
return await parser.ParseAsync(stream);
60+
}
61+
62+
private Stream GetStream(string fileName)
63+
{
64+
var fullyQualifiedName = $"{GetType().Namespace}.{fileName}";
65+
return GetType().Assembly.GetManifestResourceStream(fullyQualifiedName);
66+
}
67+
}

src/ExCSS/Model/Combinator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ExCSS
22
{
3-
internal abstract class Combinator
3+
public abstract class Combinator
44
{
55
public static readonly Combinator Child = new ChildCombinator();
66
public static readonly Combinator Deep = new DeepCombinator();

src/ExCSS/Selectors/ChildSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ExCSS
44
{
5-
internal abstract class ChildSelector : StylesheetNode, ISelector
5+
public abstract class ChildSelector : StylesheetNode, ISelector
66
{
77
private readonly string _name;
88
protected int Step;

src/ExCSS/Selectors/ComplexSelector.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace ExCSS
55
{
6-
internal sealed class ComplexSelector : StylesheetNode, ISelector
6+
public sealed class ComplexSelector : StylesheetNode, ISelector
77
{
88
private readonly List<CombinatorSelector> _selectors;
99

@@ -18,7 +18,6 @@ private struct CombinatorSelector
1818
public ISelector Selector;
1919
}
2020

21-
2221
public string Text => this.ToCss();
2322
public int Length => _selectors.Count;
2423
public bool IsReady { get; private set; }

src/ExCSS/Selectors/CompoundSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ExCSS
44
{
5-
internal sealed class CompoundSelector : Selectors, ISelector
5+
public sealed class CompoundSelector : Selectors, ISelector
66
{
77
public override void ToCss(TextWriter writer, IStyleFormatter formatter)
88
{

src/ExCSS/Selectors/FirstChildSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace ExCSS
22

33
{
4-
internal sealed class FirstChildSelector : ChildSelector
4+
public sealed class FirstChildSelector : ChildSelector
55
{
66
public FirstChildSelector()
77
: base(PseudoClassNames.NthChild)

src/ExCSS/Selectors/FirstColumnSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ExCSS
22
{
3-
internal sealed class FirstColumnSelector : ChildSelector
3+
public sealed class FirstColumnSelector : ChildSelector
44
{
55
public FirstColumnSelector()
66
: base(PseudoClassNames.NthColumn)

src/ExCSS/Selectors/FirstTypeSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ExCSS
22
{
3-
internal sealed class FirstTypeSelector : ChildSelector
3+
public sealed class FirstTypeSelector : ChildSelector
44
{
55
public FirstTypeSelector()
66
: base(PseudoClassNames.NthOfType)

src/ExCSS/Selectors/LastChildSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace ExCSS
22
{
3-
internal sealed class LastChildSelector : ChildSelector
3+
public sealed class LastChildSelector : ChildSelector
44
{
55
public LastChildSelector()
66
: base(PseudoClassNames.NthLastChild)

0 commit comments

Comments
 (0)