Skip to content

Commit

Permalink
refactor: extract xpath visitor html5 namespace handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
flavorjones committed Jan 12, 2022
1 parent 8f80cfd commit a1c34b3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ def visit_conditional_selector(node)
end

def visit_element_name(node)
if @doctype == DoctypeConfig::HTML5 && node.value.first != "*"
# if there is already a namespace, use it as normal
return node.value.first if node.value.first.include?(":")

if @doctype == DoctypeConfig::HTML5 && html5_element_name_needs_namespace_handling(node)
# HTML5 has namespaces that should be ignored in CSS queries
# https://github.com/sparklemotion/nokogiri/issues/2376
if @builtins == BuiltinsConfig::ALWAYS || (@builtins == BuiltinsConfig::OPTIMAL && Nokogiri.uses_libxml?)
Expand All @@ -279,6 +276,13 @@ def accept(node)

private

def html5_element_name_needs_namespace_handling(node)
# if this is the wildcard selector "*", use it as normal
node.value.first != "*" &&
# if there is already a namespace (i.e., it is a prefixed QName), use it as normal
!node.value.first.include?(":")
end

def nth(node, options = {})
raise ArgumentError, "expected an+b node to contain 4 tokens, but is #{node.value.inspect}" unless node.value.size == 4

Expand Down

0 comments on commit a1c34b3

Please sign in to comment.