Skip to content
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
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ At the moment, supports the following CSS selector features:
* [:last-of-type](https://www.w3.org/TR/selectors-3/#last-of-type-pseudo)
* [:not(...)](https://www.w3.org/TR/selectors-3/#negation)
* [:nth-of-type(n)](https://www.w3.org/TR/selectors-3/#nth-of-type-pseudo)
* [:nth-child(n)](https://www.w3.org/TR/selectors-3/#nth-child-pseudo): simplified syntax with just a position only
* [:has(...)](https://www.w3.org/TR/selectors-4/#has-pseudo)

And all [combinators](https://www.w3.org/TR/selectors-3/#combinators)
Expand Down
6 changes: 6 additions & 0 deletions src/Css/Css.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ public override void Append(StringBuilder builder)
=> builder.Append("[").Append(Position).Append("]");
}

record NthChildSelector(string Position) : SimpleSelector
{
public override void Append(StringBuilder builder)
=> builder.Append("[(count(preceding-sibling::*)+1)=").Append(Position).Append("]");
}

enum Combinator
{
None,
Expand Down
2 changes: 2 additions & 0 deletions src/Css/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ from identifier in Span.EqualTo("checked")
.Or(Span.EqualTo("last-of-type").Try())
.Or(Span.EqualTo("not").Try())
.Or(Span.EqualTo("nth-of-type").Try())
.Or(Span.EqualTo("nth-child").Try())
.Or(Span.EqualTo("has"))
from argument in PseudoArgumentSelector.OptionalOrDefault()
select identifier.ToStringValue() switch
Expand All @@ -118,6 +119,7 @@ from argument in PseudoArgumentSelector.OptionalOrDefault()
"not" => new NegationSelector(argument),
"nth-of-type" => (SimpleSelector)argument,
"has" => new HasSelector(argument),
string id when id == "nth-child" && argument is PositionSelector position => new NthChildSelector(position.Position),
_ => throw new NotSupportedException()
})
.Named("pseudo selector");
Expand Down
7 changes: 6 additions & 1 deletion src/Tests/CssParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,15 @@ public void CanParseSelector1()
[Fact]
public void CanParseSelector2()
{
var selector = Parser.Selector.Parse("foo > bar");
var selector = Parser.Selector.Parse("foo:nth-child(2)");
var xpath = selector.ToXPath();
}

[InlineData("foo\\+bar", "foo+bar")]
[InlineData("foo_bar", "foo_bar")]
[InlineData("foo__bar", "foo__bar")]
[InlineData("_foo-bar", "_foo-bar")]
[InlineData("-foo_bar", "-foo_bar")]
[Theory]
public void ParseIdentifier(string expression, string identifier)
=> Assert.Equal(identifier, Parser.Identifier.Parse(expression));
Expand Down
2 changes: 2 additions & 0 deletions src/Tests/XPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void ToXPath(string css, string xpath)
[InlineData("span:not([text()='4']),div[role]", "Warning 1 2 5 Standard File Archivo Edit Footer")]
[InlineData(".item__hiden-content", "Archivo")]
[InlineData("body > div > span", "1 2 4")]
[InlineData("body > div:nth-of-type(3)", "Standard")]
[InlineData("body > span:nth-child(5)", "5")]
[Theory]
public void EvaluatePageHtml(string expression, string expected)
{
Expand Down