Skip to content

Commit

Permalink
fix: strip spaces from the operator when translating nth css to xpath
Browse files Browse the repository at this point in the history
  • Loading branch information
fusion2004 committed Nov 10, 2023
1 parent cc557dd commit ffe650f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def nth(node, options = {})
end

def read_a_and_positive_b(values)
op = values[2]
op = values[2].strip
if op == "+"
a = values[0].to_i
b = values[3].to_i
Expand Down
1 change: 1 addition & 0 deletions test/css/test_css_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def assert_result_rows(intarray, result, word = "row")
assert_result_rows([5], subject.search("table//tr:nth-child(5)"))
assert_result_rows([1, 3], subject.search("div/h1.c:nth-child(2)"), "header")
assert_result_rows([3, 4], subject.search("div/i.b:nth-child(2n+1)"), "italic")
assert_result_rows([3, 4], subject.search("div/i.b:nth-child(2n + 1)"), "italic")
end

it "selects first_of_type" do
Expand Down
15 changes: 15 additions & 0 deletions test/css/test_tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ def test_scan_nth
@scanner,
)

@scanner.scan("x:nth-child(5n + 3)")
assert_tokens(
[
[:IDENT, "x"],
[":", ":"],
[:FUNCTION, "nth-child("],
[:NUMBER, "5"],
[:IDENT, "n"],
[:PLUS, " + "],
[:NUMBER, "3"],
[:RPAREN, ")"],
],
@scanner,
)

@scanner.scan("x:nth-child(-1n+3)")
assert_tokens(
[
Expand Down
2 changes: 2 additions & 0 deletions test/css/test_xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,12 @@ def assert_xpath(expecteds, asts)
it ":nth(an+b)" do
assert_xpath("//a[(position() mod 2)=0]", parser.parse("a:nth-of-type(2n)"))
assert_xpath("//a[(position()>=1) and (((position()-1) mod 2)=0)]", parser.parse("a:nth-of-type(2n+1)"))
assert_xpath("//a[(position()>=1) and (((position()-1) mod 2)=0)]", parser.parse("a:nth-of-type(2n + 1)"))
assert_xpath("//a[(position() mod 2)=0]", parser.parse("a:nth-of-type(even)"))
assert_xpath("//a[(position()>=1) and (((position()-1) mod 2)=0)]", parser.parse("a:nth-of-type(odd)"))
assert_xpath("//a[(position()>=3) and (((position()-3) mod 4)=0)]", parser.parse("a:nth-of-type(4n+3)"))
assert_xpath("//a[position()<=3]", parser.parse("a:nth-of-type(-1n+3)"))
assert_xpath("//a[position()<=3]", parser.parse("a:nth-of-type(-1n + 3)"))
assert_xpath("//a[position()<=3]", parser.parse("a:nth-of-type(-n+3)"))
assert_xpath("//a[position()>=3]", parser.parse("a:nth-of-type(1n+3)"))
assert_xpath("//a[position()>=3]", parser.parse("a:nth-of-type(n+3)"))
Expand Down

0 comments on commit ffe650f

Please sign in to comment.