Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support prefixless CSS selectors for +, ~, and > (Issue #621) #623

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions lib/nokogiri/css/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Nokogiri
module CSS
class Node
ALLOW_COMBINATOR_ON_SELF = [:DIRECT_ADJACENT_SELECTOR, :FOLLOWING_SELECTOR, :CHILD_SELECTOR]

# Get the type of this node
attr_accessor :type
# Get the value of this node
Expand All @@ -21,6 +23,7 @@ def accept visitor
# Convert this CSS node to xpath with +prefix+ using +visitor+
def to_xpath prefix = '//', visitor = XPathVisitor.new
self.preprocess!
prefix = '.' if ALLOW_COMBINATOR_ON_SELF.include?(type) && value.first.nil?
prefix + visitor.accept(self)
end

Expand Down
8 changes: 7 additions & 1 deletion lib/nokogiri/css/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ rule
: selector COMMA simple_selector_1toN {
result = [val.first, val.last].flatten
}
| prefixless_combinator_selector { result = val.flatten }
| simple_selector_1toN { result = val.flatten }
;
combinator
: PLUS { result = :DIRECT_ADJACENT_SELECTOR }
| GREATER { result = :CHILD_SELECTOR }
| TILDE { result = :PRECEDING_SELECTOR }
| TILDE { result = :FOLLOWING_SELECTOR }
| S { result = :DESCENDANT_SELECTOR }
| DOUBLESLASH { result = :DESCENDANT_SELECTOR }
| SLASH { result = :CHILD_SELECTOR }
Expand Down Expand Up @@ -59,6 +60,11 @@ rule
)
}
;
prefixless_combinator_selector
: combinator simple_selector_1toN {
result = Node.new(val.first, [nil, val.last])
}
;
simple_selector_1toN
: simple_selector combinator simple_selector_1toN {
result = Node.new(val[1], [val.first, val.last])
Expand Down
4 changes: 2 additions & 2 deletions lib/nokogiri/css/xpath_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def visit_class_condition node
{
'combinator' => ' and ',
'direct_adjacent_selector' => "/following-sibling::*[1]/self::",
'preceding_selector' => "/following-sibling::",
'following_selector' => "/following-sibling::",
'descendant_selector' => '//',
'child_selector' => '/',
}.each do |k,v|
class_eval %{
def visit_#{k} node
"\#{node.value.first.accept(self)}#{v}\#{node.value.last.accept(self)}"
"\#{node.value.first.accept(self) if node.value.first}#{v}\#{node.value.last.accept(self)}"
end
}
end
Expand Down
15 changes: 15 additions & 0 deletions test/css/test_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ def test_direct_preceding_selector
@parser.parse("E + F G")
end

def test_prefixless_child_selector
assert_xpath("./a", @parser.parse('>a'))
assert_xpath("./a//b/i", @parser.parse('>a b>i'))
end

def test_prefixless_preceding_sibling_selector
assert_xpath("./following-sibling::a", @parser.parse('~a'))
assert_xpath("./following-sibling::a//b/following-sibling::i", @parser.parse('~a b~i'))
end

def test_prefixless_direct_adjacent_selector
assert_xpath("./following-sibling::*[1]/self::a", @parser.parse('+a'))
assert_xpath("./following-sibling::*[1]/self::a/following-sibling::*[1]/self::b", @parser.parse('+a+b'))
end

def test_attribute
assert_xpath "//h1[@a = 'Tender Lovemaking']",
@parser.parse("h1[a='Tender Lovemaking']")
Expand Down