Skip to content

Commit

Permalink
css: handle relative selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jun 19, 2024
1 parent 4e8a877 commit a0cc23d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/nokogiri/css/selectors/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ def xpath(ast)
# else
# visitor.prefix
# end
prefix = if RelativeSelector === ast
"."
else
self.prefix
end
prefix + accept(ast)
end

Expand All @@ -27,6 +32,10 @@ def visit_complex_selector(node)
"#{accept(node.left)}#{accept(node.combinator)}#{accept(node.right)}"
end

def visit_relative_selector(node)
"#{accept(node.combinator)}#{accept(node.complex_selector)}"
end

def visit_compound_selector(node)
type_selector = node.type.nil? ? "*" : accept(node.type)

Expand Down Expand Up @@ -142,8 +151,13 @@ def visit_pseudo_class_function(node)
"position()>3" # TODO: OBVIOUSLY WRONG
# when "only-child"
# # TODO
# when "has"
# # TODO
when "has"
case node.arguments.first
when RelativeSelector
".#{accept(node.arguments.first)}"
else
".//#{accept(node.arguments.first)}"
end
else # custom xpath function call
# TODO
"<pseudo-class-function>"
Expand Down

0 comments on commit a0cc23d

Please sign in to comment.