Skip to content

Commit

Permalink
css: fix nth-of-type xpath predicate
Browse files Browse the repository at this point in the history
Closes #3238
  • Loading branch information
flavorjones committed Jun 19, 2024
1 parent a0cc23d commit 1a52355
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/nokogiri/css/selectors/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ def visit_compound_selector(node)
subclasses_selector = if node.subclasses.nil? || node.subclasses.empty?
EMPTY_STRING
else
"[" + node.subclasses.map { |subclass| accept(subclass) }.join(" and ") + "]"
filters = ["["]
node.subclasses.each_with_index do |subclass, j|
filters << " and " if j > 0
filters << accept(subclass)
end
filters << "]"
filters.join
end

type_selector + subclasses_selector
Expand Down
8 changes: 4 additions & 4 deletions test/css/test_xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,15 +315,15 @@ def visit_pseudo_class_aaron(node)
assert_xpath("//a[position()=1]", "a:first-of-type()")
assert_xpath("//a[position()=1]", "a:first-of-type") # no parens
assert_xpath(
"//a[contains(concat(' ',normalize-space(@class),' '),' b ')][position()=1]",
"//a[contains(concat(' ',normalize-space(@class),' '),' b ') and position()=1]",
"a.b:first-of-type",
) # no parens
end

it ":nth-of-type" do
assert_xpath("//a[position()=99]", "a:nth-of-type(99)")
assert_xpath(
"//a[contains(concat(' ',normalize-space(@class),' '),' b ')][position()=99]",
"//a[contains(concat(' ',normalize-space(@class),' '),' b ') and position()=99]",
"a.b:nth-of-type(99)",
)
end
Expand All @@ -332,7 +332,7 @@ def visit_pseudo_class_aaron(node)
assert_xpath("//a[position()=last()]", "a:last-of-type()")
assert_xpath("//a[position()=last()]", "a:last-of-type") # no parens
assert_xpath(
"//a[contains(concat(' ',normalize-space(@class),' '),' b ')][position()=last()]",
"//a[contains(concat(' ',normalize-space(@class),' '),' b ') and position()=last()]",
"a.b:last-of-type",
) # no parens
end
Expand All @@ -341,7 +341,7 @@ def visit_pseudo_class_aaron(node)
assert_xpath("//a[position()=last()]", "a:nth-last-of-type(1)")
assert_xpath("//a[position()=last()-98]", "a:nth-last-of-type(99)")
assert_xpath(
"//a[contains(concat(' ',normalize-space(@class),' '),' b ')][position()=last()-98]",
"//a[contains(concat(' ',normalize-space(@class),' '),' b ') and position()=last()-98]",
"a.b:nth-last-of-type(99)",
)
end
Expand Down

0 comments on commit 1a52355

Please sign in to comment.